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率感 ...
随机推荐
- select a.no,a.name,b.subid,b.subname,c.score
select a.no,a.name,b.subid,b.subname,c.score from a,b,c where a.no = c.no and b.subid = c.subid ;
- html备忘录
上传文件 <form action="/ajax/" method="post" enctype="multipart/form-data&qu ...
- POI创建生成excel及设置相关属性
简单的读写到excel中: import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io. ...
- 在Phonegap下实现oAuth认证
原文:http://www.kuqin.com/mobile/20120719/322873.html 前段时间做过两次关于Phonegap的现场交流会议分享.基本上把Phonegap的一些特性和大家 ...
- vue-music:歌词的其他功能
由于歌词的播放需要歌曲播放,切换歌曲,歌曲的播放模式等等有关联,因此,需要在这几处处理相关问题 1.循环播放回不到开始位置 loop() { this.$refs.audio.currentTime ...
- HTML 文件类表单元素如何限制上传类型,Accept属性设置
需求描述:简单的控制file的选择类型 解决方法:使用HTML input file 的accept属性控制 实例: <form action="demo_form.asp" ...
- POJ:2406-Power Strings(寻找字符串循环节)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Description Given two strings a and b we defin ...
- 检查DLL,EXE文件是64位或者32位的方法
检查DLL,EXE文件是64位或者32位:输入corflags <assembly path>:
- loj2145 「SHOI2017」分手是祝愿
记 \(f_i\) 是从要做 \(i\) 步好操作变成要做 \(i-1\) 步好操作的期望操作次数. 显然 \(f_i=i/n \times 1 + (1-i/n) \times (1 + f_{i+ ...
- zoj 2104 Let the Balloon Rise
Let the Balloon Rise Time Limit: 2 Seconds Memory Limit: 65536 KB Contest time again! How excit ...