HDU 1242 rescue and 优先级队列的条目
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 题意:一个牢房图。#是墙壁,a是angel。r是angel的朋友。x是敌人,每走一步消耗1个单位时间。消灭1个敌人也消耗一个单位时间。求r到a的最小时间。 。 依照正常的思路:直接从R BFS 到 a 遇到 x就多加一个时间 即可了 只是这样是不正确了(题目太水 还是A了)
伪AC代码:#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
#define w 205
char map[w][w];
int vis[w][w];
int sx,sy;
int m,n;
struct node {
int x,y,time;
};
int fx[4][2]={1,0,0,1,-1,0,0,-1};
int bfs()
{
int tx,ty;
memset(vis,0,sizeof(vis));
node now,next;
queue<node>q;
now.x=sx;now.y=sy;now.time=0;
vis[sx][sy]=1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(map[now.x][now.y]=='r')
return now.time;
for(int i=0;i<4;i++)
{
tx=now.x+fx[i][0];
ty=now.y+fx[i][1];
if(tx<1||tx>m||ty<1||ty>n||vis[tx][ty]==1||map[tx][ty]=='#')
continue;
vis[tx][ty]=1;
next.x=tx;
next.y=ty;
if(map[tx][ty]=='x')
next.time=now.time+2;
else
next.time=now.time+1;
q.push(next);
}
}
return -1;
}
int main()
{
int i,j;
while(cin>>m>>n)
{
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
cin>>map[i][j];
if(map[i][j]=='a')
{
sx=i;sy=j;
}
}
int ss= bfs();
if(ss==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ss);
}
return 0;
}但面对这组数据:
3 4
a...
##x.
###r结果 却是6(应该是5啊)
原因事实上是(2,3) 和(1.4)是同步进入队列的 进入的顺序和方向数组有关= =
这个问题能够用优先队列解决。 详见代码
优先队列版:
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
#define w 205
char map[w][w];
int vis[w][w];
int sx,sy;
int m,n;
struct node {
int x,y,time;
bool operator <(const node & t) const
{
return time>t.time; //改成<号 则较大的先出队
}
};
int fx[4][2]={1,0,0,1,-1,0,0,-1};
int bfs()
{
int tx,ty;
memset(vis,0,sizeof(vis));
node now,next;
priority_queue<node>q; //加上前缀 priority_
now.x=sx;now.y=sy;now.time=0;
vis[sx][sy]=1;
q.push(now);
while(!q.empty())
{
now=q.top(); //优先队列不能用 q.front();
q.pop();
if(map[now.x][now.y]=='r')
return now.time;
for(int i=0;i<4;i++)
{
tx=now.x+fx[i][0];
ty=now.y+fx[i][1];
if(tx<1||tx>m||ty<1||ty>n||vis[tx][ty]==1||map[tx][ty]=='#')
continue;
vis[tx][ty]=1;
next.x=tx;
next.y=ty;
if(map[tx][ty]=='x')
next.time=now.time+2;
else
next.time=now.time+1;
q.push(next);
}
}
return -1;
}
int main()
{
int i,j;
while(cin>>m>>n)
{
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
cin>>map[i][j];
if(map[i][j]=='a')
{
sx=i;sy=j;
}
}
int ss= bfs();
if(ss==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ss);
}
return 0;
}模板。
。。
版权声明:本文博主原创文章。博客,未经同意不得转载。
HDU 1242 rescue and 优先级队列的条目的更多相关文章
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- hdu 1242 Rescue(BFS,优先队列,基础)
题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...
- hdu 1242 Rescue(bfs)
此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...
- 杭电 HDU 1242 Rescue
http://acm.hdu.edu.cn/showproblem.php?pid=1242 问题:牢房里有墙(#),警卫(x)和道路( . ),天使被关在牢房里位置为a,你的位置在r处,杀死一个警卫 ...
- HDU 1242 Rescue(优先队列)
题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by ...
- HDU 1242 rescue (优先队列模板题)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
随机推荐
- Case When PK PIVOT
SELECT *FROM ScoreInfogo Name Course Score---------- ---------- -----------Lucy Chinese 74Jim Math 8 ...
- 设置UITextField的placeholder的颜色
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
- MinGW中的头文件路径级环境变量设置
整理自 MinGW中的头文件路径 C头文件目录%MINGW_PATH%/include下有头文件,里面有strcpy等c函数的声明. C++头文件目录%MINGW_PATH%/lib/gcc/mi ...
- 盘点 PHP 和 ASP.NET 的10大对比!
[编者按]本文主要针对开源 PHP 和非开源的 ASP.NET 在性能.成本.可扩展性,技术支持和复杂性等方面进行比较. 在网上论坛,总是有成百上千的文章和帖子在讨论 PHP 和 ASP.NET,究竟 ...
- Swifter初体验;按照惯例,来一个Swift版本的:iOS图片验证码?
不多解释,上图,上代码:代码
- Dynamips/Dynagen模拟CISCO路由环境
今天将<网络互连技术>--路由,交换与远程访问实训教程的实验书拿出来了看了部门. 搭建了一个基于DYNAGEN的虚拟环境. 归纳一下大约步骤: ~~~~~~~~~~~~~~ 一,在WIND ...
- 【HDU 3652】 B-number (数位DP)
B-number Problem Description A wqb-number, or B-number for short, is a non-negative integer whose de ...
- CAS单点登录配置[2]:证书生成
上一篇介绍了准备工作,本片将介绍如何生成证书. 服务器端证书 1 我们在F盘下建立一个cas文件夹,在此文件夹中生成证书文件,打开命令窗口,进入此目录下,如图: 2 生成服务器端证书, 此命令用于在当 ...
- dll的加载方式主要分为两大类,显式和隐式链接
之前简单写过如何创建lib和dll文件及简单的使用(http://blog.csdn.net/betabin/article/details/7239200).现在先再深入点写写dll的加载方式. d ...
- 无法自动调试 未能调试远程过程。这通常说明未在服务器上启用调试 WCF 托管在IIS上
解决方案,把新建的网站的app.config修改下配置 <system.web> <!-- 设置 compilation debug="true" 可将调试符号插 ...