解题思路:
注意事项:
参考代码:
#include
#include
using namespace std;
int main()
{
int m, n;
int k = 1;//后面输出第几组数据用的
while ((cin >> m >> n))
{
if (m == 0 && n == 0)//跳出循环
break;
char s[100][100];//存地形
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> s[i][j];
}
}
int a[100][100];//存地雷个数
memset(a, 0, sizeof(a));//将a[][]里的值赋为0
//下面的思路就是,将地雷周围八个点都加一,地雷所在位置记为-1;
// 已知数组都是从(0,0)开始的
//而要从(1,1)这个点开始,确保边上的点周围也有八个点,所以我们需要将数组开大一点
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (s[i][j] == '*')
{
a[i][j] = -1;
if(a[i][j+1]!=-1)
a[i][j + 1] += 1;
if (a[i+1][j + 1] != -1)
a[i + 1][j + 1] += 1;
if (a[i+1][j] !=-1)
a[i + 1][j] += 1;
if (a[i+1][j - 1] != -1)
a[i + 1][j - 1] += 1;
if (a[i][j -1] != -1)
a[i][j - 1] += 1;
if (a[i-1][j - 1] != -1)
a[i - 1][j - 1] += 1;
if (a[i-1][j] != -1)
a[i - 1][j] += 1;
if (a[i-1][j + 1] != -1)
a[i - 1][j + 1] += 1;
}
}
}
//这里要注意大小写,我就是在这里卡了小半天,现在想想都觉得自己是个呆瓜
cout<<"Field" <<" "<<"#"<< k << ":" << endl;
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (a[i][j] == -1)
cout <<s[i][j];
else
cout << a[i][j];
}
cout << endl;
}
cout << endl;
k++;//这里记得加一
}
return 0;
}
0.0分
0 人评分