解题思路:创建一个长和宽均为原本长度+2的bool数组,可避免繁杂的边界讨论
注意事项:
参考代码:
#include <iostream>
#include <string.h>
using namespace std;
int main() {
int a, b;
int p = 1;
while (cin >> a >> b) {
if (a == 0 & b == 0)
break;
else {
char s[a + 2][b + 2] = {0}; //初始化
for (int i = 1; i < a + 1; i++)
for (int j = 1; j < b + 1; j++)
cin >> s[i][j];
bool bomb[a + 2][b + 2];
for (int i = 0; i < a + 2; i++)
for (int j = 0; j < b + 2; j++) {
if (s[i][j] == '*')
bomb[i][j] = true;
else
bomb[i][j] = false;
}
for (int i = 1; i < a + 1; i++)
for (int j = 1; j < b + 1; j++) {
int n = 0;
if (!bomb[i][j]) {
if (bomb[i - 1][j - 1])
n++;
if (bomb[i - 1][j])
n++;
if (bomb[i - 1][j + 1])
n++;
if (bomb[i][j - 1])
n++;
if (bomb[i][j + 1])
n++;
if (bomb[i + 1][j - 1])
n++;
if (bomb[i + 1][j])
n++;
if (bomb[i + 1][j + 1])
n++;
s[i][j] = n + '0';
}
}
cout << "Field #" << p << ":" << endl;
p++;
for (int i = 1; i < a + 1; i++) {
for (int j = 1; j < b + 1; j++)
cout << s[i][j];
cout << endl;
}
cout << endl;
}
}
return 0;
}
0.0分
2 人评分
回文串 (C语言代码)浏览:3095 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:561 |
A+B for Input-Output Practice (VII) (C语言代码)浏览:566 |
C二级辅导-同因查找 (C语言代码)浏览:618 |
用筛法求之N内的素数。 (C语言代码)浏览:595 |
C二级辅导-等差数列 (C语言代码)浏览:891 |
A+B for Input-Output Practice (II) (C语言代码)浏览:622 |
C二级辅导-等差数列 (C语言代码)浏览:831 |
C语言训练-斐波纳契数列 (C语言代码)浏览:540 |
明明的随机数 (C语言代码)浏览:965 |