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

优先队列用法

在优先队列中,优先级高的元素先出队列。 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。 优先队列的第一种用法,也是最常用的用法:

priority_queue<int> qi;

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

priority_queue<int, vector<int>, greater<int> >qi2;

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

struct node {     friend bool operator< (node n1, node n2)     {         return n1.priority < n2.priority;     }     int priority;     int value; };

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

struct node {     friend bool operator> (node n1, node n2)     {         return n1.priority > n2.priority;     }     int priority;     int value; };

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

#include<iostream> #include<functional> #include<queue> using Namespace stdnamespace std; struct node {     friend bool operator< (node n1, node n2)     {         return n1.priority < n2.priority;     }     int priority;     int value; }; int main() {     const int len = 5;     int i;     int a[len] = {3,5,9,6,2};     //示例1     priority_queue<int> qi;     for(i = 0; i < len; i++)         qi.push(a[i]);     for(i = 0; i < len; i++)     {         cout<<qi.top()<<" ";         qi.pop();     }     cout<<endl;     //示例2     priority_queue<int, vector<int>, greater<int> >qi2;     for(i = 0; i < len; i++)         qi2.push(a[i]);     for(i = 0; i < len; i++)     {         cout<<qi2.top()<<" ";         qi2.pop();     }     cout<<endl;     //示例3     priority_queue<node> qn;     node b[len];     b[0].priority = 6; b[0].value = 1;      b[1].priority = 9; b[1].value = 5;      b[2].priority = 2; b[2].value = 3;      b[3].priority = 8; b[3].value = 2;      b[4].priority = 1; b[4].value = 4;      for(i = 0; i < len; i++)         qn.push(b[i]);     cout<<"优先级"<<'\t'<<"值"<<endl;     for(i = 0; i < len; i++)     {         cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;         qn.pop();     }     return 0; }

Instrusive

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 138 Accepted Submission(s): 34

Problem Description
The legendary mercenary Solid Matt gets a classic mission: infiltrate a military base.
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.
 
Input
The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.
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
 
Output
For each test case, output one line "Case #x: y", where x is the case number (starting from 1) and y is the answer.
If Matt cannot complete the mission, output '-1'.
 
Sample Input
2
3
M..
.N.
..T
3
M..
###
..T
 
Sample Output
Case #1: 5
Case #2: -1
 
Source
 
Recommend
hujie
 
题意:在一个NxN的方格图中,有些位置有摄像头,每一秒都会顺时针转90度,摄像头能看守的范围是2个格子(摄像头所在的和面对的一个格子),然后要从起点走到终点,你有一个箱子,当你藏在箱子里面移动时,摄像头照到你也米有事,但是这样走一格需要3秒,不藏在箱子里走需要1秒,最后要求完成任务最少的时间。
 
思路:首先,题目说了,当一个格子被摄像头照着时,你又要进入这个格子,那么你就必须要用箱子。当你从一个正在被摄像头照着的格子出去时,也必须要用箱子。注意到摄像头的运动是会循环的,当你的当前时间d,根据d%4能直接得出摄像头正在照的方向。 所以我们用d[r][c][mod] 表示当前在(r,c),并且时间%4 为mod时,的最少时间。 那么我们转移的时候也可以选择不动,其他的转移根据摄像头的状态判断一下就好了。
 #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+优先队列】的更多相关文章

  1. HDU 5040 Instrusive(BFS+优先队列)

    题意比较啰嗦. 就是搜索加上一些特殊的条件,比如可以在原地不动,也就是在原地呆一秒,如果有监控也可以花3秒的时间走过去. 这种类型的题目还是比较常见的.以下代码b[i][j][x]表示格子i行j列在x ...

  2. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  3. hdu 1072 Nightmare (bfs+优先队列)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...

  4. hdu 5040 Instrusive

    Instrusive Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Tota ...

  5. HDU——1242Rescue(BFS+优先队列求点图最短路)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  6. 2014年北京网络赛 Instrusive HDU 5040 题解 优先队列

    网赛的时候看了这道题,发现就是平常的那种基础搜索题. 由于加了一个特殊条件:可以一次消耗3秒或原地停留1秒. 那就不能使用简单的队列了,需要使用优先队列才行. 题意 告诉一副地图:一个起点,一个终点, ...

  7. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  8. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  9. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

随机推荐

  1. 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 ;

  2. html备忘录

    上传文件 <form action="/ajax/" method="post" enctype="multipart/form-data&qu ...

  3. POI创建生成excel及设置相关属性

    简单的读写到excel中: import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io. ...

  4. 在Phonegap下实现oAuth认证

    原文:http://www.kuqin.com/mobile/20120719/322873.html 前段时间做过两次关于Phonegap的现场交流会议分享.基本上把Phonegap的一些特性和大家 ...

  5. vue-music:歌词的其他功能

    由于歌词的播放需要歌曲播放,切换歌曲,歌曲的播放模式等等有关联,因此,需要在这几处处理相关问题 1.循环播放回不到开始位置 loop() { this.$refs.audio.currentTime ...

  6. HTML 文件类表单元素如何限制上传类型,Accept属性设置

    需求描述:简单的控制file的选择类型 解决方法:使用HTML  input file 的accept属性控制 实例: <form action="demo_form.asp" ...

  7. POJ:2406-Power Strings(寻找字符串循环节)

    Power Strings Time Limit: 3000MS Memory Limit: 65536K Description Given two strings a and b we defin ...

  8. 检查DLL,EXE文件是64位或者32位的方法

    检查DLL,EXE文件是64位或者32位:输入corflags <assembly path>:

  9. loj2145 「SHOI2017」分手是祝愿

    记 \(f_i\) 是从要做 \(i\) 步好操作变成要做 \(i-1\) 步好操作的期望操作次数. 显然 \(f_i=i/n \times 1 + (1-i/n) \times (1 + f_{i+ ...

  10. zoj 2104 Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2 Seconds      Memory Limit: 65536 KB Contest time again! How excit ...