枚举法的应用

枚举法的应用——炸弹人小游戏

  1 #include <iostream>
  2 
  3 #define H 20    //地图行上限 
  4 #define L 21    //地图列上限 
  5 
  6 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  7 
  8 void BackUp(char map[][L],int n,int m,char backUpMap[][L])    //将map中的地图备份到backUpMap中 
  9 {
 10     for(int i=0;i<n;i++)
 11     {
 12         for(int j=0;j<=m;j++)
 13         {
 14             backUpMap[i][j]=map[i][j];
 15         }
 16     }
 17 }
 18 
 19 void Bomb(char map[][L],int n,int m,int a,int b)    //根据炸弹的位置改造地图 
 20 {
 21     if(map[a][b]=='.')
 22     {
 23         int sum=0;
 24         int y=a;
 25         int x=b;
 26         while(map[y][x]!='#'&&y<n)    //向上统计
 27         {
 28             if(map[y][x]!='.')
 29             {
 30                 map[y][x]='.';
 31             }
 32             ++y;
 33         }
 34         y=a;    //归位 
 35         x=b;
 36         while(map[y][x]!='#'&&y>=0)    //向下统计 
 37         {
 38             if(map[y][x]!='.')
 39             {
 40                 map[y][x]='.';
 41             }
 42             --y;
 43         }
 44         y=a;
 45         x=b;
 46         while(map[y][x]!='#'&&x>=0)    //向左统计 
 47         {
 48             if(map[y][x]!='.')
 49             {
 50                 map[y][x]='.';
 51             }
 52             --x;
 53         }
 54         y=a;
 55         x=b;        
 56         while(map[y][x]!='#'&&x<m)    //向右统计
 57         {
 58             if(map[y][x]!='.')
 59             {
 60                 map[y][x]='.';
 61             }
 62             ++x;
 63         }
 64         printf("炸弹爆炸后:
");
 65         for(int i=0;i<n;i++)
 66         {
 67             printf("%s
",map[i]);
 68         }                  
 69     }     
 70 }
 71 
 72 int Analyse(char map[][L],int n,int m,int i,int j)    //分析炸弹爆炸将造成的伤害 
 73 {
 74     if(map[i][j]=='.')
 75     {
 76         int sum=0;
 77         int y=i;
 78         int x=j;
 79         while(map[y][x]!='#'&&y<n)    //向上统计
 80         {
 81             if(map[y][x]!='.')
 82             {
 83                 ++sum;
 84             }
 85             ++y;
 86         }
 87         y=i;    //归位 
 88         x=j;
 89         while(map[y][x]!='#'&&y>=0)    //向下统计 
 90         {
 91             if(map[y][x]!='.')
 92             {
 93                 ++sum;
 94             }
 95             --y;
 96         }
 97         y=i;
 98         x=j;
 99         while(map[y][x]!='#'&&x>=0)    //向左统计 
100         {
101             if(map[y][x]!='.')
102             {
103                 ++sum;
104             }
105             --x;
106         }
107         y=i;
108         x=j;        
109         while(map[y][x]!='#'&&x<m)    //向右统计
110         {
111             if(map[y][x]!='.')
112             {
113                 ++sum;
114             }
115             ++x;
116         }    
117         return sum; 
118     }    
119     return 0;
120 }
121 
122 void SearchBest(char map[][L],int n,int m)    //查找最优位置 
123 {
124     int p=0;
125     int q=0;
126     int max=0;
127     for(int i=0;i<n;i++)
128     {
129         for(int j=0;j<m;j++)
130         {
131             int sum=Analyse(map,n,m,i,j);
132             if(sum>max)
133             {
134                 max=sum;
135                 p=i;
136                 q=j;
137             }                 
138         }
139     }
140     printf("最佳位置为(%d,%d):
",p,q);
141     Bomb(map,n,m,p,q);        
142     printf("共炸死%d个敌人
",max);    
143 }
144 
145 int main(int argc, char** argv) {
146     char map[H][L];        //地图大小不超过H*L
147     int n,m,a,b;    //n代表行数,m代表列数 //a,b代表放炸弹的位置 
148     printf("请输入行数和列数:");
149     scanf("%d%d",&n,&m);
150     printf("请创建一个%d行%d列的地图:
",n,m);
151     for(int i=0;i<n;i++)    //读入地图 
152     {
153         scanf("%s",&map[i]);
154     }
155     printf("您创建的地图如下:
");
156     for(int i=0;i<n;i++)
157     {
158         printf("%s
",map[i]);
159     } 
160     char backUpMap[H][L];
161     BackUp(map,n,m,backUpMap);
162     printf("请输入您要放炸弹的位置(a,b):");
163     scanf("%d%d",&a,&b);
164     int sum=Analyse(map,n,m,a,b);
165     Bomb(map,n,m,a,b);
166     printf("共炸死%d个敌人
",sum);
167     printf("
-----------------------------
");
168     SearchBest(backUpMap,n,m);
169     return 0;
170 }
原文地址:https://www.cnblogs.com/flypie/p/4929824.html