# Enumeration method -- "Aha! Algorithm

Aha, teacher Lei's Aha! Algorithm learning record.

Enumeration method can intuitively solve our problems, but it will waste time.
For example, we have one: [] [] [] + [] [] [] [] = [] [] []. We fill in 1 ~ 9. How many methods are there?
Enumeration can be used to write out intuitively:

```#include<stdio.h>
int main()
{
int a,b,c,d,e,f,g,h,i;
int sum=0,n;
for(a=1;a<=9;a++)
for(b=1;b<=9;b++)
for(c=1;c<=9;c++)
for(d=1;d<=9;d++)
for(e=1;e<=9;e++)
for(f=1;f<=9;f++)
for(g=1;g<=9;g++)
for(h=1;h<=9;h++)
for(i=1;i<=9;i++)
if(a!=b&&a!=c&&a!=d&&a!=e&&a!=f&&a!=g&&a!=h&&a!=i
&&b!=c&&b!=d&&b!=e&&b!=f&&b!=g&&b!=h&&b!=i
&&c!=d&&c!=e&&c!=f&&c!=g&&c!=h&&c!=i
&&d!=e&&d!=f&&d!=g&&d!=h&&d!=i
&&e!=f&&e!=g&&e!=h&&e!=i
&&f!=g&&f!=h&&f!=i
&&g!=h&&g!=i
&&h!=i
&&100*a+10*b+c+100*d+10*e+f==100*g+10*h+i)
{
sum++;
}
n=sum/2;//The same is true
printf("%d species",n);
getchar();getchar();
return 0;
}
```

The above code can also be carried by an array:

```#include<stdio.h>
int main()
{
int array,i,total,book,sum,n;
total=0;
for(array=1;array<=9;array++)
for(array=1;array<=9;array++)
for(array=1;array<=9;array++)
for(array=1;array<=9;array++)
for(array=1;array<=9;array++)
for(array=1;array<=9;array++)
for(array=1;array<=9;array++)
for(array=1;array<=9;array++)
for(array=1;array<=9;array++)
{
for(i=1;i<=9;i++)
{
book[i]=0;
}
for(i=1;i<=9;i++)
{
book[array[i]]=1;
}
sum=0;
for(i=1;i<=9;i++)
{
sum=sum+book[i];
}
if(sum==9&&array*100+array*10+array+array*100+array*10+array==array*100+array*10+array)
{
total++;

}

}
n=total/2;
printf("%d species",n);
getchar();getchar();
return 0;
}
```

There is a bomb man's Chestnut in the book: it roughly means to destroy the enemy by placing a bomb. The bomb can only be placed in the open space. The bomb can move up, down, left and right. It will be blocked when it meets the wall, and it will destroy the enemy when it meets the enemy. There is also a wall in the game that can be exploded, but it can't penetrate after being exploded. Then there is a super bomb, which can be at an unlimited distance. Then where can we put our bomb to blow up the most people? The author gives an idea that the wall is # represented, the enemy is represented by G, and our free area is represented by. Then we use a two-dimensional string array to store our map, and use two variables to represent the relationship between rows and columns.

```#include<stdio.h>
int main()
{
char array;
int m,n;
int x,y;
int sum;//Number of enemies that can be destroyed
scanf("%d %d",&n,&m);
int i,j;
int p,q,map;//The defined variable should not be defined in the loop statement, otherwise it becomes a local variable and should be defined as a global variable.
{
scanf("%s",array[i]);
}
for(i=0;i<=n-1;i++)
{
//int j;
for(j=0;j<=m-1;j++)
{
if(array[i][j]=='.')//Bomb on the ground
{
//int sum;// Number of enemies that can be destroyed
sum=0;
//int x,y;
x=i;y=j;
//Upward attack
while(array[x][y]!='#'/ / if it's not a wall
{
//See if there's anyone
if(array[x][y]=='G')
{
sum++;
}
x--;//Continue upward statistics
}
x=i;y=j;
//Downward attack
while(array[x][y]!='#')
{
if(array[x][y]=='G')
{
sum++;
}
x++;
}
x=i;y=j;
//Right attack
while(array[x][y]!='#')
{
if(array[x][y]=='G')
{
sum++;
}
y++;
}
x=i;y=j;
//Attack left
while(array[x][y]!='#')
{
if(array[x][y]=='G')
{
sum++;
}
y--;
}

if(sum>map)//Constantly updated comparison
{
map=sum;
p=i;
q=j;
}
}
}
}
printf("(%d , %d)The place can be destroyed%d An enemy\n",p,q,map);
getchar();getchar();
return 0;

}
```

In the book, the author also mentioned a problem called "matchstick equation". According to my personal understanding, after checking some materials, I feel that it is slightly vague in the book. Let's take a look at the problem first:  Then he asked: if Xiao hum has m (m ≤ 24) matchsticks in his hand, how many different ways can Xiao hum spell?

In the most straightforward way, we can enumerate each bit to establish the relationship between them, and the author also wrote that it will run for more than 1000 seconds. So the author cited A chestnut, that is, enumerating A and B, then C is represented by A+B.

```#include<stdio.h>
int fun(int x)
{
int num=0;
int book={6,2,5,5,4,5,6,3,7,6};  //Use an array to record the number of matchsticks required for numbers 0 to 9
while(x/10!=0)  // If x divided by 10 is not equal to 0, it means that the number has at least two digits
{
num=num+book[x%10];
x=x/10;
}
num=num+book[x];  //Add the highest number of matchsticks
return num;
}
int main()
{
int a,b,c,m,sum=0;
scanf("%d",&m);   //Total number of matchsticks
//It's 1111 in the book, but it's better to write 11111 here
for(a=0;a<=11111;a++)   //Start enumeration
{
for(b=0;b<=11111;b++)
{
c=a+b;
if(fun(a)+fun(b)+fun(c)==m-4)
{
printf("%d+%d=%d\n",a,b,c);
sum++;
}
}
}
printf("%d Methods",sum);
return 0;
}
```

For the understanding of "1111", please refer to this blogger's article: About Aha! Analysis of matchstick equation "1111" problem in Chapter 3 of algorithm.

Total arrangement of numbers
How many ways to arrange 1, 2 and 3?

```#include<stdio.h>
int main()
{
int a,b,c;
for(a=1;a<=3;a++)
for(b=1;b<=3;b++)
for(c=1;c<=3;c++)
if(a!=b&&a!=c&&b!=c)
printf("%d%d%d\n",a,b,c);
return 0;
}
```

When the data is larger, this method will not work, and the subsequent "search" will help us.

Algorithmic clock out~

Tags: C Algorithm

Posted on Wed, 01 Sep 2021 19:07:02 -0400 by aprinc