Game III

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 31   Accepted Submission(s) : 11

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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!
#include <iostream>
#include <cstdio>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
struct node
{
int x,y,u,v,ti;
node(int a,int b,int c,int d,int e){x=a;y=b;u=c;v=d;ti=e;}
};
int n,m,i,j,sx,sy,tx,ty,ans;
char mp[][];
int vis[][][][]; //标记走过否
int dr[][]={{,},{,-},{-,},{,} }; bool check(int x,int y)
{
if (x>= && x<n && y>= && y<m && mp[x][y]!='X') return ;
return ;
}
void bfs()
{
queue<node>Q;
vis[sx][sy][tx][ty]=;
Q.push(node(sx,sy,tx,ty,));
while(!Q.empty())
{
node p=Q.front();
Q.pop();
if (abs(p.x-p.u)+abs(p.y-p.v)<=) { ans=p.ti; return; }
for(i=;i<;i++)
{
int xx=p.x+dr[i][];
int yy=p.y+dr[i][];
int uu=p.u-dr[i][];
int vv=p.v-dr[i][];
if (check(xx,yy))
{
if (!check(uu,vv))
{
uu=p.u;
vv=p.v;
}
if (!vis[xx][yy][uu][vv])
{
vis[xx][yy][uu][vv]=;
Q.push(node(xx,yy,uu,vv,p.ti+));
}
}
}
}
return;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(i=;i<n;i++)
{
scanf("%s",&mp[i]);
for(j=;j<m;j++)
{
if (mp[i][j]=='Z') sx=i,sy=j;
if (mp[i][j]=='S') tx=i,ty=j;
}
}
ans=-;
memset(vis,,sizeof(vis));
bfs();
if (ans==-) printf("Bad Luck!\n");
else printf("%d\n",ans);
}
return ;
}

HDU2216:Game III(BFS)的更多相关文章

  1. HDU 2216 Game III(BFS)

    Game III Problem Description Zjt and Sara will take part in a game, named Game III. Zjt and Sara wil ...

  2. UVA 1672不相交的正规表达式

    题意 输入两个正规表达式,判断两者是否相交(即存在一个串同时满足两个正规表达式).本题的正规表达式包含如下几种情况: 单个小写字符 $c$ 或:($P | Q$). 如果字符串 $s$ 满足 $P$ ...

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

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

  4. 337. House Robber III——树的题目几乎都是BFS、DFS,要么递归要么循环

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  5. 模板 树链剖分BFS版本

    //点和线段树都从1开始 //边使用vector vector<int> G[maxn]; ],num[maxn],iii[maxn],b[maxn],a[maxn],top[maxn], ...

  6. zoj 1649 Rescue (BFS)(转载)

    又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...

  7. [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III

    Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...

  8. HDU 3277 Marriage Match III(二分+最大流)

    HDU 3277 Marriage Match III 题目链接 题意:n个女孩n个男孩,每一个女孩能够和一些男孩配对,此外还能够和k个随意的男孩配对.然后有些女孩是朋友,满足这个朋友圈里面的人.假设 ...

  9. 2016级算法期末上机-H.难题·AlvinZH's Fight with DDLs III

    1119 AlvinZH's Fight with DDLs III 思路 难题,最小点覆盖. 分析题意,某一个任务,既可以在笔记本A的 \(a\) 模式下完成,也可以在笔记本B的 \(b\) 模式下 ...

随机推荐

  1. LeetCode OJ 78. Subsets

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  2. Java 内部类和匿名内部类

    1.内部类: /* 局部内部类: 在一个类 的方法内部定义另外一个类,那么另外一个类就称作为局部内部类. 局部内部类要注意的细节: 1. 如果局部 内部类访问了一个局部变量,那么该局部变量必须使用fi ...

  3. 插件管理工具 Alcatraz

    Alcatraz 安装: https://github.com/alcatraz/Alcatraz Github官网链接 终端安装方法 mkdir -p ~/Library/Application\ ...

  4. html各元素中的区别

    HTML中DIV与SPAN的区别 html的div和span, 经常会用到, 尤其是前者. 1. div是块级元素, 实际上就是一个区域, 主要用于容纳其他标签. 默认的display属性是block ...

  5. 嵌套json的查询

    postgres=# SELECT  t.data->'objects'->1->'src' AS ctFROM   reports as t     , json_array_el ...

  6. File文件操作类

    public class FileTest { //遍历出E:根目录下所有的文件夹,并输出文件夹名 static void testOne(){  //构建File对象,设置文件路径  File ro ...

  7. 【第三篇】学习 android 事件总线androidEventbus之发布事件,子线程中接收

    发送和接收消息的方式类似其他的发送和接收消息的事件总线一样,不同的点或者应该注意的地方: 1,比如在子线程构造方法里面进行实现总线的注册操作: 2,要想子线程中接收消息的功能执行,必须启动线程. 3, ...

  8. ZOJ 1119 SPF

    Tarjan算法求解割点 #include<cstdio> #include<cstring> #include<cmath> #include<vector ...

  9. C# var 隐式类型 var 用法 特点

    var 关键字是C# 3.0 开始,在方法范围中声明的变量: var有以下特点: * 1.var在编译器编译的时候根据初始值推断出其的类型          * 2.不能赋值除了初始值类型之外的其他类 ...

  10. DDE复盘流程

    开始复盘: 1 导入前面数据 重新复盘: 1.打开行情管理器 2.关闭图表 3.删除tick和1分钟图 4.关闭行情管理器 5.开启.