HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085
出的很好的双向bfs,卡时间,普通的bfs会超时
题意方面:
1. 可停留
2. ghost无视墙壁
3. 需要检查两次某个地点是否有ghost,正要到达的时候(t),以及即将启程的时候(t+1).
在编程时需要注意的是:
当两个人汇合时,不需要检查即将启程的那次.
M可以走3步,在这3步中间只需要检查是否能到达(t)
出问题在: 1. 检查时间处理的不清晰
2.普通bfs会超时
3.双向bfs需要完全处理完某一时间,否则会出现
a.女孩已经走完了程序自动退出(que[0].empty()&&!que[1].empty())
b.因为女孩的步数比较多不是最优解的情况
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int inf =0x3fffffff;
const int maxn=1e3+3;
char maz[maxn][maxn];
int n,m;
struct pnt
{
int x,y;
pnt()
{
x=y=0;
}
pnt(int tx,int ty):x(tx),y(ty) {}
} z[2],b,g;
typedef pair<pnt,int> P;
int zn; bool ok(int x,int y,int time)
{
for(int i=0; i<2; i++)
{
int h=abs(x-z[i].x)+abs(y-z[i].y);
if(h<=2*time)
{
return false;
}
}
return true;
} int dg[maxn][maxn],db[maxn][maxn];
queue<pnt> que[2];
const int dx[4]= {1,-1,0,0};
const int dy[4]= {0,0,1,-1};
bool in(int x,int y)
{
return x>=0&&x<n&&y>=0&&y<m&&maz[x][y]!='X';
}
int tempt(int step,int s,int cnt)
{
int sz=que[s].size();
while(sz--)
{
pnt f=que[s].front();que[s].pop();
if(!ok(f.x,f.y,step))continue;
for(int di=0; di<4; di++)
{
pnt t=pnt(f.x+dx[di],f.y+dy[di]);
if(in(t.x,t.y)&&ok(t.x,t.y,step))
{
if(s)
{
if(db[t.x][t.y]>step)
{
db[t.x][t.y]=step;
if(dg[t.x][t.y]!=inf)
return step;
que[s].push(t);
}
}
else
{
if(dg[t.x][t.y]>step)
{
dg[t.x][t.y]=step;
if(db[t.x][t.y]!=inf)
return step;
que[s].push(t);
}
}
}
}
}
return -1;
} int bfs()
{
while(!que[0].empty())que[0].pop();
while(!que[1].empty())que[1].pop(); que[0].push(g);
que[1].push(b);
int step=0;
while(!que[0].empty()&&!que[1].empty())
{
step++;
int tmp;
if((tmp=tempt(step,0,0))!=-1)return tmp;
if((tmp=tempt(step,1,1))!=-1)return tmp;
if((tmp=tempt(step,1,2))!=-1)return tmp;
if((tmp=tempt(step,1,3))!=-1)return tmp;
}
return -1;
} void init()
{
zn=0;
for(int i=0; i<n; i++)fill(dg[i],dg[i]+m,inf);
for(int i=0; i<n; i++)fill(db[i],db[i]+m,inf);
}
int main()
{
int T;
scanf("%d",&T);
for(int ti=1; ti<=T; ti++)
{
scanf("%d%d",&n,&m);
init();
for(int i=0; i<n; i++)
{
scanf("%s",maz[i]);
for(int j=0; j<m; j++)
{
if(maz[i][j]=='Z')
{
z[zn++]=pnt(i,j);
}
else if(maz[i][j]=='G')
{
g=pnt(i,j);
dg[g.x][g.y]=0;
}
else if(maz[i][j]=='M')
{
b=pnt(i,j);
db[b.x][b.y]=0;
}
}
}
int ans=bfs();
printf("%d\n",ans);
}
return 0;
}
HDU 3085 Nightmare II 双向bfs 难度:2的更多相关文章
- HDU 3085 Nightmare Ⅱ(双向BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...
- HDOJ3085 Nightmare II 双向BFS
重构一遍就A了...但这样效率太低了...莫非都要重构???QWQ 每一秒男同志bfs3层,女同志bfs1层.注意扩展状态时,要判一下合不合法再扩展,而不是只判扩展的状态合不合法,否则有可能由非法的走 ...
- HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)
HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU - 3085 Nightmare Ⅱ
HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
- Nightmare Ⅱ(双向BFS)
Problem Description Last night, little erriyue had a horrible nightmare. He dreamed that he and his ...
- HDU 6171 Admiral(双向BFS+队列)题解
思路: 最大步骤有20,直接BFS会超时. 因为知道开始情况和结果所以可以用双向BFS,每个BFS规定最大步骤为10,这样相加肯定小于20.这里要保存每个状态搜索到的最小步骤,用Hash储存.当发现现 ...
随机推荐
- mysql 锁的粒度
1.锁的类型分为读锁和写锁,这个很好区分.可以这样认为:如果有增删改,就是写锁.如果是查询,就是读锁.2.锁的粒度也就是锁的范围,分为行锁和表锁.锁的范围和多个因素有关,包括事务隔离级别.是否使用索引 ...
- Android 代码混淆 防止反编译
为了防止代码被反编译,因此需要加入混淆.混淆也可以通过第三方进行apk混淆,也可以用android中的proguard进行混淆. 混淆步骤: 1.配置混淆文件,名字可以随意,在这里使用proguard ...
- JSP中RequestDispatcher的用法
RequestDispatcher是一个Web资源的包装器,可以用来把当前request传递到该资源,或者把新的资源包括到当前响应中.RequestDispatcher接口中定义了两个方法:inclu ...
- Http报头Accept与Content-Type的区别
Http报头Accept与Content-Type的区别 1.Accept属于请求头, Content-Type属于实体头. Http报头分为通用报头,请求报头,响应报头和实体报头. 请求方的http ...
- 测试分页查询出数据并分文件导出[java工程]
package cn.shiyanjun.test; import java.util.ArrayList; import java.util.List; public class ExcelTest ...
- michael的沟通秘籍
1 多准备几个邮件模板2 lower expectation over delivery less promise extra give3 involve resource earliar 尽早接手公 ...
- SyncServer obj
- signalR的一些细节
获取根目录通过AppDomain.CurrentDomain.BaseDirectory 因为不能直接获取session ,使用的替代方案如下 private static Dictionary< ...
- centos7配置mono和jexus5.6.2
一.通过集成包安装mono: 1.添加Mono的 包库源: 把Mono Project public Jenkins GPG signing 导入系统 wget http://jenkins.mon ...
- 189. Rotate Array -- 将数组前一半移到后一半
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...