hdoj-1242-Rescue【广搜+优先队列】
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21510 Accepted Submission(s): 7671
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up,
down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
13
pid=1240" target="_blank">
1240
pid=1072" target="_blank">
1072
pid=1372" target="_blank">
1372
pid=1175" target="_blank">
1175
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
char MAP[202][202];
bool visit[202][202];
struct node{
int x,y;
int flag;
};
int u[4]={1,0,-1,0};
int v[4]={0,1,0,-1};
int cmp(node a,node b){
return a.flag<b.flag;
}
int BFS(int i,int j){
queue<node> Q; //Q要在数组内定义,否则要记得每次用之前将队列清空。由于上次的队列可能会由于没有全然出队而保留上次的数据。当中我在这就出了错! node t;
t.x=i; t.y=j;
t.flag=0;
Q.push(t);
visit[i][j]=1;
while(!Q.empty()){
t=Q.front();
int tx,ty;
tx=t.x;ty=t.y;
if(MAP[tx][ty]=='r'){
return t.flag;
}
Q.pop();
node temp[4];
int p=0,k;
for(k=0;k<4;++k){
if(MAP[tx+u[k]][ty+v[k]]!='#'&&!visit[tx+u[k]][ty+v[k]]){
if(MAP[tx+u[k]][ty+v[k]]=='x'){
temp[p].x=tx+u[k];
temp[p].y=ty+v[k];
visit[temp[p].x][temp[p].y]=1;
temp[p].flag=t.flag+2;
++p;
}
else{
temp[p].x=tx+u[k];
temp[p].y=ty+v[k];
visit[temp[p].x][temp[p].y]=1;
temp[p].flag=t.flag+1;
++p;
}
// Q.push(temp); //此处不能 直接入队,由于同等级的 r要比 x耗时短,所以先将 t上下左右的四个点存放到数组中
}
}
sort(temp,temp+p,cmp); //经过排列后再依次入队
for(k=0;k<p;++k){
Q.push(temp[k]);
}
}
return -1;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
int i,j;
for(i=0;i<=n+1;++i){
for(j=0;j<=m+1;++j){
MAP[i][j]='#';
}
}
int ax,ay;
for(i=1;i<=n;++i){
getchar();
for(j=1;j<=m;++j){
scanf("%c",&MAP[i][j]);
if(MAP[i][j]=='a') ax=i,ay=j;
}
}
memset(visit,0,sizeof(visit));
int res=BFS(ax,ay);
if(res==-1) printf("Poor ANGEL has to stay in the prison all his life.\n");
else printf("%d\n",res);
}
return 0;
}
hdoj-1242-Rescue【广搜+优先队列】的更多相关文章
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- hdoj 1242 Rescue
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1242 Rescue (BFS+优先队列)
题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间. 析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条 ...
- HDU - 3345 War Chess 广搜+优先队列
War chess is hh's favorite game: In this game, there is an N * M battle map, and every player has hi ...
- BFS HDOJ 1242 Rescue
题目传送门 题意:从r走到a,遇到x多走一步,问最小走到a的步数 分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS.我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不 ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 3152 Obstacle Course(优先队列,广搜)
题目 用优先队列优化普通的广搜就可以过了. #include<stdio.h> #include<string.h> #include<algorithm> usi ...
- USACO Milk Routing /// 优先队列广搜
题目大意: 在n个点 m条边的无向图中 需要运送X单位牛奶 每条边有隐患L和容量C 则这条边上花费时间为 L+X/C 求从点1到点n的最小花费 优先队列维护 L+X/C 最小 广搜到点n #inclu ...
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
随机推荐
- WebDriverException: Message: unknown error: Chrome failed to start: crashed
the last answer WebDriverException: Message: unknown error: Chrome failed to start: crashed
- tab bar controller
下面记一下怎样通过代码的方式为选项卡添加视图. 1.创建一个基于Empty Application的项目 2.创建两个新类,基类选择UIViewController,勾选With XIB for us ...
- jquery-closest
1.closest() 本例演示如何通过 closest() 完成事件委托.当被最接近的列表元素或其子后代元素被点击时,会切换黄色背景: $( document ).bind("click& ...
- ArrayList集合(JDK1.8)
简述 List是继承于Collection接口,除了Collection通用的方法以外,扩展了部分只属于List的方法. 常用子类 ?ArrayList介绍 1.数据结构 其底层的数据结构是数组,数 ...
- java面试微信交流群-欢迎你的加入
Java后端技术专注Java相关技术:SSM.Spring全家桶.微服务.MySQL.MyCat.集群.分布式.中间件.Linux.网络.多线程,偶尔讲点运维Jenkins.Nexus.Docker. ...
- python 02 8/21-8/23
计算机由硬件系统和软件系统组成,硬件系统分为由CPU(运算器+控制器)+内存储器(电信号)组成的主机部分,由输入设备+输出设备+外存储器(硬盘(磁信号).U盘)组成的外设部分.软件系统由系统软件和应用 ...
- Python数据分析 Pandas模块 基础数据结构与简介(一)
pandas 入门 简介 pandas 组成 = 数据面板 + 数据分析工具 poandas 把数组分为3类 一维矩阵:Series 把ndarray强大在可以存储任意数据类型可以专门处理时间数据 二 ...
- Nginx安装及基本配置
本文内容: 90%来自以下网址:http://www.nginx.cn/install ,修改了一些版本信息 10%来自以下网址:http://nginx.org/en/docs/beginners_ ...
- LeetCode(25)Reverse Nodes in k-Group
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- BootStrap学习01框架搭建
中文文档:https://v3.bootcss.com/css/ 开发工具 WebStorm 一.新建项目08bootstrap 引入bootstrap-3.3.7,引入jQuery,引入holder ...