3
5
2015
0

UVa 10189 Minesweeper

#include <cstdio>
#define ok(p,q) p>0 && p<=n && q>0 && q<=m && a[p][q]=='*' //用宏简化代码
int n,m,x;
char a[101][101];

int main()
{
	scanf("%d%d",&n,&m);
	while (n!=0){
		printf ("Field #%d:\n",++x);
		getchar();
		for (int i=1;i<=n;++i,getchar())
			for (int j=1;j<=m;++j)
				a[i][j]=getchar();
		for (int i=1;i<=n;++i,putchar('\n'))
			for (int j=1;j<=m;++j){
				if (a[i][j]=='*'){ //是雷直接输出
					putchar('*');
					continue;
				}
				a[i][j]='0'; //直接用ascii码操作
				if (ok(i-1,j-1)) ++a[i][j];
				if (ok(i-1,j))   ++a[i][j];
				if (ok(i-1,j+1)) ++a[i][j];
				if (ok(i,j-1))   ++a[i][j];
				if (ok(i,j+1))   ++a[i][j];
				if (ok(i+1,j-1)) ++a[i][j];
				if (ok(i+1,j))   ++a[i][j];
				if (ok(i+1,j+1)) ++a[i][j];
				putchar(a[i][j]);
			}
		scanf("%d%d",&n,&m);
		if (n) putchar('\n'); //惨痛的教训,最后一个矩阵后不加空行
	}
	return 0;
}
Category: 题解 | Tags: UVa 挑战编程 模拟 | Read Count: 229

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com