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

HDU 2216 Game III(BFS)的更多相关文章

  1. hdu - 2216 Game III && xtu 1187 Double Maze (两个点的普通bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=2216 zjt和sara在同一个地图里,zjt要去寻找sara,zjt每移动一步sara就要往相反方向移动,如果他 ...

  2. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  3. hdu 2216 bfs

    题目大意:两个东西朝相同方向移动Sample Input4 4XXXX.Z...XS.XXXX4 4XXXX.Z...X.SXXXX4 4XXXX.ZX..XS.XXXXSample Output11 ...

  4. hdu 2102 A计划-bfs

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  5. HDU 1072(记忆化BFS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...

  6. HDU 2364 (记忆化BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往 ...

  7. HDU 2579 (记忆化BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:走迷宫.对于障碍点,只有当前(dep+1)%k才能走,问最少时间. 解题思路: 只有 ...

  8. HDU 2653 (记忆化BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...

  9. hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...

随机推荐

  1. Logstash安装搭建(一)

    Logstash是一个具有实时管道的开源数据收集引擎.可以动态地统一不同来源的数据,并将数据归到不同目的地.也是一个管理事件和日志工具.你可以用它来收集日志,分析它们,并将它们储存起来以供以后使用. ...

  2. DOM学习笔记--入门1

    HTML DOM 是关于如何获取.修改.添加或删除 HTML 元素的标准. 首先节点有很多种,不仅仅HTML元素是节点,尤其 要注意文本节点的存在. 根据 W3C 的 HTML DOM 标准,HTML ...

  3. [DP之普通系列]

    noip快要来了 要练练dp 难度也挺接近 还是挺好的 [Usaco2013 Nov]Pogo-Cow 这一道题要下一段大于这一段 所以的话我们就要记录每一段的状态 F[i,j]=F[j,k]+A[i ...

  4. 深入学习sea.js

    入门学习了文档之后,在深入学习里面的一些有趣的知识点 =================================== 一.配置 seajs.config({ alias:( a3:'./js/ ...

  5. 手工杀毒辅助软件(PC Hunter) V1.51 免费绿色版

    软件名称: 手工杀毒辅助软件(PC Hunter) 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 4.7MB 图片预览: 软件简介: PC Hunter是 ...

  6. BigDecimal-解决商业计算

    1.String to BigDecimal String amtStr = "1234.56"; BigDecimal amtBD = new BigDecimal(amtStr ...

  7. 三大框架之hibernate的反转

    选择工程.包名 Finish

  8. Java作用域

    1. java访问控制修饰符 Java中,可以使用访问控制符来保护对类.变量.方法和构造方法的访问.Java支持4种不同的访问权限. 默认的,也称为 default,在同一包内可见,不使用任何修饰符. ...

  9. JavaScript Date对象更进一步

    总结分享这个近期开发解决的一个Bug. Javascript的Date对象具有容错性,会自动根据当年的日期根据设置的属性值转换,也就是说Date对象的setDate会影响setMonth,month会 ...

  10. 用angular实现$.param()

    首先介绍一下$.param() 功能: 序列化对象或数组,返回字符串 eg: var params = { width:1900, height:1200 }; var str = jQuery.pa ...