poj2612

简单模拟

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
using namespace std;

#define maxn 11

int n;
bool map[maxn][maxn], touched;
char out[maxn][maxn];

void input()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
char ch = getchar();
if (ch == '.')
map[i][j]
= false;
else
map[i][j]
= true;
}
getchar();
}
}

int boom(int x, int y)
{
if (x < 0 || y < 0 || x >= n || y >= n)
return 0;
if (map[x][y])
return 1;
return 0;
}

int cal(int x, int y)
{
return boom(x + 1, y) + boom(x + 1, y + 1) + boom(x + 1, y - 1) + boom(x - 1, y) + boom(x - 1, y + 1) + boom(x - 1, y - 1) + boom(x, y + 1) + boom(x, y - 1);
}

void work()
{
touched
= false;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
char ch = getchar();
if (ch == '.')
out[i][j] = '.';
else
{
out[i][j] = cal(i, j) + '0';
if (map[i][j])
touched
= true;
}
}
getchar();
}
}

void output()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (touched && map[i][j])
{
printf(
"*");
continue;
}
printf(
"%c", out[i][j]);
}
printf(
"\n");
}
}

int main()
{
//freopen("t.txt", "r", stdin);
scanf("%d", &n);
getchar();
input();
work();
output();
return 0;
}

原文地址:https://www.cnblogs.com/rainydays/p/2047582.html