PC/UVa:110102/10189
题目要求计算n * m
矩阵中每个非地雷位置周围的地雷数目和,没有什么难度,但是最主要的问题是输出格式,每两个输出结果之间用一个空行隔开,最后一个输出结果后没有空行。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char field[100][100];
int row = 0, col = 0;
int num = 1;
while (cin >> row >> col){
if (row == 0 && col == 0) break;
if (num >= 2) cout << endl;
memset(field, 0, 100 * 100 * sizeof(char));
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cin >> field[i][j];
}
}
cout << "Field #" << num++ << ":" << endl;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (field[i][j] == '.'){
int cnt = 0;
if (i - 1 >= 0 && j - 1 >= 0)
cnt += field[i - 1][j - 1] == '*' ? 1 : 0;
if (i - 1 >= 0)
cnt += field[i - 1][j] == '*' ? 1 : 0;
if (i - 1 >= 0 && j + 1 < col)
cnt += field[i - 1][j + 1] == '*' ? 1 : 0;
if (j + 1 < col)
cnt += field[i][j + 1] == '*' ? 1 : 0;
if (i + 1 < row && j + 1 < col)
cnt += field[i + 1][j + 1] == '*' ? 1 : 0;
if (i + 1 < row)
cnt += field[i + 1][j] == '*' ? 1 : 0;
if (i + 1 < row && j - 1 >= 0)
cnt += field[i + 1][j - 1] == '*' ? 1 : 0;
if (j - 1 >= 0)
cnt += field[i][j - 1] == '*' ? 1 : 0;
cout << cnt;
}
else cout << '*';
}
cout << endl;
}
//cout << endl;
}
return 0;
}