hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242
感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了。
但是别人说要用优先队列来保证时间最优,我倒是没明白,步数最优跟时间最优不是等价的吗?就算士兵要花费额外时间,可是既然先到了目标点那时间不也一定是最小的?
当然用优先队列+ a去搜索r是最稳妥的。
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std; struct maze
{
int x,y,t;
bool operator < (const maze a) const
{
return t>a.t;
}
};
int n,m,time;
bool flag;
char field[][];
int dir[][]={{-,},{,},{,},{,-}};
void bfs(maze s)
{
priority_queue<maze>que;
que.push(s);
while(!que.empty())
{
maze e=que.top();
que.pop();
for(int i=;i<;i++)
{
s.x=e.x+dir[i][];
s.y=e.y+dir[i][];
s.t=e.t+;
if(s.x>=&&s.x<n&&s.y>=&&s.y<m&&field[s.x][s.y]!='#')
{
if(field[s.x][s.y]=='r')
{
flag=;time=s.t;return;
}
else if(field[s.x][s.y]=='x') s.t++; //printf("%d %d %d\n",s.x,s.y,s.t);
field[s.x][s.y]='#';
que.push(s);
}
}
}
} int main()
{
//freopen("a.txt","r",stdin);
maze s;
while(~scanf("%d%d",&n,&m))
{
getchar();
for(int i=;i<n;i++)
{
scanf("%s",field[i]);
for(int j=;j<m;j++)
{
if(field[i][j]=='a')
{
s.x=i;
s.y=j;
s.t=;
}
}
}
//printf("%d %d\n",s.x,s.y);
flag=;
field[s.x][s.y]='#';
bfs(s);
if(flag) printf("%d\n",time);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
}
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct maze
{
int x,y,cost;
bool operator < (const maze a) const
{
return cost>a.cost;
}
};
int r,c;
int vp,vs,vt;
int sr,sc,tr,tc;
int dir[][]={{-,},{,},{,-},{,}};
char field[][];
int bfs()
{
priority_queue<maze>que;
maze s;
s.x=sr;s.y=sc;s.cost=;
field[s.x][s.y]='@';
que.push(s);
while(!que.empty())
{
maze e=que.top();
que.pop();
// printf("%d %d %d\n",s.x,s.y,s.cost);
if(e.x==tr&&e.y==tc) return e.cost;
for(int i=;i<;i++)
{
s=e;
s.x=e.x+dir[i][];
s.y=e.y+dir[i][];
if(s.x<||s.x>=r||s.y<||s.y>=c||field[s.x][s.y]=='@') continue;
if(field[s.x][s.y]=='#') s.cost+=vp;
else if(field[s.x][s.y]=='.') s.cost+=vs;
else if(field[s.x][s.y]=='T') s.cost+=vt;
field[s.x][s.y]='@';
que.push(s);
}
}
return -;
}
int main()
{
//freopen("data.txt","r",stdin);
//freopen("b.txt","w",stdout);
int j=;
while(~scanf("%d%d",&r,&c))
{
//printf("%d %d\n",r,c);
scanf("%d%d%d",&vp,&vs,&vt);
//printf("%d %d %d\n",vr,vs,vt);
getchar();
for(int i=;i<r;i++) scanf("%s",field[i]);
scanf("%d%d%d%d",&sr,&sc,&tr,&tc);
printf("Case %d: %d\n",j++,bfs());
}
return ;
}
hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)的更多相关文章
- hdu 2425 Hiking Trip
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2425 Hiking Trip Description Hiking in the mountains ...
- hdu 2425 Hiking Trip (bfs+优先队列)
Problem Description Hiking in the mountains is seldom an easy task for most people, as it is extreme ...
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- HDU 1242 Rescue(优先队列)
题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- HDU 1242 rescue (优先队列模板题)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu 1242 Rescue(BFS,优先队列,基础)
题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...
随机推荐
- O(V*n)的多重背包问题
多重背包问题: 有n件物品,第i件价值为wi,质量为vi,有c1件,问,给定容量V,求获得的最大价值. 朴素做法: 视为0,1,2,...,k种物品的分组背包 [每组只能选一个] f[i][j]=Ma ...
- OrzFAng系列–树 解题报告
题目描述 方方方种下了三棵树,两年后,第二棵树长出了n个节点,其中1号节点是根节点. 给定一个n个点的树 支持两种操作 方方方进行m次操作,每个操作为: (1)给出两个数i,x,将第i个节点的子树中, ...
- Task相关
1.Task的优势: 1)把任务当成变量来用,可以作为参数而传递: 2)可以捕获到异步操作中发生的异常. 2.开始异步 Task.Factory.StartNew(() => Thread.Sl ...
- 2014ACM/ICPC亚洲区西安站 复旦命题
http://codeforces.com/gym/100548 A 签到 问一个序列是不是yes,yes的序列满足每个数都是3的倍数. #include<cstdio> int main ...
- GA项目体会
1.NaN表示运算的结果是未定义的计算过程,例如0/0.在计算EBO的时候,由于使用泊松分布的计算过程,出现了0/0的情况,所以控制台才会提示"非数字". 2.保障资金太小的时候可 ...
- XHProf的安装和使用(PHP性能测试神器)
XHProf是Facebook开发的性能调试工具,帮助我们的PHP程序性能调优,更加健壮.XHProf安装和使用方法将在本章讲解.XHProf是PHP的PECL扩展.没有XDeBug那些耗费资源,更加 ...
- C#创建UTF8无BOM文本文件
In order to omit the byte order mark (BOM), your stream must use a custom instance of UTF8Encoding i ...
- javascript和swf在网页中交互的一些总结
Javascript和swf在网页中交互一般可有以下几种情况: 1.swf和这些调用的javascript在同域 2.swf和这些调用的javascript在不同域,比如加载远程的swf然后call别 ...
- Javascript 正则表达式_3
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 从Config文件中读取节点的配置信息
下面是web.config中与本内容有关的细节 <appSettings> <add key="servername" value="www" ...