hdu 5040 Instrusive【BFS+优先队列】
11733274 | 2014-09-26 12:42:31 | Accepted | 5040 | 62MS | 1592K | 4848 B | G++ | czy |
先转一个优先队列的用法:
http://www.cppblog.com/shyli/archive/2007/04/06/21366.html
在优先队列中,优先级高的元素先出队列。 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。 优先队列的第一种用法,也是最常用的用法:

通过<操作符可知在整数中元素大的优先级高。 故示例1中输出结果为:9 6 5 3 2
第二种方法: 在示例1中,如果我们要把元素从小到大输出怎么办呢? 这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。

其中 第二个参数为容器类型。 第二个参数为比较函数。 故示例2中输出结果为:2 3 5 6 9
第三种方法: 自定义优先级。









在该结构中,value为值,priority为优先级。 通过自定义operator<操作符来比较元素中的优先级。 在示例3中输出结果为: 优先级 值 9 5 8 2 6 1 2 3 1 4 但如果结构定义如下:









则会编译不过(G++编译器) 因为标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。 而且自定义类型的<操作符与>操作符并无直接联系,故会编译不过。
//代码清单


























































Instrusive
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 138 Accepted Submission(s): 34
The military base can be seen as an N * N grid. Matt's target is in one of the grids and Matt is now in another grid.
In normal case, Matt can move from a grid to one of the four neighbor grids in a second. But this mission is not easy.
Around the military base there are fences, Matt can't get out of the base.
There are some grids filled with obstacles and Matt can't move into these grids.
There are also some surveillance cameras in the grids. Every camera is facing one of the four direction at first, but for every second, they will rotate 90 degree clockwisely. Every camera's sight range is 2, which means that if Matt is in the same grid as the camera, or in the grid that the camera is facing, he will be seen immediately and the mission will fail.
Matt has a special equipment to sneak: a cardbox. Matt can hide himself in the card box and move without being noticed. But In this situation, Matt will have to use 3 seconds to move 1 grid. Matt can also just hide in the cardbox without moving. The time to hide and the time to get out of the cardbox can be ignored.
Matt can't take the risk of being noticed, so he can't move without cardbox into a grid which is now insight of cameras or from a grid which is now insight of cameras. What's more, Matt may be in the cardbox at the beginning.
As a live legend, Matt wants to complete the mission in the shortest time.
For each test cases, the first line contains one integer:N(1<=N<=500)
In the following N lines, each line contains N characters, indicating the grids.
There will be the following characters:
● '.' for empty ● '#' for obstacle ● 'N' for camera facing north ● 'W' for camera facing west ● 'S' for camera facing south ● 'E' for camera facing east ● 'T' for target ● 'M' for Matt
If Matt cannot complete the mission, output '-1'.
3
M..
.N.
..T
3
M..
###
..T
Case #2: -1
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<string> #define N 505
#define M 10000002
#define mod 10000007
//#define p 10000007
#define mod2 100000000
#define inf 1000000000
//#define ll long long
//#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n;
int mi[N][N];
char s[N][N];
int dirx[]={-,,,};
int diry[]={,,,-}; struct PP
{
friend bool operator< (PP n1, PP n2)
{
return n1.time > n2.time;
}
int x;
int y;
int time;
}; PP start,end; void ini()
{
int i,j;
scanf("%d",&n);
//memset(s,-1,sizeof(s)); for(i=;i<=n+;i++){
for(j=;j<=n+;j++){
s[i][j]='#';
}
} for(i=;i<=n;i++){
scanf("%s",s[i]+);
}
for(i=;i<=n;i++){
for(j=;j<=n;j++){
mi[i][j]=inf;
if(s[i][j]=='M'){
start.x=i;
start.y=j;
mi[i][j]=;
}
else if(s[i][j]=='T'){
end.x=i;
end.y=j;
} else if(s[i][j]=='N'){
s[i][j]=;
} else if(s[i][j]=='W' ){
s[i][j]=;
} else if(s[i][j]=='S' ){
s[i][j]=;
} else if(s[i][j]=='E' ){
s[i][j]=;
}
}
}
} int ok1(int i,int j,int time)
{
if(i>= && i<=n && j>= && j<=n && s[i][j]!='#' && time<mi[i][j])
// if(s[i][j]!='#' && time<mi[i][j])
return ;
else
return ;
} int ok2(int i,int j,int time)
{
int ni,nj;
int o;
//if(s[i][j]=='N' || s[i][j]=='W' || s[i][j]=='S' || s[i][j]=='E' )
// return 0;
for(o=;o<;o++){
ni=i+dirx[o];
nj=j+diry[o];
if(ni>= && ni<=n && nj>= && nj<=n){
if(s[ni][nj]>= && s[ni][nj]<=){
if((time%)==(o+s[ni][nj])% ){
return ;
}
}
/*
if(s[ni][nj]=='N' && (time%4)==(o+2)%4 ){
return 0;
} if(s[ni][nj]=='W' && (time%4)==(o+3)%4 ){
return 0;
} if(s[ni][nj]=='S' && (time%4)==(o)%4 ){
return 0;
} if(s[ni][nj]=='E' && (time%4)==(o+1)%4 ){
return 0;
}
*/
}
}
return ;
} void bfs()
{
int i,j;
start.time=;
// queue<PP> q;
priority_queue<PP> q;
PP now,nx;
q.push(start);
while(q.size()>=)
{
now=q.top();
if(now.x==end.x && now.y==end.y) break;
q.pop();
nx=now;
nx.time++;
for(i=;i<;i++){
nx.x=now.x+dirx[i];
nx.y=now.y+diry[i];
if(ok1(nx.x,nx.y,nx.time)==) continue; if(nx.time+<mi[nx.x][nx.y]){
nx.time+=;
mi[nx.x][nx.y]=nx.time;
q.push(nx);
nx.time-=;
} if(s[now.x][now.y]>= && s[now.x][now.y]<=){
continue;
}
if(s[nx.x][nx.y]>= && s[nx.x][nx.y]<=){
continue;
}
// if(s[now.x][now.y]=='N' || s[now.x][now.y]=='W' || s[now.x][now.y]=='S' || s[now.x][now.y]=='E' )
// continue;
// if(s[nx.x][nx.y]=='N' || s[nx.x][nx.y]=='W' || s[nx.x][nx.y]=='S' || s[nx.x][nx.y]=='E' )
// continue;
// if(s[now.x][now.y]>3 && s[nx.x][nx.y]>3 ){
for(j=;j<=;j++){
if(nx.time+j>=mi[nx.x][nx.y]) continue;
if(ok2(now.x,now.y,now.time+j)== && ok2(nx.x,nx.y,now.time+j)==) {
nx.time+=j;
mi[nx.x][nx.y]=nx.time;
q.push(nx);
nx.time-=j;
break;
}
}
// } }
}
} void out()
{
if(mi[end.x][end.y]==inf){
printf("-1\n");
}
else{
printf("%d\n",mi[end.x][end.y]);
}
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
// while(T--)
// while(scanf("%d%d",&n,&m)!=EOF)
{
// if(n==0 && m==0) break;
printf("Case #%d: ",cnt);
ini();
bfs();
out();
} return ;
}
hdu 5040 Instrusive【BFS+优先队列】的更多相关文章
- HDU 5040 Instrusive(BFS+优先队列)
题意比较啰嗦. 就是搜索加上一些特殊的条件,比如可以在原地不动,也就是在原地呆一秒,如果有监控也可以花3秒的时间走过去. 这种类型的题目还是比较常见的.以下代码b[i][j][x]表示格子i行j列在x ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...
- hdu 5040 Instrusive
Instrusive Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tota ...
- HDU——1242Rescue(BFS+优先队列求点图最短路)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 2014年北京网络赛 Instrusive HDU 5040 题解 优先队列
网赛的时候看了这道题,发现就是平常的那种基础搜索题. 由于加了一个特殊条件:可以一次消耗3秒或原地停留1秒. 那就不能使用简单的队列了,需要使用优先队列才行. 题意 告诉一副地图:一个起点,一个终点, ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
随机推荐
- Android(java)学习笔记142:Android中补间动画(Tween Animation)
本文主要简单介绍补间动画使用代码实现, 关于使用xml实现补间动画, 可以参看:自定义控件三部曲之动画篇(一)——alpha.scale.translate.rotate.set的xml属性及用法 1 ...
- spring框架的总结
http://www.cnblogs.com/wangzn/p/6138062.html 大家好,相信Java高级工程师对spring框架都很了解吧!那么我以个人的观点总结一下spring,希望大家有 ...
- C#导入有道词典单词本到扇贝
由于扇贝查词没有有道方便,所以很多时候添加生词都是在使用有道词典,然后顺手就保存到了有道单词本,不过在扇贝记单词可以打卡,记单词更方便,进入扇贝页面后发现扇贝单词批量导入居然一次只支持10个,查了扇贝 ...
- JavaScript中数据类型和typeof返回的数据类型
除了上图,要注意三点:1.symbol是ES6中新增的数据类型 2.typeof(null)结果是Object 3.typeof(Object)和typeof(Array)的结果是function,因 ...
- Delphi 中内存映射对于大文件的使用
这篇文章主要介绍了Delphi 中内存映射对于大文件的使用的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下 Delphi 中内存映射对于大文件的使用 平时很少使用大文件的内存映射,碰巧遇到了 ...
- CVE-2010-3333
环境 windows xp sp3 office 2003 sp0 windbg ollydbg vmware 12.0 0x00 RTF格式 RTF是Rich TextFormat的缩写,意即富文本 ...
- java在线聊天项目0.8版 实现把服务端接收到的信息返回给每一个客户端窗口中显示功能
迭代器的方式会产生锁定 服务器端增加发送给每个客户端已收到信息的功能 所以当获取到一个socket,并打开它的线程进行循环接收客户端发来信息时,我们把这个内部类的线程Client保存到集合List&l ...
- ios之UITabelViewCell的自定义(代码实现)
在用到UITableVIew的时候,经常会自定义每行的Cell 在IOS控件UITableView详解中的下面代码修改部分代码就可以实现自定义的Cell了 [cpp] view plaincopy - ...
- Emmet:HTML/CSS代码快速编写神器--20150422
Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...
- CSS在线压缩
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...