Minesweeper(深搜)

描述:

Minesweeper is a game available with the Windows™ operating system. The object of Minesweeper is to locate all the mines as quickly as possible without uncovering any of them. You can uncover a square by clicking it. If you uncover a mine, you lose the game. If a number appears on a square, it indicates how many mines are in the eight squares that surround the numbered one.
Sometimes clicking on a square, that has no mines surrounding it, reveals many squares. It will reveal all of the squares surrounding it until it reaches squares that have mines surrounding it.

Your job is to write a program to determine the result of clicking on a square during a Minesweeper game.


輸入:

The first sixteen lines will contain thirty characters each. Each character will be either an ‘X’, that represents a square with a mine or a period (.) that will represent a square without a mine.

The next line is an integer n, and the next n lines will each contain two integers, r and c, that represent the row and column of the square that is clicked. Assume the top left square of the minefield to be at row one and column one.



輸出:

The output data will contain the results of clicking on the square. For sake of simplicity, assume that each of the clicks, from the input data, is the first click of a new game. If the square contains a mine, output “MINE - YOU LOSE”. If the square does not contain a mine, but there are some mines surrounding it, output “NO MINE - # SURROUNDING IT”, where # is the number of mines surrounding it. If the square does not contain a mine and there are no mines surrounding it, output “NO MINE - # SQUARES REVEALED”, where # is the number of squares revealed. Note that the characters in the output data are uppercase.




樣例輸入:

.....XXX.............X...X...X
..........XX...X......X.......
....X.X.......................
.............X...X......XXXX..
X....X.....X...X...X.......X..
X.X....X........X.X..X..X.X.X.
...X..X..........X.X...X..XX..
..X...X.......X.........X...X.
....XXXXX.................X...
.X.X.........X..........XX..X.
..XXXX...X.X............X.....
...XX...X.XX...X........X.X...
XXXX.X..XX....X...............
.........X.X...X...........X..
X.X.X.XX.....X..X.X...X..X..XX
..X.........X...............X.
3
1 6
3 8

9 20


樣例輸出:

MINE - YOU LOSE
NO MINE - 1 SURROUNDING IT

NO MINE - 72 SQUARES REVEALED





題目大意:

規定了掃雷的面積區域爲16*30,輸入地圖(‘X’表示雷)和一個正整數n,接下去n行每行兩個正整數(xi,yi)代表一個座標(注意第一行第一列表示爲座標1 1)。若(xi,yi)所在位置爲雷則輸出MINE - YOU LOSE,不是雷的話,但周圍還有一些地雷,則輸出“NO MINE - #SURROUNDING IT”,其中#是周圍的地雷。如果廣場不包含礦井,周圍沒有地雷,則輸出“NO MINE - #SQUARES REVEALED”,其中#是顯示的廣場數。




#include<stdio.h>
#include<string.h>
#define H 16
#define L 30
char map[20][35];
char copymap[20][35];
int safe;
int mark[20][35];
int asd(int a,int b)
{
	for(int dr=-1;dr<=1;dr++)
	{
		for(int dc=-1;dc<=1;dc++)
		{					
			if((dr!=0||dc!=0)&&copymap[a+dr][b+dc]=='X')				//如果周圍有雷該地區置爲‘#’方便之後計算外邊框 
			{
				copymap[a][b]='#';
				return 0;
			}		
		}
	}
	return 1;
}
void dfs(int a,int b)
{
	int flag=0;
	if(copymap[a][b]=='X'||a<1||a>H|b<1||b>L||copymap[a][b]=='#')         //邊界位置判斷和返回情況判斷 
	return ;
	else
	{
		if(asd(a,b)==1)													//判斷周圍是否有雷,用safe來記錄數量但這樣算出的結果不是第三種情況的最終結果,還需要加上外邊框 
		{
			safe++;
			flag=1;
		}
	}
	if(flag==1)
	{
	for(int dr=-1;dr<=1;dr++)
	{
		for(int dc=-1;dc<=1;dc++)
		{
			if((dr!=0||dc!=0)&&mark[a+dr][b+dc]==0)
			{
				mark[a+dr][b+dc]=1;									//標誌變量置爲1表示已經訪問過 
				dfs(a+dr,b+dc);
			}	
		}
	}
	}
}
void check(int a,int b)
{
	if(copymap[a][b]=='X')											//所在地爲雷的情況 
	printf("MINE - YOU LOSE\n");
	else
	{
		int count=0;
		for(int dr=-1;dr<=1;dr++)
		{
			for(int dc=-1;dc<=1;dc++)
			{
				if((dr!=0||dc!=0)&&copymap[a+dr][b+dc]=='X')
				count++;											//計數該位置附近的雷的數量 
			}
		}
		if(count!=0)												//所在地沒有雷但周圍有雷 
		printf("NO MINE - %d SURROUNDING IT\n",count);
		else														//其他情況 
		{
			mark[a][b]=1;
			dfs(a,b);												//深度搜索周圍地方 
			for(int i=1;i<=H;i++)									//加上描述樣例圖中右下角的邊框(及標誌1,2,3的位置) 
			{
				for(int j=1;j<=L;j++)
				{
					if(copymap[i][j]=='#')
					safe++;
				}
			}
			printf("NO MINE - %d SQUARES REVEALED\n",safe);
		}
	}
}
int main()
{
	int t,a,b;
	for(int i=1;i<=H;i++)
	{
		scanf("%s",map[i]+1);
	}
	scanf("%d",&t);
	while(t--)
	{
		for(int i=1;i<=H;i++)
		{
			for(int j=1;j<=L;j++)
			{
				copymap[i][j]=map[i][j];				//複製一個模板地圖信息防止多組數據使得原圖被破壞 
			}
		}		
		safe=0;
		memset(mark,0,sizeof(mark));					//定義一個標誌mark二維數組來判斷各個點是否被訪問 
		scanf("%d %d",&a,&b);
		check(a,b);										//判斷它是題意中三種情況的哪一種 
	}
	return 0;
} 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章