解题思路:
检测每一个字符周围八个字符是否存在地雷,考虑到字符四个角落及四条边存在限制,因此在四个角落及四条边外面在套上另外一层空字符串,使得每一个输入的字符串都存在八个领字符串。
注意事项:
参考代码:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int a, b;
int num = 1;
while (cin >> a >> b)
{
if (a == 0 && b == 0)
break;
string* s = new string[a];
for (int i = 0; i < a; i++)
{
cin >> s[i];
}
cout << "Field #" << num << ":" << endl;
string* str = new string[a + 2];
string s1 = "";
for (int i = 0; i < b+2; i++)
{
s1 += '0';
}
string s2 = s1;
str[0] = s1;
str[a + 1] = s2;
for (int i = 0; i < a; i++)
{
str[i + 1] ='0'+ s[i]+'0';
}
for (int y = 1; y < a + 1; y++)
{
for (int x = 1; x < b + 1; x++)
{
if (str[y][x] == '*')
{
cout << '*';
continue;
}
else
{
int c = 0;
if (str[y - 1][x - 1] == '*')
c++;
if (str[y - 1][x] == '*')
c++;
if (str[y - 1][x + 1] == '*')
c++;
if (str[y][x - 1] == '*')
c++;
if (str[y][x + 1] == '*')
c++;
if (str[y + 1][x - 1] == '*')
c++;
if (str[y + 1][x] == '*')
c++;
if (str[y + 1][x + 1] == '*')
c++;
cout << c;
}
}
cout << endl;
}
num++;
cout << endl;
}
return 0;
}
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题10.3 (C语言代码)浏览:823 |
C语言程序设计教程(第三版)课后习题8.9 (C++代码)(其实不需要开数组,一个一个字符读入并判断就好了)浏览:925 |
C二级辅导-公约公倍 (C语言代码)浏览:2122 |
九宫重排 (C++代码)浏览:2160 |
C语言训练-计算1~N之间所有奇数之和 (C语言代码)浏览:644 |
C语言程序设计教程(第三版)课后习题5.8 (C语言代码)浏览:567 |
成绩转换 (C语言代码)浏览:1005 |
淘淘的名单 (C语言代码)答案错误???浏览:593 |
P1001 (C语言代码)浏览:799 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:574 |