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
在优先队列中,优先级高的元素先出队列。 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。 优先队列的第一种用法,也是最常用的用法:
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
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率感 ...
随机推荐
- 换个语言学一下 Golang (1)
做技术的总是有些拗.这么多年一直在.net的框框里打转转.直到现在市场上.net工作越来越难找,项目越来越老才发现不做出改变不行了.就从学习Go开始吧. Go语言的特点 简洁.快速.安全 并行.有趣. ...
- 洛谷 P2370 P2370 yyy2015c01的U盘
https://www.luogu.org/problemnew/show/P2370 二分+背包 #include <algorithm> #include <iostream&g ...
- LeetCode 买卖股票的最佳时机
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...
- UVa-156-反片语
这题比较精妙的是,我们对于单词重排,实际上是进行了标准化的处理,即按照字典序排序. 这样的话,就很方便地处理了单词的重排问题,我们不需要使用全排列函数进行排列尝试,我们直接化简为一,然后进行比较就可以 ...
- ubuntu 安装 php7.2
sudo apt-get install software-properties-common python-software-properties sudo add-apt-repository p ...
- PyCharm学习笔记(一) 界面配置
通过Ctrl+鼠标滚轮调整字体大小 设置代码区默认字体及大小 设置调试区的字体大小 设置代码风格:如Tab缩进 定义Python模板文件 # @Time : ${DATE} ${TIME} # @ ...
- int (*a)[10] 和 int *a[10] 的区别
int *a[10] :指针数组.数组a里存放的是10个int型指针 int (*a)[10] :数组指针.a是指针,指向一个数组.此数组有10个int型元素 int *a[10] 先找到声明符a,然 ...
- Linux服务器硬件设备信息查看
一.cpu信息 cpu信息存储在/proc文件系统的cpuinfo(/proc/cpuinfo)文件里,可以直接查看这个文件以获得cpu信息,所列字段解释如下: processor : 核心编号,如: ...
- Java-使用哈希码比较对象的值
在一些特殊的情况下使用 package com.tj; import java.io.File; public class MyHash { public static void main(Strin ...
- CodeBlocks "no such file or directory
1但编译时还是会报错:no such file or directory;这是为什么呢? :在项目/构建选项/搜索路径 选项下,点击添加按钮,添加自己的头文件的存放文件夹,搞定... 2.code ...