Enumerations and exercises

Enumeration (exhaustive algorithm)

Enumeration is a kind of problem-solving strategy that tries to answer one by one, that is, to directly find the answer by violent circulation

Example:
Perfect Cube: An equation of the form a ^ 3 = b ^ 3 + c ^ 3 +10 ^ 3 is called a perfect cube equation. Write a program for a given positive integer N (N <= 100), find all combinations so that the equation holds, where a, b, c, d is greater than 1, less than or equal to N and b <= c <= d .
According to the value of a, output from small to large. When a in the two perfect cubic equations is equal, the output of small b is prioritized, and the output of small c is the same first ...
** Solution idea: ** Quad loop enumeration a, b, c, d, and finally add a Judgment conditions (direct violence resolution)

#include<stdio.h>
int main()
{
  int n;
  scanf("%d",&n);
  for(int a=2;a<=n;a++)
    for(int b=2;b<a;b++)
      for(int c=b;c<a;c++)
        for(int d=c;d<a;d++)
        if(a*a*a==b*b*b+c*c*c+d*d*d)
          printf("%d %d %d %d",a,b,c,d);
    return 0;
        }

Example:
Physiological cycle:
the peak days of human physical strength, emotional intelligence, and IQ, which appear every 23 days, 28 days, and 33 days, respectively. For everyone, we want to know when the three peaks fall on the same day. Given the days p, e, and i where three peaks appear (not necessarily the day when the first peak appears), given another specified day d, and your task is to output the day d, the next three The day when the peak falls on the same day (indicated by the distance d days)
Input:
Enter four integers: p, e, i and d. p, e, and i represent the days when physical, emotional, and intellectual peaks occur, respectively. d is the given day. May be less than p, e or i. All given days are non-negative and less than or equal to 365, and the requested days are less than or equal to 21252.
Output:
From the given day, the next three peak days on the same day (the number of days away from the given day).
The problem-solving idea is generally to add a judgment to the loop, so how can it be faster and reduce the number of loops. The core is as follows

for(k=d+1;(k-p)%23;k++)
  for(;(k-e)%28;k+=23)
    for(;(k-i)%33;k+=23*28)
      printf("%d",k-d);

Example:
Xiaoheng recently fell in love with the "Bomberman" game. Do you remember the Bomberman on the game? Use bombs to destroy the enemy. It is necessary to eliminate all the enemies on the screen and find the hidden door hidden in the wall to pass the level.
 Now there is a special level as follows. You only have one bomb, but this bomb is super powerful (the killing distance is too long, you can destroy all the enemies in the killing range). Where can I put a bomb to eliminate the most enemies?
Insert picture description here
We first model this map. The wall is indicated by #. There are two kinds of walls here, one can be blown up and the other can't be blown up. However, since there is only one bomb, they are all indicated by #, and the bomb cannot pass through the wall. The enemy is denoted by G, and the open space is denoted by. Of course, the bomb can only be placed on the open space.
Enter
13 13
#############
# GG.GGG # GGG. #
###. # G # G # G # G #
#… #… G #
#G #. ###. # G # G #
# GG.GGG. #. GG # #G
#. # G #. #. ###
## G… G… #
#G #. # G ###. # G #
#… G # GGG.GG #
# G #. # G # G #. # G #
# GG.GGG # G.GG #
############# Output:
8

#include<stdio.h>
int main()
{
       char a[20][21];               
       int i,j,sum,map=0,p,q,x,y,n,m;
       scanf("%d%d",&n,&m);           //n代表行,m代表列
       for(i=0;i<n;i++)
          scanf("%s",a[i]);
       for(i=0;i<n-1;i++)
       {
          for(j=0;j<m;j++) 
            {
                 if(a[i][j]=='.')     //如果是空地 
                 {
                      sum=0;                        //记录炸弹数 
                      x=i;y=j;                       //x和y记录此时的i和j 
                      while(a[x][y]!='#')        //不是墙 
                      {
                        if(a[x][y]=='G')         //是怪物 
                        sum++;x--;             //向上走 
                       }
                 x=i;y=j;
                 while(a[x][y]!='#')
                 {
                    if(a[x][y]=='G')
                       sum++;x++;          //向下走 
                   }
               x=i;y=j;
               while(a[x][y]!='#')
               {
                  if(a[x][y]=='G')
                     sum++;y--;    //向左走 
                   }
                x=i;y=j;
                while(a[x][y]!='#')
                {
                   if(a[x][y]=='G')
                       sum++;y++;    //向右走 
                  }
                  if(sum>map)  //储存最大值 
                  {  
                     map=sum;p=i;   //记录地点 
                     q=j;
                     }
                   }
                 }
              }
          printf("将炸弹放在(%d,%d)处,最多可消灭%d个敌人。\n",p,q,map);
          return 0;
}
``
Published 10 original articles · Likes2 · Visits 217

Guess you like

Origin blog.csdn.net/dfwef24t5/article/details/104140786