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. 上下文 xx

    上下文,就是指在程序中的某个位置,可以访问到的所有资源的总和. 具体说来,在程序中资源可能是一个变量.一个常量.一个类的引用等等.

  2. URL URI URN的区别

    下面这张图可以完美的解释他们三者之间的关系 URI包含URL和URN Uniform Resource Identifier :统一资源标志符,用于标识某一互联网资源 Uniform Resoutce ...

  3. JAVA中IP和整数相互转化(含有掩码的计算)

    import java.net.InetAddress;/** * 用于IP和整数之间的相互转换 * @author Andy.Wang * */public class IPv4Util {    ...

  4. 6.python

    最早的'密码本' ascii 涵盖了英文字母大小写,特殊字符,数字.01010101ascii 只能表示256种可能,太少,创办了万国码 unicode 16表示一个字符不行,32位表示一个字符. A ...

  5. Bootstrap历练实例:表单控件状态(禁用的字段集fieldset)

    禁用的字段集 fieldset 对 <fieldset> 添加 disabled 属性来禁用 <fieldset> 内的所有控件. <!DOCTYPE html>& ...

  6. linux 用dd命令读写引导区文件

    分类: LINUX 备份MBR,linux下使用如下命令: # dd if=/dev/hda of=/root/linux.bin bs=512 count=1 这里注意使用if=/dev/hda备份 ...

  7. (12)zabbix agent 类型所有key

    zabbix服务器端通过与zabbix agent通信来获取客户端服务器的数据,agent分为两个版本,其中一个是主动一个是被动,在配置主机我们可以看到一个是agent,另一个是agent(activ ...

  8. Mac OS X下安装Vue脚手架(vue-cli)

    前言 Vue作为前端三大框架(Angular,React,Vue)之一,号称是最简单,最容易上手的框架,同时也是行内的大趋势,还可以用来开发最火的小程序.具有开发快,双向数据流等特点,有些人认为Vue ...

  9. 《UNIX环境高级编程》笔记——4.文件和目录

    一.引言 本章描述文件系统的其他特征和文件的性质.有些背景知识需要注意,例如用户ID与文件权限.文件系统等. 二.函数stat.fstat.fstatat和lstat #include <sys ...

  10. django第10天(聚合查询,常用字段)

    django第10天 聚合查询 聚合函数的使用场景 单独使用:不分组,只查聚合结果 分组使用:按字段分组,可查分组字段与聚合结果 导入聚合函数 from django.db.models import ...