#include<stdio.h>
void initBoard(char arr[102][102], int N, int M)
{
for (int i = 0; i < 102; i++)
{
for (int j = 0; j < 102; j++)
{
arr[i][j] = '^';
}
}
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= M; j++)
{
scanf("%c", &arr[i][j]);
}
getchar();
}
}
int get_mine_count(char mine[102][102], int x, int y)
{
return (mine[x - 1][y] +
mine[x + 1][y] +
mine[x][y - 1] +
mine[x][y + 1] +
mine[x - 1][y - 1] +
mine[x - 1][y + 1] +
mine[x + 1][y - 1] +
mine[x + 1][y + 1] - 8 * '^')/('*'-'^');
}
void findMine(char arr[102][102], int N, int M)
{
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= M; j++)
{
if (arr[i][j] == '*')
{
printf("%c",arr[i][j]);
}
else
{
printf("%c",'0'+get_mine_count(arr, i, j));
}
}
printf("\n");
}
}
int main()
{
int count = 1;
int N = 0;
int M = 0;
char arr[102][102] = { 0 };
while(scanf("%d %d\n", &N, &M)!=EOF)
{
// getchar();
if (M == N && N == 0)
{
return 0;
}
initBoard(arr, N, M);
printf("Field #%d:\n",count);
findMine(arr, N, M);
printf("\n");
count++;
}
return 0;
}
第11行循环为什么不从0开始呢