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
#include
#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)&©map[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)&©map[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;
} 


你可能感兴趣的:(Minesweeper(深搜))