解题思路:
检测每一个字符周围八个字符是否存在地雷,考虑到字符四个角落及四条边存在限制,因此在四个角落及四条边外面在套上另外一层空字符串,使得每一个输入的字符串都存在八个领字符串。
注意事项:
参考代码:
#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语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C语言代码)浏览:1027 |
多输入输出练习1 (C语言代码)浏览:1176 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:1027 |
【简单计算】 (C语言代码)浏览:622 |
哥德巴赫曾猜测 (C语言代码)浏览:2313 |
用筛法求之N内的素数。 (C++代码)浏览:692 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:532 |
1908题解浏览:633 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:542 |
A+B for Input-Output Practice (V) (C语言代码)浏览:459 |