扫雷
Description
扫雷游戏你一定玩过吧!现在给你若干个n×m的地雷阵,请你计算出每个矩阵中每个单元格相邻单元格内地雷的个数,每个单元格最多有8个相邻的单元格。 0<n,m<=100
Input
输入包含若干个矩阵,对于每个矩阵,第一行包含两个整数n和m,分别表示这个矩阵的行数和列数。接下来n行每行包含m个字符。安全区域用‘.’表示,有地雷区域用'*'表示。当n=m=0时输入结束。
Output
对于第i个矩阵,首先在单独的一行里打印序号:“Field #i:”,接下来的n行中,读入的'.'应被该位置周围的地雷数所代替。输出的每两个矩阵必须用一个空行隔开。
(注意两个矩阵之间应该有一个空行,由于oj的格式化这里不能显示出来)
Sample Input 1
4 4 *... .... .*.. .... 3 5 **... ..... .*... 0 0
Sample Output 1
Field #1: *100 2210 1*10 1110 Field #2: **100 33200 1*100
Hint
HINT:时间限制:1.0s 内存限制:256.0MB
0<n,m<=100
思路:建二维数组,对找到地雷的周围8位加一。
代码不难但是要注意格式和跨越数组的问题。还有基本上刷题网站不支持数组={0}这样的初始化,要自己遍历初始化一下,我找这bug找半天。
AC代码如下:
#include <iostream>
using namespace std;
int main(){
int n,m,ant = 0;
while(cin >> n >> m){
if(n == 0 && m == 0) break;
ant++;
char s[n+2][m+2];
for(int i = 0;i < n+2;i++){
for(int j = 0;j < m+2;j++){
s[i][j] = '0';
}
}
for(int i = 1;i < n+1;i++){
for(int j = 1;j < m+1;j++){
cin >> s[i][j];
}
}
for(int i = 1;i < n+1;i++){
for(int j = 1;j < m+1;j++){
if(s[i][j] != '*'){
s[i][j]='0';
if(s[i-1][j-1] == '*') s[i][j]++;
if(s[i-1][j] == '*') s[i][j]++;
if(s[i-1][j+1] == '*') s[i][j]++;
if(s[i][j-1] == '*') s[i][j]++;
if(s[i][j+1] == '*') s[i][j]++;
if(s[i+1][j-1] == '*') s[i][j]++;
if(s[i+1][j] == '*') s[i][j]++;
if(s[i+1][j+1] == '*') s[i][j]++;
}
}
}
cout <<"Field #"<<ant<<":"<<endl;
for(int i = 1;i < n+1;i++){
for(int j = 1;j < m+1;j++){
cout << s[i][j];
}
cout << endl;
}
cout << endl;
}
return 0;
}
附一个测试样例:
50 20
.*...*..............
..*.....*..........*
..*.......*...**.*..
*.*..*...*..........
....*........**....*
.............*..*.*.
*............**.....
........***.........
....*..*...*........
.**.................
*....*.....*...*....
......*...*.*.......
.*..*.*.*...........
*.......*...*.....*.
.....*.....**....***
****.........**.*..*
**......*.......*...
.....*......*.*...*.
..*.*....*...*.*.**.
**.....*.*.*..*.**..
.*........*....**...
.*.*.......*....*...
.....*..*...*...*...
*.*......*..........
.*...**.**........*.
*.*..*.**.....*.....
...........*.*...*..
..**..*..*.*..**....
.....*....***.......
****..*.....**....*.
..*..*.*.*.....**..*
.*......*.*..*.*....
.*....*.*......*....
..*.*......*...*..**
..*....**.*.......*.
..*..*........*.*...
.......**.*....*....
..*.....**.*.*..*.*.
....**.......*.*....
...*..*.*.........*.
*..*.*....*.*...**.*
*..*....**.........*
***.***.....*.......
*.***..*.......*.*..
.....*.....*........
....*....*.....*...*
..*.***....*.*.*.*.*
.*.*..**.......*.*.*
.........*..**......
..*.......*........*
20 50
...*........*.*............*...*...**..*.*..*.....
.*......*.****.**..*........*.*...***...*.........
.........................*....**..*.....*.*.*...*.
.....**......*....****............*..*...*..*..*..
.**..***...*.......*..*.........*.*.**...*.**.*...
****....*.......*.*.****.........*..............**
.*...*.*.*.......*......*.....*.....*..***..*.....
.....**.....*.....*.........*.*.......*.....*.....
.........*..*....*...****.*.*.*.***........**.**..
..........*....*..*...***..*....*........**....*..
...*.*......*..*.**.....*.**..*..*......**........
..*......*...*......*..*.**....**......*.**...*..*
......*.......*....*.*..****.....*..*.*.*.....*...
..*..*.**.........**.*..*.......*...........*.....
...*..**..*.*.............***.**.*........*.......
...**..*......*..***....*.....*.**..***....**...*.
....*.....*.....*..*...*.........*......*..**...*.
..*.....*.**...*.*.*.....*.*..............*..*.**.
.................*....*.*....*..*....**..****.*...
..*................*.*..*..**.*........*.*.*...**.
50 20
*...........**......
.*......*.**..*.*.*.
.........*..*.*...**
...*.....*..**.*.*..
..**...**..*...*.*..
....*..*..*.**......
*.*.....*.*.*....*..
..*.*...*....*..*...
....*..**...*...*.*.
....*.***.....*.**.*
...***........*.*...
.........*.......*..
....*.*..***...*...*
.......*.*.**.*.....
...*.****...*.*.*...
.*.......**...**...*
.*.....*...**...*...
.........*........**
*........*.*.***...*
..*..*..........**.*
.**..*..**...*......
......*.......*.....
.*........*.......*.
............**.*..*.
....*..*.*.**.......
..*....*.....**..***
.....*..*..*......*.
.....*..*.*.........
...*..*.*.*.*.*....*
..*....**.*...**..**
*.........*..*..*.*.
...*.***...***..*...
......*...***..*...*
.........*......*...
........**..........
.....*......*..*...*
*...**..**.......*..
...*...*..****.....*
...*...***....*.*.*.
**..**.*...**..*....
*.*.*.......*.......
..*...*...*.*...*.*.
.*.*..*.....*.*.....
.**....**...*.**....
...*.......*.*...*..
*........**.......**
..*....*..*.....**..
...*...*..**.*.*....
...*.....*....**....
*.*.*.......*..*..*.
20 50
....*...*..**.....**.*...*.....*.....**...*.*...**
..**...*..*....*.***....*..***......*.....*.......
...*..*....*..........*....**.....*....**.........
*...*.....................*...*.......*.....**....
.....*........**...*..**...*.........*..*....**...
...*....*.......*.....**.**.*..*..*.........*.....
..*.........*...*.................***..**.**...*.*
.........................*.*....*.*.........*...*.
..*...........****.*.*..**.*..*....*..*........*..
.*.*.*.*....*..**.*...*...*..*....**............*.
.*......*........*...***..***..**...*.***..**.***.
*.....*.*........*..*.*...*.*......*.............*
..*......*..........*..*...***..*....*..*..*.*.*..
...*...**........*...*..........*..*....**...*....
*......*.*.....*....*...*.*.*....*.....*..*.......
*......*.*..*.....*.....**....*...*.****....*..*..
.........*...***..*....**...*.....*..*...*.***....
.......*...........*..*........*.*...*...**..**...
.*..*.......*...*.***.**.*.*..*..*..*......*....*.
*..*....*.*...*..*......*...**...*.....*..*.......
50 20
..........*....**.**
..**..*.....*.......
...*................
......**....*..*....
....*.......*...*...
....*...*...*.*..*.*
*.............*.....
***.........*....**.
.....***.....*.*.*..
****....*.*....*..*.
*....*..**.......*..
...........*........
....*...............
*....*....**..*....*
*..*.............*..
.......*..*..*..*..*
........**..*...*.*.
*..*...*....*.*.*...
...*............**..
..**...**.*.*.....*.
..*..*..*..*.....*..
*..*.......*.......*
...*...*.*.......**.
.*.****...*...*....*
..**.*..............
..**.*...*........*.
.*..*....*..**......
..............*...**
.....*...***........
.*..............*..*
*....*..*..........*
...*..**.........**.
.........*..*...**..
..***......****.*...
.*....**.*.......*..
......*...*..*......
**.....*.........**.
.*.....*.*.........*
.*..**.....*........
*.........*.**...***
.*....**.....**.*.*.
.....*.*.....*..*..*
........**..**.*.*.*
...**.......*...*...
*......*....*......*
....*....*...*.**...
*.*...............*.
...*..*.*.*...*...*.
...*.*.***..*...**..
..*.*...**.**......*
20 50
*.....*....*..........**..**.*......*.*.*..*......
........*..*.........*....*.......*..*..*.......*.
.*.*..*.**......*..**..*.............*..*.*..*..*.
.*....*.....***..*........*.**..*.......*...*.**..
........*..*....*......*.......**......*.**..*....
.**...*.*.**...*.*....*......*..**..*....**....*..
....***..*..*.**.*....*.........*..*......*....***
..*..**...**..**....*.**.*...*...*.*......**.****.
........*....*...***.........*.***........*..*...*
..............*........*..*...........*.....*.....
...***..*..*......*.....*.............****......*.
......**.*.......*....**.*.*.....**..*....*.*...*.
*......***..*.......**..****...*.**....*****......
...........*.*.....**...*...***.*...*.**......*...
....*..*....*.**..*.....**..*.*.*...*...*.*.*.....
......*.......*.....**.*.**....***.....**...*.....
..*.***.......****.**.*.......*......*.......***..
.*...***...........**...*.**.........*.*.......**.
.*.*.......*.*.......*..***...*......*.**....*.*..
.*....***..........*.*.*...**.*.....*.*.*....*.*.*
50 20
*.*...*.*.**...**...
**..........**...*..
**.**.....*...*..*.*
**..**.*.......*....
.*....*...*.*.*...*.
..*.........*.....*.
......*.....**.....*
.*.*....*....**..*..
*..*.*..............
.*...*...*...**....*
.*....*...*....*...*
........**.....*.***
.....*.*..**..*.*...
.......*.....*..*.**
.....*..*...*....*..
......*.......*..*..
*.......*...*.......
*..*....*...**.....*
*............**...*.
..*.....**......*..*
.....*..**...**.....
....*....***.**.....
........*....*...*..
....**.*.....**.***.
.*....*..*.....*.*..
.......*....*......*
.*.*.*.*........*..*
...*...........*..*.
..*...........*.....
***...*..*.**.......
**..**...*........*.
.......*..***..*****
..*.**..**.....*...*
*....*...**..*.....*
**....*....*....*...
.............*.*.*..
.*.*.......*........
.........*.....*....
*...*..**...........
**....*...*..*..***.
.*................*.
..*....*......*.....
*...*.*..**.....*...
.....*.*............
.***....**....*.....
**.......*.*..*.....
.*.*.**...*..*......
....***......*.*..**
...*..**.**..*.*..**
..*.*...*.......*...
20 50
*.**....*.*......**.....*......**.........**......
*.*......*..**...*.*....*.*..........*.***...*....
.......**.***.......*...**..**.*...*....**........
...*..**....*....*.....*..**...*.*....*...........
...*....*...........*.....*..**....***.....*..*...
......*.............**.*......*.........*.......*.
.**..*....*...**....*...*..*..*..*..*......**.*.*.
..*.***...........*.....**..*.*....*....*.........
.....*....*.**...****..*.*.......*...*..*.***....*
..**...**...*.....**..***..*....*.............*...
.*..*.*.........*.*..*.*.*..**..*.**..*.........*.
...*...*.*..**...*.**.**...*.*...*..*...*.**.*..**
.....**....*...*...*.........**....*..*..**...*...
..*..*..*......*..**.**...**.*.**....*.*...*......
..*.....*..*....*...*..........*...*.............*
*...*......*.*.....*...........*.......*..........
**......*..*..*..*........*..*...*....*...*..*..**
.......*......*....**.*...*....*.....*.....**.....
*..*.**..*..*.***..*....**.**......*...***.**..*.*
...**...*.....*....**....*....*.........*........*
50 20
.......*..*...*....*
......**...*..*.....
......*...........*.
.*..........*.......
..*.................
....*.*.............
.*.......*..........
...........*...*.*..
...*....**.....*..*.
**.*.......*..*....*
.....*...*.*.**..*..
.*..*.............*.
....*.........*.*...
.....*..*....**.**..
......***..*...**...
...*.......*.....*..
**.......*..*..****.
*.**.....*......*.*.
*..*.**.*..**...*...
.*........**.......*
*.......*....*......
................**..
.*...*.*....*.......
**.*.......*......*.
*****..*...**...*...
.*.*............*...
.....*.......**.....
.....*....*.....*.*.
.*..........*......*
.......**.*.*.*.....
*...*.*...........*.
.*...*...***.**....*
..*..*....*..*....*.
...*..*.**.**......*
............**.....*
*..*.....*..*.......
*.......*.....*.....
.......*.*.*.***....
.........*....*.*...
..*........**..*....
*....*....**.....*..
.**.****....*....*.*
.**...............*.
.......*............
..............*..*.*
.*...*..*.*....*....
.....*..***.*.*...*.
*.....*.**..*......*
..*.*.*.............
.*..**.*...*.***..**
20 50
.*..*...............*..****..*..............*..***
*.**....**.**....*...*.**......*.*................
..**.*.*.....*.........*..*...**..*..........*.*..
..*....*.*....*...*...**.**....*..*.....*.*.**....
*...**...**..*........*...***.......*..*...*.*....
..*.............*.*.**..*.*.*.........***....**.*.
..**..*.*.....*.*...*..***...*...**....*......**..
..*.**.......*....***........*.....*......*..**.*.
...........*......*.**......*.*...*****....**.****
...*.*.*..**....*......***.**.*.*......*.**......*
.....*.....**.....*.*....*....***........**....**.
**.*.....*.*...**..*..*..*.**..*...*.....*....**..
..*.*...*...........****.....*.*...***...*......*.
...*.......*.*....*.*...........**..........*...*.
.*.........**.*.**..........**....*...*....*..*..*
...*.*...**........*.........*........**....*.*..*
.....*.*...*...*...*...*....*....*....**.....**.*.
...*......*................*..*.*.*.**....*.*....*
..*...*.......*...*........*.*......***.....*.....
*..*...............*..***.*..***..*.....*.....*.*.
0 0