南邮 OJ 1149 Mine sweeping

Mine sweeping

时间限制(普通/Java) :  1000 MS/ 3000 MS          运行内存限制 : 32768 KByte
总提交 : 77            测试通过 : 24 

比赛描述

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....
.....
.....
.....
.....

题目来源

HDU OJ




#include<iostream>
bool mine[100][100];		//地图上的这一点是否有地雷
char show[100][100];		//将要输出的数组
int N,i,j;
bool is_mine(int i,int j){
	if(i>=0 && i<N &&j>=0 &&j<N && mine[i][j])
		return 1;
	else
		return 0;
}
void cal_show(int i,int j){
	if(i<0 || i>=N || j<0 || j>=N || show[i][j] || is_mine(i,j)){
		return;
	}
	if(is_mine(i-1,j-1)) ++show[i][j];
	if(is_mine(i-1,j)) ++show[i][j];
	if(is_mine(i-1,j+1)) ++show[i][j];
	if(is_mine(i,j-1)) ++show[i][j];
	if(is_mine(i,j+1)) ++show[i][j];
	if(is_mine(i+1,j-1)) ++show[i][j];
	if(is_mine(i+1,j)) ++show[i][j];
	if(is_mine(i+1,j+1)) ++show[i][j];
	show[i][j] += '0';
	if(show[i][j]=='0'){
		cal_show(i-1,j-1);
		cal_show(i-1,j);
		cal_show(i-1,j+1);
		cal_show(i,j-1);
		cal_show(i,j+1);
		cal_show(i+1,j-1);
		cal_show(i+1,j);
		cal_show(i+1,j+1);
	}
}
int main(){
	while(scanf("%d",&N)!=EOF){
		getchar();
		for(i=0;i<N;++i){
			for(j=0;j<N;++j){
				show[0][0] = getchar();
				if(show[0][0]=='O')
					mine[i][j] = 0;
				else
					mine[i][j] = 1;
			}
			getchar();
		}
		scanf("%d%d",&i,&j);
		if(mine[i][j]){
			printf("it is a beiju!\n\n");
		}else{
			memset(show,0,sizeof(show));
			cal_show(i,j);
			for(i=0;i<N;++i){
				for(j=0;j<N;++j){
					if(show[i][j])
						printf("%c",show[i][j]);
					else
						printf(".");
				}
				printf("\n");
			}
			printf("\n");
		}
	}
}



你可能感兴趣的:(ACM,mine,南邮OJ,sweeping)