zcmu--2110: 扫雷

2110: 扫雷

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 111  Solved: 47
[Submit][Status][Web Board]

Description

 扫雷游戏你一定玩过吧!现在给你若干个n×m的地雷阵,请你计算出每个矩阵中每个单元格相邻单元格内地雷的个数,每个单元格最多有8个相邻的单元格。 0<n,m<=100

Input

输入包含若干个矩阵,对于每个矩阵,第一行包含两个整数n和m,分别表示这个矩阵的行数和列数。接下来n行每行包含m个字符。安全区域用‘.’表示,有地雷区域用'*'表示。当n=m=0时输入结束。

Output

对于第i个矩阵,首先在单独的一行里打印序号:“Field #i:”,接下来的n行中,读入的'.'应被该位置周围的地雷数所代替。输出的每两个矩阵必须用一个空行隔开。

Sample Input

4 4

*...

....

.*..

....

3 5

**...

.....

.*...

0 0

Sample Output

Field #1: *100 2210 1*10 1110 Field #2: **100 33200 1*100

HINT

 (注意两个矩阵之间应该有一个空行,由于oj的格式化这里不能显示出来)

数据规模和约定

0<n,m<=100

Source

算法提高

【分析】水题,但是因为数组没有初始化wa两次。。。

#include<bits/stdc++.h>
using namespace std;
char a[105][105];
int dir[][2]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
int main()
{
	int n,m,ca=0;
	while(~scanf("%d%d",&n,&m)&&n&&m)
	{
		if(ca)cout<<endl;
		memset(a,0,sizeof(a));///!!!!!!!!!!!!!!!!!!!!
		for(int i=0;i<n;i++)
			scanf("%s",&a[i]);
		printf("Field #%d:\n",++ca);
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(a[i][j]=='*')cout<<a[i][j];
				else if(a[i][j]=='.'){
					int cnt=0;
					for(int k=0;k<8;k++)
					{
						int x=dir[k][0]+i;
						int y=dir[k][1]+j;
						if(a[x][y]=='*')cnt++;
					}
					cout<<cnt;
				}
			}
			cout<<endl;
		}
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/81609333