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 ...
随机推荐
- 剑指offer-17题
题目要求:输入一个表示整数的字符串,把该字符串转换成整数并输出.例如输入字符串"345",则输出整数345. 分析:这道题能够很好地反应出程序员的思维和编程习惯. 的确,自己编写的 ...
- 2-Highcharts 3D图之3D柱状图带可调试倾斜角度
<!DOCTYPE> <html lang='en'> <head> <title>2-Highcharts 3D图之3D柱状图带可调试倾斜角度< ...
- [geeksforgeeks] Convert a given Binary Tree to Doubly Linked List
http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ Given a Bin ...
- HDOJ 1069 DP
开启DP之路 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1069 描述一下: 就是给定N(N<=20)个方体,让你放置,求放置的最高高度,限制条件 ...
- Ajax风格的一款网页Loading效果
现在比较流行的一款Ajax风格的网页Loading,多见于一些大量使用Ajax技术的网站中,页面加载时会自动显示提示信息,带载入动画效果,网页加载完自动消失,是一款正在具有Loading功能的网页进度 ...
- Sqli-labs less 29
Less-29 首先先看下tomcat中的index.jsp文件 在apache的index.php中,sql语句为 $sql="SELECT * FROM users WHERE id=' ...
- ASP.NET母版页与内容页相对路径的问题
1. 图片问题 图片显示问题:<img runat="server" src="~/images/ad468x60.gif" alt="&quo ...
- DELPHI 获取本月 的第一天 和 最后一天
USER :DateUtils 使用 StartOfTheMonth 和 EndOfTheMonth 函数获取即可: procedure TForm1.btn1Click(Sender: TObj ...
- LA 4727
Integers 1, 2, 3,..., n are placed on a circle in the increasing order as in the following figure. W ...
- 思考 ”前端开发人员都在关注的 GitHub 资源“
点这里 原文: 资源 免费的计算机编程类中文书籍 免费编程书籍 计算机科学论文 codeparkshare Python初学者书籍.视频.资料.社区推荐 Python资料汇总 app应用推荐 码农周刊 ...