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.

input

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.

output

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.


sample input

.....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.
2
1 6
3 8

sample output

MINE - YOU LOSE
NO MINE - 1 SURROUNDING IT


题意:是一个搜索问题,给定地图大小,X表示地雷,然后有n个点,表示点击此点的操作,有三种输出结果。

分析:典型的深搜算法

#include
#include
#include
#include
#include
using namespace std;
char map[20][31],map1[20][31];
int s,mark[20][31];//mark数组,0为未探测过 
void dfs(int a,int b)
{
	int flag=0;
	if(map1[a][b]=='X'||a<1||a>16|b<1||b>30||map1[a][b]=='#')
	return ;
	else
	{
		int flag1=1;
		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')
				{
					map1[a][b]='#';
					flag1=0;
				}		
			}
		}
		if(flag1==1)
		{
			s++;//全部安全,周围没有地雷 
			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;
					dfs(a+dr,b+dc);
				}	
			}
		}
	}
}
int main()
{
	int t,a,b;
	for(int i=1;i<=16;i++)
	{
		scanf("%s",map[i]+1);
	}
	scanf("%d",&t);
	while(t--)
	{
		for(int i=1;i<=16;i++)
		{
			for(int j=1;j<=30;j++)
			{
				map1[i][j]=map[i][j];
			}
		}		
		s=0;
		memset(mark,0,sizeof(mark));
		scanf("%d %d",&a,&b);
		if(map1[a][b]=='X')
		printf("MINE - YOU LOSE\n");//第一种 
		else
		{
			int cnt=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')
					cnt++;	
				}
			}
			if(cnt!=0)
			printf("NO MINE - %d SURROUNDING IT\n",cnt);//第二种 
			else
			{
				mark[a][b]=1;
				dfs(a,b);
				for(int i=1;i<=16;i++)
				{
					for(int j=1;j<=30;j++)
					{
						if(map1[i][j]=='#')
						s++;
					}
				}
				printf("NO MINE - %d SQUARES REVEALED\n",s);//第三种 
			}
		}
	}
	return 0;
} 


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