Game III
Problem Description
Zjt and Sara will take part in a game, named Game III. Zjt and Sara will be in a maze, and Zjt must find Sara. There are some strang rules in this maze. If Zjt move a step, Sara will move a step in opposite direction.
Now give you the map , you shold find out the minimum steps, Zjt have to move. We say Zjt meet Sara, if they are in the same position or they are adjacent .
Zjt can only move to a empty position int four diraction (up, left, right, down). At the same time, Sara will move to a position in opposite direction, if there is empty. Otherwise , she will not move to any position.
The map is a N*M two-dimensional array. The position Zjt stays now is marked Z, and the position, where Sara stays, is marked E.
Now give you the map , you shold find out the minimum steps, Zjt have to move. We say Zjt meet Sara, if they are in the same position or they are adjacent .
Zjt can only move to a empty position int four diraction (up, left, right, down). At the same time, Sara will move to a position in opposite direction, if there is empty. Otherwise , she will not move to any position.
The map is a N*M two-dimensional array. The position Zjt stays now is marked Z, and the position, where Sara stays, is marked E.
> . : empty position
> X: the wall
> Z: the position Zjt now stay
> S: the position Sara now stay
Your task is to find out the minimum steps they meet each other.
Input
The input contains several test cases. Each test case starts with a line contains three number N ,M (2<= N <= 20, 2 <= M <= 20 ) indicate the size of the map. Then N lines follows, each line contains M character. A Z and a S will be in the map as the discription above.
Output
For each test case, you should print the minimum steps. “Bad Luck!” will be print, if they can't meet each other.
Sample Input
4 4
XXXX
.Z..
.XS.
XXXX
4 4
XXXX
.Z..
.X.S
XXXX
4 4
XXXX
.ZX.
.XS.
XXXX
Sample Output
1
1
Bad Luck!
Answer
题解:这个BFS很有意思,跟典型的题目不同,它的目标点在动。当Z在移动的时候,S会往相反方向移动(如果能动)。就是这点,导致WA了两次,vis数组只开了二维来记录Z有没有走过,然后看了题解是要开四维数组,保存Z、S。然后第三组样例又一只跑不出来,发现要把判断是否访问语句放到判断S是否移动的后面才行。
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//#define LOCAL
int n,m;
char mp[][];
bool vis[][][][];
int dir[][]={{,},{-,},{,-},{,}};
struct Node
{
int zx,zy;
int sx,sy;
int step;
}t,nn;
int bfs()
{
queue<Node> q;
while(!q.empty())q.pop();
vis[t.zx][t.zy][t.sx][t.sy]=;
t.step=;
q.push(t);
while(!q.empty())
{
t=q.front(),q.pop();
if(t.sx==t.zx&&abs(t.sy-t.zy)==)return t.step;
if(t.sy==t.zy&&abs(t.sx-t.zx)==)return t.step;
if(t.sx==t.zx&&t.sy==t.zy)return t.step;
for(int i=;i<;i++)
{
nn.zx=t.zx+dir[i][];
nn.zy=t.zy+dir[i][];
if(mp[nn.zx][nn.zy]=='X')continue;
if(nn.zx<||nn.zx>=n||nn.zy<||nn.zy>=m)continue;
nn.sx=t.sx-dir[i][];
nn.sy=t.sy-dir[i][];
if(nn.sx<||nn.sx>=n||nn.sy<||nn.sy>=m)nn.sx=t.sx,nn.sy=t.sy;
if(mp[nn.sx][nn.sy]=='X')nn.sx=t.sx,nn.sy=t.sy;
nn.step=t.step+;
if(vis[nn.zx][nn.zy][nn.sx][nn.sy])continue;
vis[nn.zx][nn.zy][nn.sx][nn.sy]=;
q.push(nn);
}
}
return -;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
while(cin>>n>>m)
{
msv;
for(int i=;i<n;i++)cin>>mp[i];
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(mp[i][j]=='Z')t.zx=i,t.zy=j;
else if(mp[i][j]=='S')t.sx=i,t.sy=j;
}
int ans=bfs();
if(ans==-)printf("Bad Luck!\n");
else printf("%d\n",ans);
}
return ;
}