描述:
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 tou-
ched 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 a number 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....
.....
.....
.....
.....
代码及注释见下:
#include<bits/stdc++.h>//万能头文件
using namespace std;
struct point//结构体封装便于操作
{
int x;
int y;
char dot;//用来储存原来的地图
char show;//用来储存点击后输出的地图
};
int n;
int tx,ty;
queue <point> qu;//BFS没有添加参数,所以变量最好定在全局,而且规模大的定全局程序不容易崩
point hidemap[101][101],pos;
int visit[101][101]={0};//存储该位置是否别访问过
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
bool Check(int x,int y)//判断点位是否越界
{
return x>=0 && x<n && y>=0 && y<n;
}
void BFS()//广度优先搜索
{
while(!qu.empty())
{
pos=qu.front();
visit[pos.x][pos.y]=1;//标记是否访问过
qu.pop();//读过就扔,渣男![\滑稽]
int minenum;//表示该点周围的雷数目
minenum=0;
for(int i=0;i<8;i++)
{
int nextx = pos.x + dir[i][0];//访问该位置的8向
int nexty = pos.y + dir[i][1];
if(Check(nextx,nexty) && (hidemap[nextx][nexty].dot=='X'))//判断是否为雷
{
minenum++;
}
}
if(minenum>0)
{
hidemap[pos.x][pos.y].show=minenum+'0';//如果该位置周围有雷则存入,并结束
}
else if(minenum==0)//如果没有雷
{
hidemap[pos.x][pos.y].show='0';
for(int i=0;i<8;i++)
{
int nextx = pos.x + dir[i][0];
int nexty = pos.y + dir[i][1];
if(Check(nextx,nexty) && hidemap[nextx][nexty].dot!='X' && visit[nextx][nexty]!=1)
{
qu.push(hidemap[nextx][nexty]);//如果该点没越界也不是雷且没被访问过则存入队列
visit[nextx][nexty]=1;//这一句很重要,没有它会导致重复的点被压入队列,会TLE
}
}
}
}
}
int main()
{
while(cin>>n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>hidemap[i][j].dot;
hidemap[i][j].x=i;//初始化
hidemap[i][j].y=j;
hidemap[i][j].show='.';//默认为.这样BFS没搜到的地方就不会被改变
}
}
cin>>tx>>ty;
if(hidemap[tx][ty].dot=='X')//若判的地方为雷直接输出
{
cout<<"it is a beiju!"<<endl;
}
else//不是则进行BFS
{
memset(visit,0,sizeof visit);
qu.push(hidemap[tx][ty]);//先把起点手动压入
BFS();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<hidemap[i][j].show;
}
cout<<endl;
}
}
cout<<endl;//额外的空行在这里,漏了会PE
}
return 0;
}