ethan0ne has been particularly interested in minesweeper. It is a small game pre-installed on Windows 7 and before. Now ethan0ne has encountered several n × m minefields. You are asked to count the number of mines in adjacent cells of each cell in each matrix, with up to 8 adjacent cells per cell. 0 < n, m < = 100
The input contains several matrices, and for each matrix, the first row contains two integers, n and m, representing the number of rows and columns of the matrix, respectively. The next n lines contain m characters each. Safe areas are denoted by ‘.’ and mine-prone areas by ‘*’. Input ends when n=m=0.
For the i-th matrix, the sequence number is first printed on a single line: “Field # I:”, and on the next n lines, the ‘.’ read in should be replaced by the number of mines around that position. Each of the two matrices in the output must be separated by a blank line.
4 4
…
…
.…
…
3 5
**…
…
.*…
0 0
Field #1:
100
2210
110
1110
Field #2:
**100
33200
1*100
#include <iostream>
using namespace std;
char trans(int num)
{
for (int i = 0; i <= 9; i++)
{
if (num == i)
return '0' + i;
}
}
int main()
{
char arr[100][100];
int n, m; //代表行和列
int i, j;
int count = 0; //统计,方圆八里的雷有几个
int field = 0;
while (cin >> n >> m, m || n)
{
field++;
//棋盘的输入
for (i = 0; i < n; i++)
cin >> arr[i];
//统计,每一个格的数字应该是几
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (arr[i][j] != '*')
{
count = 0;
int op[] = { 0, 1, -1 };
for (int t1 = 0; t1 < 3; t1++)
{
for (int t2 = 0; t2 < 3; t2++)
{
if (i + op[t1] >= 0 && j + op[t2] >= 0 &&
i + op[t1] < n && j + op[t2] < m &&
arr[i + op[t1]][j + op[t2]] == '*')
count++;
}
}
arr[i][j] = trans(count);
}
}
}
cout << "Field #" << field << ":" << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
cout << arr[i][j];
}
cout << endl;
}
cout << endl;
}
return EXIT_SUCCESS;
}