http://acm.hdu.edu.cn/showproblem.php?pid=1242

感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了。

但是别人说要用优先队列来保证时间最优,我倒是没明白,步数最优跟时间最优不是等价的吗?就算士兵要花费额外时间,可是既然先到了目标点那时间不也一定是最小的?

当然用优先队列+ a去搜索r是最稳妥的。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. struct maze
  8. {
  9. int x,y,t;
  10. bool operator < (const maze a) const
  11. {
  12. return t>a.t;
  13. }
  14. };
  15. int n,m,time;
  16. bool flag;
  17. char field[][];
  18. int dir[][]={{-,},{,},{,},{,-}};
  19. void bfs(maze s)
  20. {
  21. priority_queue<maze>que;
  22. que.push(s);
  23. while(!que.empty())
  24. {
  25. maze e=que.top();
  26. que.pop();
  27. for(int i=;i<;i++)
  28. {
  29. s.x=e.x+dir[i][];
  30. s.y=e.y+dir[i][];
  31. s.t=e.t+;
  32. if(s.x>=&&s.x<n&&s.y>=&&s.y<m&&field[s.x][s.y]!='#')
  33. {
  34. if(field[s.x][s.y]=='r')
  35. {
  36. flag=;time=s.t;return;
  37. }
  38. else if(field[s.x][s.y]=='x') s.t++;
  39.  
  40. //printf("%d %d %d\n",s.x,s.y,s.t);
  41. field[s.x][s.y]='#';
  42. que.push(s);
  43. }
  44. }
  45. }
  46. }
  47.  
  48. int main()
  49. {
  50. //freopen("a.txt","r",stdin);
  51. maze s;
  52. while(~scanf("%d%d",&n,&m))
  53. {
  54. getchar();
  55. for(int i=;i<n;i++)
  56. {
  57. scanf("%s",field[i]);
  58. for(int j=;j<m;j++)
  59. {
  60. if(field[i][j]=='a')
  61. {
  62. s.x=i;
  63. s.y=j;
  64. s.t=;
  65. }
  66. }
  67. }
  68. //printf("%d %d\n",s.x,s.y);
  69. flag=;
  70. field[s.x][s.y]='#';
  71. bfs(s);
  72. if(flag) printf("%d\n",time);
  73. else printf("Poor ANGEL has to stay in the prison all his life.\n");
  74. }
  75. }
 
http://acm.hdu.edu.cn/showproblem.php?pid=2425
这题是给定了起始点和目标点,让你求起始点到目标点的最少花费。总共有3种点,每一种点的花费都不同。跟上面那体没有很大区别。
这题简直悲剧,提交错了5,6次,就是一点小错误还害得我对拍了很多次。以后一定要细心。
判断到达目标点的时候不要再循环里面判断,可能出错。
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5. struct maze
  6. {
  7. int x,y,cost;
  8. bool operator < (const maze a) const
  9. {
  10. return cost>a.cost;
  11. }
  12. };
  13. int r,c;
  14. int vp,vs,vt;
  15. int sr,sc,tr,tc;
  16. int dir[][]={{-,},{,},{,-},{,}};
  17. char field[][];
  18. int bfs()
  19. {
  20. priority_queue<maze>que;
  21. maze s;
  22. s.x=sr;s.y=sc;s.cost=;
  23. field[s.x][s.y]='@';
  24. que.push(s);
  25. while(!que.empty())
  26. {
  27. maze e=que.top();
  28. que.pop();
  29. // printf("%d %d %d\n",s.x,s.y,s.cost);
  30. if(e.x==tr&&e.y==tc) return e.cost;
  31. for(int i=;i<;i++)
  32. {
  33. s=e;
  34. s.x=e.x+dir[i][];
  35. s.y=e.y+dir[i][];
  36. if(s.x<||s.x>=r||s.y<||s.y>=c||field[s.x][s.y]=='@') continue;
  37. if(field[s.x][s.y]=='#') s.cost+=vp;
  38. else if(field[s.x][s.y]=='.') s.cost+=vs;
  39. else if(field[s.x][s.y]=='T') s.cost+=vt;
  40. field[s.x][s.y]='@';
  41. que.push(s);
  42. }
  43. }
  44. return -;
  45. }
  46. int main()
  47. {
  48. //freopen("data.txt","r",stdin);
  49. //freopen("b.txt","w",stdout);
  50. int j=;
  51. while(~scanf("%d%d",&r,&c))
  52. {
  53. //printf("%d %d\n",r,c);
  54. scanf("%d%d%d",&vp,&vs,&vt);
  55. //printf("%d %d %d\n",vr,vs,vt);
  56. getchar();
  57. for(int i=;i<r;i++) scanf("%s",field[i]);
  58. scanf("%d%d%d%d",&sr,&sc,&tr,&tc);
  59. printf("Case %d: %d\n",j++,bfs());
  60. }
  61. return ;
  62. }
 

hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)的更多相关文章

  1. hdu 2425 Hiking Trip

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2425 Hiking Trip Description Hiking in the mountains ...

  2. hdu 2425 Hiking Trip (bfs+优先队列)

    Problem Description Hiking in the mountains is seldom an easy task for most people, as it is extreme ...

  3. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  4. HDU 1242 Rescue(优先队列)

    题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description   Angel was caught by ...

  5. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  6. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  7. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  8. HDU 1242 rescue (优先队列模板题)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. hdu 1242 Rescue(BFS,优先队列,基础)

    题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...

随机推荐

  1. 剑指offer-17题

    题目要求:输入一个表示整数的字符串,把该字符串转换成整数并输出.例如输入字符串"345",则输出整数345. 分析:这道题能够很好地反应出程序员的思维和编程习惯. 的确,自己编写的 ...

  2. 2-Highcharts 3D图之3D柱状图带可调试倾斜角度

    <!DOCTYPE> <html lang='en'> <head> <title>2-Highcharts 3D图之3D柱状图带可调试倾斜角度< ...

  3. [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 ...

  4. HDOJ 1069 DP

    开启DP之路 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1069 描述一下: 就是给定N(N<=20)个方体,让你放置,求放置的最高高度,限制条件 ...

  5. Ajax风格的一款网页Loading效果

    现在比较流行的一款Ajax风格的网页Loading,多见于一些大量使用Ajax技术的网站中,页面加载时会自动显示提示信息,带载入动画效果,网页加载完自动消失,是一款正在具有Loading功能的网页进度 ...

  6. Sqli-labs less 29

    Less-29 首先先看下tomcat中的index.jsp文件 在apache的index.php中,sql语句为 $sql="SELECT * FROM users WHERE id=' ...

  7. ASP.NET母版页与内容页相对路径的问题

    1. 图片问题 图片显示问题:<img runat="server" src="~/images/ad468x60.gif" alt="&quo ...

  8. DELPHI 获取本月 的第一天 和 最后一天

    USER :DateUtils 使用 StartOfTheMonth 和 EndOfTheMonth 函数获取即可:   procedure TForm1.btn1Click(Sender: TObj ...

  9. LA 4727

    Integers 1, 2, 3,..., n are placed on a circle in the increasing order as in the following figure. W ...

  10. 思考 ”前端开发人员都在关注的 GitHub 资源“

    点这里 原文: 资源 免费的计算机编程类中文书籍 免费编程书籍 计算机科学论文 codeparkshare Python初学者书籍.视频.资料.社区推荐 Python资料汇总 app应用推荐 码农周刊 ...