TOJ 3184 Mine sweeping

描述

I think most of you are using system named of xp or vista or win7.And these system is consist of a famous game what is mine sweeping.You must have played it before.If you not,just look the game rules followed.

There are N*N grids on the map which contains some mines , and if you touch that ,you lose the game.If a position not containing a mine is touched, an integer K (0 < =K <= 8) appears indicating that there are K mines in the eight adjacent positions. If K = 0, the eight adjacent positions will be touched automatically, new numbers will appear and this process is repeated until no new number is 0. Your task is to mark the mines' positions without touching them.

Now, given the distribution of the mines, output the numbers appearing after the player's first touch.

输入

The first line of each case is two numbers N (1 <= N <= 100) .Then there will be a map contain N*N grids.The map is just contain O and X.'X' stands for a mine, 'O' stand for it is safe with nothing. You can assume there is at most one mine in one position. The last line of each case is two numbers X and Y(0<=X<N,0<=Y<N, indicating the position of the player's first touch.

输出

If the player touches the mine, just output "it is a beiju!".

If the player doesn't touch the mine, output the numbers appearing after the touch. If a position is touched by the player or by the computer automatically, output the number. If a position is not touched, output a dot '.'.

Output a blank line after each test case.

样例输入

5
OOOOO
OXXXO
OOOOO
OXXXO
OOOOO
1 1
5
OOOOO
OXXXO
OOOOO
OXXXO
OOOOO
0 0

样例输出

it is a beiju!

1....
.....
.....
.....
.....

题目来源

HDOJ

扫雷游戏~

#include <stdio.h>
#include <string.h>
#define MAXN 150
int N;
int flag[MAXN][MAXN];
char map[MAXN][MAXN];
char out[MAXN][MAXN];
int judge(int x, int y){
	int flag=0;
	for(int i=x-1; i<=x+1; i++){
		for(int j=y-1; j<=y+1; j++){
			if(1<=i && i<=N && 1<=j && j<=N && !(i==x&&j==y)){
				if(map[i][j]=='X')flag++;
			}
		}
	}
	return flag;
}
void dfs(int x, int y){
	int sum=judge(x,y);
	if(sum==0){
		out[x][y]='0';
		for(int i=x-1; i<=x+1; i++){
			for(int j=y-1; j<=y+1; j++){
				if(1<=i && i<=N && 1<=j && j<=N && !(i==x&&j==y)){
					if(!flag[i][j])
						dfs(i,j);
				}
				flag[i][j]=1;
			}
		}
	}else{
		out[x][y]=sum+'0';
	}
}
int main()
{
	while( scanf("%d",&N)!=EOF ){
		for(int i=1; i<=N; i++){
			getchar();
			for(int j=1; j<=N; j++){
				scanf("%c",&map[i][j]);
			}
		}
		int x,y;
		scanf("%d %d",&x,&y);
		x++;
		y++; 
		if(map[x][y]=='X'){
			puts("it is a beiju!
");
			continue;
		}
		memset(flag,0,sizeof(flag));
		memset(out,'.',sizeof(out));
		dfs(x,y);
		for(int i=1; i<=N; i++){
			for(int j=1; j<=N; j++){
				printf("%c",out[i][j]);
			}
			printf("
");
		}
		printf("
");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/chenjianxiang/p/3539689.html