HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步
分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是双向BFS
就一秒一秒走就行,男的一秒走3,女的一秒走1,然后走过的分别赋值男女标记,当走到对方的标记时就是答案了
然后有一个不能走的地方和两个鬼的位置曼哈顿距离搞一下就行,
注:然后涨了姿势,是队列可以直接赋值,q1=q2,以前都不知道
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=;
int T,n,m;
int dx[]= {,,,-};
int dy[]= {-,,,};
int mp[N][N];
char s[N][N];
struct Point
{
int x,y;
} z[];
queue<Point>q[];
int step;
bool check(int x,int y)
{
if(x<||x>n||y<||y>m)return ;
if(s[x][y]=='X')return ;
int l1=abs(x-z[].x)+abs(y-z[].y);
int l2=abs(x-z[].x)+abs(y-z[].y);
if(l1<=*step||l2<=*step)return ;
return ;
}
bool bfs(int pos,int num)
{
q[]=q[pos];
for(int i=; i<num; ++i)
{
while(!q[].empty())
{
Point a=q[].front();
q[].pop();
q[pos].pop();
if(!check(a.x,a.y))continue;
for(int j=; j<; ++j)
{
int x=a.x+dx[j];
int y=a.y+dy[j];
if(!check(x,y))continue;
if(mp[x][y]==pos)continue;
if(mp[x][y]==(pos^))return ;
mp[x][y]=pos;
q[pos].push(Point {x,y});
}
}
q[]=q[pos];
}
return ;
}
int solve()
{
int ans=-;
while(!q[].empty()||!q[].empty())
{
++step;
if(bfs(,)||bfs(,))
{
ans=step;
break;
}
}
return ans;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=; i<=n; ++i)
scanf("%s",s[i]+);
for(int i=; i<; ++i)
while(!q[i].empty())
q[i].pop();
step=;
int cnt=;
for(int i=; i<=n; ++i)
{
for(int j=; j<=m; ++j)
{
mp[i][j]=-;
if(s[i][j]=='M')q[].push(Point {i,j}),mp[i][j]=;
else if(s[i][j]=='G')q[].push(Point{i,j}),mp[i][j]=;
else if(s[i][j]=='Z')z[cnt].x=i,z[cnt].y=j,cnt++;
}
}
printf("%d\n",solve());
}
return ;
}
HDU 3085 Nightmare Ⅱ 双向BFS的更多相关文章
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Nightmare Ⅱ HDU - 3085 (双向bfs)
Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were tra ...
- HDU - 3085 Nightmare Ⅱ
HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...
- HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)
HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- HDU 3085 Nightmare Ⅱ(双向BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...
- HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...
- 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
随机推荐
- crontab定时任务中文乱码问题
手动执行都很正常的的脚本,添加到定时任务中日志文件全是乱码经过多方查证终于找到了原因! crontab启动的任务没有获取系统的环境变量,导致中文乱码解决办法: 在执行的脚步中添加编码方式或者添加对 ...
- Impala入门笔记
From:http://tech.uc.cn/?p=817 问题背景: 初步了解Impala的应用 重点测试Impala的查询速度是否真的如传说中的比Hive快3~30倍 写作目的: 了解Impala ...
- poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj 2553 The Bottom of a Graph(强连通分量+缩点)
题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K ...
- 一个简单的aJax——后台用servlet技术
示例:webDemo 一.客户端 <%-- Created by IntelliJ IDEA. User: Administrator Date: 15-12-2 Time: 上午5:41 To ...
- 在smarty模板中嵌入php代码
我个人并不太喜欢smarty的语法,写起来比较啰嗦易出现匹配出错,但是旧项目中有许多工程都是采用它作模板.最近需要在此上稍微加一些PHP的内容,但我不想在模板控制层去一个一个assign,而想在模板文 ...
- 其实,前面倒腾那么多,只是为了想玩SPRING BOOT
嘿嘿,,曲线达到.. 看来看来很多国内的速成,都不爽. 官方教程最体贴~~~:) http://docs.spring.io/spring-boot/docs/current/reference/ht ...
- Nginx 实现MySQL的负载均衡
Nginx属于七层架构,支持的是http协议,本身对tcp协议没有支持.所以不能代理mysql等实现负载均衡.但是lvs这个东西不熟悉,主要是公司的的负载均衡都是nginx所以决定研究一下nginx的 ...
- Android:通过Intent访问一个网页
Intent(意图)主要是解决Android应用的各项组件之间的通讯. 小实例 package com.example.testopen; import android.app.Activity; i ...
- border-radius的水平和竖直半径
通常我们设置border-radius都只区分四个角的, 如border-radius: 1em 2em. 其实每个角的border-radius都由两部分组成, 水平半径和竖直半径. 要设置水平和竖 ...