Description
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.
Note: the new ghosts also can devide as the original ghost.
Input
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.
Output
Sample Input
3
5 6
XXXXXX
XZ..ZX
XXXXXX
M.G...
......
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G...
10 10
..........
..X.......
..M.X...X.
X.........
.X..X.X.X.
.........X
..XX....X.
X....G...X
...ZX.X...
...Z..X..X
Sample Output
1
1
-1
题意:
看M和G能不能在不遇到鬼的情况下,找到对方.
M一次可以走三步 G只能走一步 鬼可以走两步
貌似用优先队列改了队列里面的顺序..不能用
改成队列就过了.
完全不知道为什么..不管了..
M的三步我用了一个leave去保存, 从0-2 到了2之后 就把该点的步数加一 放在队列里面
其余的 记得先比较跟鬼的距离,因为鬼是先走的,当前点如果和鬼的曼哈顿距离 小于2*step的话 , 就不能用该点 continue
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
const int N=810;
int n,m;
char Map[N][N];
bool mark[N][N][2],flag,used[N][N];
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
struct node
{
int x,y,step,leave;
}M,G,Z[2];
queue<node>q;
queue<node>p;
bool check(int x,int y,int num,int step)
{
if (x<0 || x>=n || y<0 || y>=m || mark[x][y][num] || Map[x][y]=='X' || abs(x-Z[0].x)+abs(y-Z[0].y)<=2*step || abs(x-Z[1].x)+abs(y-Z[1].y)<=2*step)
return 0;
return 1;
}
bool check(int x,int y,int step)
{
if (abs(x-Z[0].x)+abs(y-Z[0].y)<=2*step || abs(x-Z[1].x)+abs(y-Z[1].y)<=2*step)
return 0;
return 1;
}
int bfs()
{
flag=0;
while (!q.empty())
q.pop();
while (!p.empty())
p.pop();
q.push(M);
p.push(G);
int s=0;
while (!q.empty() || !p.empty())
{
node start,later;
while (!q.empty() && q.front().step<=s)
{
start=q.front();
q.pop();
if (!check(start.x,start.y,start.step+1))
continue;
for (int i=0 ; i<4 ; i++)
{
int nx=start.x+dir[i][0];
int ny=start.y+dir[i][1];
if (check(nx,ny,0,start.step+1))
{
if (mark[nx][ny][1])
return start.step+1;
mark[nx][ny][0]=1;
later.x=nx;
later.y=ny;
later.step=start.step;
if (start.leave==2)
{
later.leave=0;
later.step++;
}
else
later.leave=start.leave+1;
q.push(later);
}
}
}
while (!p.empty() && p.front().step<=s)
{
start=p.front();
p.pop();
if (!check(start.x,start.y,start.step+1))
continue;
for (int i=0 ; i<4 ; i++)
{
int nx=start.x+dir[i][0];
int ny=start.y+dir[i][1];
if (check(nx,ny,1,start.step+1))
{
if (mark[nx][ny][0])
return start.step+1;
mark[nx][ny][1]=1;
later.x=nx;
later.y=ny;
later.step=start.step+1;
later.leave=start.leave;
p.push(later);
}
}
}
s++;
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
memset(mark,0,sizeof(mark));
int z=0;
for (int i=0 ; i<n ; i++)
{
scanf("%s",Map[i]);
for (int j=0 ; j<m ; j++)
{
if (Map[i][j]=='M')
{
M.x=i;
M.y=j;
M.leave=0;
mark[i][j][0]=1;
M.step=0;
}
else if (Map[i][j]=='G')
{
G.x=i;
G.y=j;
G.leave=0;
mark[i][j][1]=1;
G.step=0;
}
else if (Map[i][j]=='Z')
{
Z[z].x=i;
Z[z].y=j;
Z[z++].step=0;
}
}
}
printf("%d\n",bfs());
}
return 0;
}
/*
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G...
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
.XG...
5 6
XXXXXX
XZZ..X
XXXXXX
MG....
.X....
5 6
XXXXXX
XZZ..X
XXXXXX
M.X...
XX.XG.
*/
578




被折叠的 条评论
为什么被折叠?



