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. Python基础知识学习_Day7

    一.Subprocess模块 1常用方法 执行命令,返回命令执行状态,0 or非0 >>> retcode = subprocess.call(["ls", &q ...

  2. Beyond Compare V3.2.3 Beta 中文版

    软件名称: Beyond Compare V3.2.3 Beta 中文版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win7 / Vista / Win2003 / WinXP 软件大小 ...

  3. java.lang.IllegalArgumentException: View not attached to window manager

    公司项目线上bug: java.lang.IllegalArgumentException: View not attached to window manager at android.view.W ...

  4. 某网站看到的某神的Symfony_使用心得

    1.symfony2这种量级的框架怎么可能有捷径可走?其定位是松藕合和易扩展,并不是很容易的事.sf2是靠configuration系统(它可以把各种语义化yml配置转为数组)和服务容器概念来实现的b ...

  5. hdu 2087 剪花布条 kmp模板题

    也是kuangbin专题的 专题名字太长 不复制了…… 刚好数据结构也学了kmp 找一道题敲敲模板…… 暴力的字符串匹配是O(n*m)的时间复杂度 而kmp通过一个O(m)的预处理将字符串匹配的时间复 ...

  6. ios 显示其他app的购买页面

    using UnityEngine; using System.Collections; using System.Runtime.InteropServices ; public class IOS ...

  7. svn checkout操作

    svn checkout https://svn.com/svn/project 该操作从svn服务器上拉代码下来,并且建立本地和远端的文件对应,状态的关联. 1,和export的区别 svn检出操作 ...

  8. TheSeventhWeekJavaText

    如何用二维数组绘制五子棋盘 在定义一个棋盘类,声明一个二维字符数组,为每一个数组元素赋值为"+",打印输出就是一个简单地棋盘,如下图: 然后定义BufferedReader的实例对 ...

  9. Learning from the CakePHP source code - Part II

    原文:http://debuggable.com/posts/learning-from-the-cakephp-source-code-part-ii:480f4dd6-57fc-4715-8709 ...

  10. SPA UI-router

    ------------------------------------------------------------------------------------ SPA SPA(单页面应用): ...