FZU OJ 1056 :扫雷游戏

Problem 1056 扫雷游戏

Accept: 2624    Submit: 6903
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

扫雷是Windows自带的游戏。游戏的目标是尽快找到雷区中的所有地雷,而不许踩到地雷。如果方块上的是地雷,将输掉游戏。如果方块上出现数字,则表示在其周围的八个方块中共有多少颗地雷。

你的任务是在已知地雷出现位置的情况下,得到各个方块中的数据。

*...
.... “*”表示有地雷
.*.. “.”表示无地雷
....
经过处理应得到
*100
2210
1*10
1110

 Input

输入有多组数据,每组数据的第一行有两个数字,m,n(0<m,n<100)表示游戏中雷区的范围为m×n。接下来m行每行有n个字符。“*” 表示有地雷,“.”表示无地雷。最后一组数据m=0,n=0表示输入结束,不需要处理。

 Output

对于每组输入数据,输出结果,各方块数字间不留空格。每组结果之后有一个空行。

 Sample Input

2 3
***
...
4 4
*...
....
.*..
....
0 0

 Sample Output

***
232

*100
2210
1*10
1110

一开始想着这道题要用dfs之类的算法, 后来发现想多了。。。直接遍历一遍记录就可以了,但是没有看到每组输出后有一颗空行,导致错了一次。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
const int maxn=1e6+10;
char a[110][110];
int b[110][110];
int n,m;
int main()
{
	while(cin>>m>>n&&n!=EOF&&m!=EOF)
	{
		if(n==0&&m==0) break;
		for(int i=1;i<=m;i++)
		for(int j=1;j<=n;j++)
		cin>>a[i][j];
		memset(b,0,sizeof(b));
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=n;j++)
			{
				if(a[i][j]=='*')
				{
					b[i-1][j]+=1;
					b[i-1][j-1]+=1;
					b[i-1][j+1]+=1;
					b[i][j-1]+=1;
					b[i][j+1]+=1;
					b[i+1][j+1]+=1;
					b[i+1][j]+=1;
					b[i+1][j-1]+=1;
				}
			}
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=n;j++)
			{
				if(a[i][j]=='*')
				{
					cout<<"*";
					continue;
				}
				else cout<<b[i][j];
			}
			cout<<endl;
		}
		cout<<endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Friends-A/p/9309027.html