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 ...
随机推荐
- cf 496B Secret Combination
题目链接:B. Secret Combination You got a box with a combination lock. The lock has a display showing n d ...
- Python3中的新特性(1)——新的语言特性
1.源代码编码和标识符 Python3假定源代码使用UTF-8编码.另外,关于标识符中哪些字符是合法的规则也放宽了.特别是,标识符可以包含代码点为U+0080及以上的任意有效Unico ...
- Goolg Chrome 插件开发--Hello world
开发Chrome插件很简单,只要会web开发(html+javascript+css)那么这个就是能驾轻就熟,只需要了解一下插件具体的运行环境及要求就OK了. 1.先创建一个html文件,名字随便取, ...
- GA项目体会
1.NaN表示运算的结果是未定义的计算过程,例如0/0.在计算EBO的时候,由于使用泊松分布的计算过程,出现了0/0的情况,所以控制台才会提示"非数字". 2.保障资金太小的时候可 ...
- 使用 polyfills 的简易方法
本文作者为 Andrew Betts 与 Robert Nyman.Andrew 是金融时报(Financial Times)实验室主任,该实验室旨在金融时报开发并推广实践性的 Web 技术.Robe ...
- HDU 4148 Length of S(n)(字符串)
题目 字符串处理 题意要猜,解析见代码: /* 这题每个S(n)是描述S(n-1)值 例如: S(1)=1; S(2)=11;即描述S(1)有1个1=11 S(3)=21;即描述S(2)有2个1=21 ...
- SGU101
Dominoes – game played with small, rectangular blocks of wood or other material, each identified by ...
- POJ 2182
#include <iostream> #define MAXN 8005 using namespace std; int _m[MAXN]; int main() { //freope ...
- D&F学数据结构系列——二叉排序树
二叉排序树(Binary Sort Tree) 定义:对于树中的每个结点X,它的左子树中所有关键字值小于X的关键字值,而它的右子树中所有关键字值大于X的关键字值. 二叉查找树声明: #ifndef _ ...
- POJ 1185 炮兵阵地 (状压DP)
题目链接 题意 : 中文题不详述. 思路 :状压DP,1表示该位置放炮弹,0表示不放.dp[i][j][k],代表第 i 行的状态为k时第i-1行的状态为 j 时放置的最大炮弹数.只是注意判断的时候不 ...