HDU 2216 Game III(BFS)
Game III
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.
#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)的更多相关文章
- hdu - 2216 Game III && xtu 1187 Double Maze (两个点的普通bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=2216 zjt和sara在同一个地图里,zjt要去寻找sara,zjt每移动一步sara就要往相反方向移动,如果他 ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- hdu 2216 bfs
题目大意:两个东西朝相同方向移动Sample Input4 4XXXX.Z...XS.XXXX4 4XXXX.Z...X.SXXXX4 4XXXX.ZX..XS.XXXXSample Output11 ...
- hdu 2102 A计划-bfs
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- HDU 1072(记忆化BFS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...
- HDU 2364 (记忆化BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往 ...
- HDU 2579 (记忆化BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:走迷宫.对于障碍点,只有当前(dep+1)%k才能走,问最少时间. 解题思路: 只有 ...
- HDU 2653 (记忆化BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...
- hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...
随机推荐
- linux的环境变量设置
source/etc/profile是让/etc/profile文件修改后立即生效, 还有一种方法是:. /etc/profile 注意:.和/etc/profile有空格 linux中source命 ...
- 关于java集合排序
对于排序,java开发者并不陌生. 为避免以后遗忘,现在再次总结一下! 常见8大排序算法, 平时自己熟悉的只有几种种!冒泡,二分/折半.插入.快排等!现在一一讲解一下,这里只讲思想,暂时不做实现! 一 ...
- java 基本数据类型跟封装类型的差距
import java.util.*; class test1{ public static void main(String[] args){ long start1 = System.curren ...
- Vim插件之Command-T使用问题
最近在使用vim插件CommandT时出现问题其实就是vim没有支持ruby,不过google之后找到了解决方法,老外的态度还是很让人敬佩的,度娘搜索的结果太让人呕心了.. 贴下,以后再次遇到解决. ...
- 破译情报-NOIP2016提高组复赛模拟试题
[题目描述] 最近国安人员截获了一份 RB 国的秘密情报, 全文都是经过加密的,每个单 词都很长.破译人员想到先把单词化简一下,方法是把每个单词尽量取短些的前 缀,但所取的前缀不能是其他单词的前缀. ...
- <poj - 2139> Six Degrees of Cowvin Bacon 最短路径问题 the cow have been making movies
本题链接:http://poj.org/problem?id=2139 Description: The cows have been making movies lately, so the ...
- CentOS6.5部署L2TP over IPSec
一.环境介绍: 1.CentOS 6.5 (要求双网卡做软路由,如果只是做VPN可以单网卡) a.外网IP: b.内网IP: 2.Window 10 主机一台做为一台内网测试软路由使用: a.内网IP ...
- 效果网址http://sc.chinaz.com/tag_jiaoben/tupianlunbo.html
http://sc.chinaz.com/tag_jiaoben/tupianlunbo.html
- CSU 1811 Tree Intersection
莫队算法,$dfs$序. 题目要求计算将每一条边删除之后分成的两棵树的颜色的交集中元素个数. 例如删除$u->v$,我们只需知道以$v$为$root$的子树中有多少种不同的颜色(记为$qq$), ...
- angular js一探
下一代angular js. 概念:mvc:作为dataModel的$scope. 还必须导入angular的库. ng-app:告诉angular引擎从这里开始是他因该管理的内容.(引入之后,可以在 ...