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.

>  . : 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 ;
}

最新文章

  1. 闪回恢复区大小不够。报ORA-19809、ORA-19804
  2. VS2012 easyui datagrid url访问之坑
  3. loadrunner统计字符串中指定字符出现的次数
  4. Unity教程之再谈Unity中的优化技术
  5. Poj(3687),拓扑排序,
  6. Python 类型的分类
  7. EntityFramework在root目录web.config中的配置设置
  8. 从jsp页面到servlet传值的不同方式
  9. Python多线程练习(threading)
  10. Python学习之路——迭代器
  11. ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry ’1′ for key ‘PRIMARY’
  12. 百度富文本ueditor使用小结
  13. SQL Server 经典案例
  14. dubbo负载均衡策略和集群容错策略都有哪些
  15. 第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据
  16. Mysql Lock wait timeout exceeded; try restarting transaction的问题
  17. P2306 被yyh虐的mzc
  18. Linux写时拷贝技术(copy-on-write)
  19. php uncode 转汉字编码
  20. VC++中CEdit控件实现回车换行

热门文章

  1. Ubuntu 16.04 samba相关配置
  2. WindowsServer2012 R2 64位中文标准版(IIS8.5)下手动搭建PHP环境详细图文教程(二)安装IIS8.5
  3. java静态方法之线程安全问题
  4. sharedMesh变量
  5. 【Python】functools.wraps定义函数装饰器
  6. Python学习笔记——进阶篇【第八周】———进程、线程、协程篇(Socket编程进阶&amp;多线程、多进程)
  7. 一个数组分四个ul并且每个ul里边有四个li显示
  8. 关于scanf()函数的一点理解
  9. HDU 5839 Special Tetrahedron
  10. Java中泛型的理解