Problem B: Minesweeper
Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 29 Solved: 7
[Submit][Status][Web Board]
Description
Minesweeper Have you ever played Minesweeper? This cute little game comes with a certain operating system whose name we can't remember. The goal of the game is to find where all the mines are located within a M x N field. The game shows a number in a square which tells you how many mines there are adjacent to that square. Each square has at most eight adjacent squares. The 4 x 4 field on the left contains two mines, each represented by a ``*'' character. If we represent the same field by the hint numbers described above, we end up with the field on the right: *... .... .*.. .... *100 2210 1*10 1110
Input
The input will consist of an arbitrary number of fields. The first line of each field contains two integers n and m ( 0 < n, m<100) which stand for the number of lines and columns of the field, respectively. Each of the next n lines contains exactly m characters, representing the field. Safe squares are denoted by ``.'' and mine squares by ``*,'' both without the quotes. The first field line where n = m = 0 represents the end of input and should not be processed.
Output
For each field, print the message Field #x: on a line alone, where x stands for the number of the field starting from 1. The next n lines should contain the field with the ``.'' characters replaced by the number of mines adjacent to that square. There must be an empty line between field outputs.
Sample Input
Sample Output
HINT
模拟题。模拟的是扫雷这个小游戏。输入雷的分布,输出每一个格子四周的雷数矩阵。也就是模拟扫雷里数字的产生过程。
一开始把计算那一步想的复杂了,由于没有初始化,我只好把检测的情况分为边缘、内部,边缘又分为四角和边,这样有三种情况,每一种情况的检测区域又不一样,我要每一种都分别定义。后来发现没必要分类,直接检测周围8个格子的区域就行,只不过这样就需要提前初始化好以及让数组留出边缘。
My code:
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 char a[102][102]={0}; 8 int n,m,count=1; 9 while(cin>>n>>m){ 10 if(n==0 && m==0) 11 break; 12 for(int i=1;i<=n;i++) //input 13 for(int j=1;j<=m;j++) 14 cin>>a[i][j]; 15 for(int i=1;i<=n;i++) //计算 16 for(int j=1;j<=m;j++){ 17 if(a[i][j]=='*') 18 continue; 19 20 a[i][j]='0'; 21 if(a[i-1][j]=='*') 22 ++a[i][j]; 23 if(a[i+1][j]=='*') 24 ++a[i][j]; 25 if(a[i][j+1]=='*') 26 ++a[i][j]; 27 if(a[i][j-1]=='*') 28 ++a[i][j]; 29 30 if(a[i-1][j-1]=='*') 31 ++a[i][j]; 32 if(a[i-1][j+1]=='*') 33 ++a[i][j]; 34 if(a[i+1][j-1]=='*') 35 ++a[i][j]; 36 if(a[i+1][j+1]=='*') 37 ++a[i][j]; 38 } 39 40 if(count!=1) 41 cout<<endl; 42 cout<<"Field #"<<count++<<':'<<endl; 43 for(int i=1;i<=n;i++){ //输出 44 for(int j=1;j<=m;j++){ 45 cout<<a[i][j]; 46 } 47 cout<<endl; 48 } 49 } 50 return 0; 51 } 52 53 /************************************************************** 54 Problem: 1099 55 User: freecode 56 Language: C++ 57 Result: Accepted 58 Time:0 ms 59 Memory:1268 kb 60 ****************************************************************/
Freecode : www.cnblogs.com/yym2013