1. #include<iostream>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<algorithm>
  6. #include<queue>
  7. #include<stack>
  8. using namespace std;
  9. const int qq=110;
  10. const int MAX=10000;
  11. char map[qq][qq];
  12. int pre[qq][qq];
  13. int a[MAX],b[MAX];
  14. int n,m;
  15. int dir[5][2]={0,0,1,0,-1,0,0,1,0,-1}; // 走的顺序是右,左,下,上
  16. struct Node
  17. {
  18. int x,y;
  19. int pre; // 定义来源的方向 1左 2右 3上 4下
  20. char mark; //是否存在怪兽
  21. int time;
  22. friend bool operator <(Node a,Node b)
  23. {
  24. return a.time>b.time;
  25. }
  26. };
  27. priority_queue<Node>Q;
  28. int check(int x,int y)
  29. {
  30. if(x<0||y<0||x>=n||y>=m) //判断是否越界
  31. return 0;
  32. if(pre[x][y]) // 判断是否走过这点
  33. return 0;
  34. if(map[x][y]=='X') // 判断是否是墙
  35. return 0;
  36. return 1;
  37. }
  38. void bfs()
  39. {
  40. Node ans,cns;
  41. ans.x=0;ans.y=0;ans.pre=0;ans.time=0;ans.mark=map[0][0];
  42. Q.push(ans);
  43. while(!Q.empty()){
  44. cns=Q.top();
  45. Q.pop();
  46. //printf("%d %d\n",cns.x,cns.y);
  47. if(cns.x==n-1&&cns.y==m-1){
  48. //printf("%d %d\n",cns.x,cns.y);
  49. while(!Q.empty())
  50. Q.pop();
  51. Q.push(cns);
  52. break;
  53. }
  54. for(int i=1;i<=4;++i){
  55. ans.x=cns.x+dir[i][1];
  56. ans.y=cns.y+dir[i][0];
  57. if(check(ans.x,ans.y)){
  58. ans.mark=map[ans.x][ans.y];
  59. ans.pre=i;
  60. pre[ans.x][ans.y]=i;
  61. if(map[ans.x][ans.y]>='1'&&map[ans.x][ans.y]<='9')
  62. ans.time=cns.time+1+(map[ans.x][ans.y]-'0');
  63. else
  64. ans.time=cns.time+1;
  65. Q.push(ans);
  66. }
  67. }
  68. }
  69. return;
  70. }
  71. void out()
  72. {
  73. Node ans;
  74. ans=Q.top();
  75. Q.pop();
  76. printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans.time);
  77. int r=1;
  78. a[0]=ans.x;b[0]=ans.y;
  79. int x=ans.x,y=ans.y;
  80. //printf("%d %d\n",x,y);
  81. while(x!=0||y!=0){ // x和y搞反了 调了两个小时、 真特么醉了
  82. if(pre[x][y]==1)
  83. y=y-1;
  84. else if(pre[x][y]==2)
  85. y=y+1;
  86. else if(pre[x][y]==3)
  87. x=x-1;
  88. else if(pre[x][y]==4)
  89. x=x+1;
  90. a[r]=x;b[r++]=y;
  91. // printf("%d %d\n",x,y);
  92. }
  93. int t=1;
  94. for(int i=r-2;i>=0;--i){
  95. printf("%ds:(%d,%d)->(%d,%d)\n",t++,a[i+1],b[i+1],a[i],b[i]);
  96. if(map[a[i]][b[i]]>='1'&&map[a[i]][b[i]]<='9')
  97. for(int j=0;j<map[a[i]][b[i]]-'0';++j)
  98. printf("%ds:FIGHT AT (%d,%d)\n",t++,a[i],b[i]);
  99. }
  100. }
  101. int main()
  102. {
  103. while(~scanf("%d %d",&n,&m)){
  104. memset(pre,0,sizeof(pre));
  105. memset(a,0,sizeof(a));
  106. memset(b,0,sizeof(b));
  107. for(int i=0;i<n;++i)
  108. scanf("%s",map[i]);
  109. bfs();
  110. if(Q.empty()) printf("God please help our poor hero.\n");
  111. else out();
  112. printf("FINISH\n");
  113. while(!Q.empty())
  114. Q.pop();
  115. }
  116. return 0;
  117. }
  118. // 记录路径的数组开小了、又调了好久!!QAQ

HDU 1026 BSF+优先队列+记录路径、的更多相关文章

  1. hdu 1226 BFS + bfs记录路径

    http://acm.hdu.edu.cn/showproblem.php? pid=1226 为了节省空间.您可以使用vis初始化数组初始化-1. 发现BFSeasy错了地方 始一直WA在这里:就是 ...

  2. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

  3. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  4. hdu 1026(优先队列+路径输出)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. HDU 1026 Ignatius and the Princess I(带路径的BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...

  6. hdu 1026 Ignatius and the Princess I 搜索,输出路径

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  7. hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  8. hdu 1074(状态压缩dp+记录路径)

    题意:给了n个家庭作业,然后给了每个家庭作业的完成期限和花费的实践,如果完成时间超过了期限,那么就要扣除分数,然后让你找出一个最优方案使扣除的分数最少,当存在多种方案时,输出字典序最小的那种,因为题意 ...

  9. hdu 1664(数论+同余搜索+记录路径)

    Different Digits Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. NFS服务器搭建与配置

    原文:https://blog.csdn.net/qq_38265137/article/details/83146421 NFS服务简介什么是NFS?NFS就是Network File System ...

  2. Spring boot--控制器增强

    在Spring3.2中,新增了@ControllerAdvice注解.关于这个注解的官方说明https://docs.spring.io/spring-framework/docs/5.0.0.M1/ ...

  3. php_imagick是怎么实现复古效果的呢?

    php_imagick程序示例 1.创建一个缩略图并显示出来 <?phpheader('Content-type: image/jpeg');$image = new Imagick('imag ...

  4. 带三角形下标的提示框(按钮button)

    HTML:<div class="leaflet-popup-content-wrapper"> <div class="leaflet-popup-c ...

  5. 【风马一族_php】NO2_php基础知识

    原文来自:http://www.cnblogs.com/sows/p/5995763.html (博客园的)风马一族 侵犯版本,后果自负 回顾 什么是php以及php的发展史 搭建web服务器 apa ...

  6. photoshop正确的打开方式

    首先这边我先贴一个地址:https://www.adobe.com/cn/products/photoshop.html 安装软件,这里就不赘述了,真的不会,可以百度^_^我当初就是百度的,哈哈 说到 ...

  7. hdu 1728 逃离迷宫 BFS加优先队列 DFS()

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意就是能否在规定的转弯次数内从起点走到终点.刚走时那步方向不算. 只会bfs(),但想到这题需要记录转弯 ...

  8. C++之MD5加密(签名)

    md5.h : #include <stdio.h> #include <stdlib.h>#include <time.h> #include <strin ...

  9. GDB调试命令手册

    使用GDB 启动 $ gdb program           # program是你的可执行文件,一般在当前目录 $ gdb program core      # gdb同时调试运行程序和cor ...

  10. asp 与php 的不同之处

    1.asp语句完成后不用加逗号:php要: 2.asp连接字符窜是用&:php用.; 3.asp需要用dim声明变量才能使用:php不用: 4.asp数组用(),php用[]; 5.asp的条 ...