被这道题坑到了,如果单纯的模拟题目所给的步骤,就会出现同一个位置要走两次的情况。。。所以对于bfs来说会很头痛。

第一个代码是wa掉的代码,经过调试才知道这个wa的有点惨,因为前面的操作有可能会阻止后面的正确操作:

  1. #include"iostream"
  2. #include"stdio.h"
  3. #include"algorithm"
  4. #include"cmath"
  5. #include"string.h"
  6. #include"queue"
  7. #define mx 100
  8. using namespace std;
  9. char castle[mx][mx];
  10. int n,m,t,p,sx,sy,ex,ey;
  11. struct node
  12. {
  13. int x,y;
  14. int magic;
  15. int times;
  16. char flag;//用来标记当前位置是@还是.
  17. friend bool operator<(node a,node b)
  18. {
  19. if(b.times!=a.times) return b.times<a.times;
  20. return a.magic<b.magic;
  21. }
  22. };
  23. int dir[][]={{,},{,-},{,},{-,}};
  24. bool judge(int x,int y)
  25. {
  26. if(x>=&&x<n&&y>=&&y<m&&castle[x][y]!='#') return true;
  27. return false;
  28. }
  29. void bfs()
  30. {
  31. node cur,next;
  32. int i;
  33. cur.x=sx;cur.y=sy;cur.times=;cur.magic=p;cur.flag='.';
  34. priority_queue<node>q;
  35. q.push(cur);
  36. while(!q.empty())
  37. {
  38. cur=q.top();
  39. q.pop();
  40. // cout<<cur.x<<' '<<cur.y<<' '<<cur.magic<<' '<<cur.times<<' '<<cur.flag<<endl;
  41. if(cur.x==ex&&cur.y==ey&&cur.times<=t)
  42. {
  43. cout<<"Yes, Yifenfei will kill Lemon at "<<cur.times<<" sec."<<endl;
  44. return;
  45. }
  46. if(cur.times>t)
  47. {
  48. cout<<"Poor Yifenfei, he has to wait another ten thousand years."<<endl;
  49. return;
  50. }
  51.  
  52. for(i=;i<;i++)
  53. {
  54. next.x=cur.x+dir[i][];
  55. next.y=cur.y+dir[i][];
  56. if(judge(next.x,next.y))
  57. {
  58. if(cur.flag=='@'&&cur.magic>)
  59. {
  60. next.times=cur.times+;
  61. next.magic=cur.magic-;
  62. if(castle[next.x][next.y]=='@')next.flag='@';
  63. else next.flag='.';
  64. castle[next.x][next.y]='#';
  65. q.push(next);
  66. }
  67. else if(cur.flag=='.')
  68. {
  69. if(castle[next.x][next.y]=='@')
  70. {
  71. if(cur.magic>)
  72. {
  73. next.times=cur.times+;
  74. next.magic=cur.magic-;
  75. next.flag='@';
  76. castle[next.x][next.y]='#';
  77. q.push(next);
  78. }
  79. }
  80. else
  81. {
  82. next.times=cur.times+;
  83. next.magic=cur.magic;
  84. next.flag='.';
  85. castle[next.x][next.y]='#';
  86. q.push(next);
  87. if(cur.magic>)
  88. {
  89. next.times=cur.times+;
  90. next.magic=cur.magic-;
  91. q.push(next);
  92. }
  93.  
  94. }
  95. }
  96. }
  97. }
  98. }
  99. cout<<"Poor Yifenfei, he has to wait another ten thousand years."<<endl;
  100. }
  101. int main()
  102. {
  103. int c=;
  104. while(scanf("%d%d%d%d",&n,&m,&t,&p)==)
  105. {
  106. int i,j;
  107. for(i=;i<n;i++)
  108. for(j=;j<m;j++)
  109. {
  110. cin>>castle[i][j];
  111. if(castle[i][j]=='Y') {sx=i;sy=j;castle[i][j]='#';}
  112. else if(castle[i][j]=='L'){ex=i;ey=j;castle[i][j]='.';}
  113. }
  114. cout<<"Case "<<++c<<":"<<endl;
  115. bfs();
  116. }
  117. return ;
  118. }

参考大神的解法后才知道这题在一开始就应该对步骤简化,因为只有前后两步都是’.'是才可以用走的,。其他任何情况下都要用飞的,所以没有必要像之前那样分

很多种情况讨论。果然编程之前还是要多思考思考尽量简化步骤,这样不仅可以提高效率,而且还可以避免发生不必要的错误。

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<queue>
  4. using namespace std;
  5. char g[][];
  6. bool vis[][][];
  7. int n,m,p,t,si,sj,ans;
  8. int dir[][]={{,},{,},{-,},{,-}};
  9. struct node
  10. {
  11. int step,p,x,y;
  12. node(int a=,int b=,int c=,int d=):x(a),y(b),p(c),step(d){}
  13. bool friend operator <(const node a,const node b)
  14. {
  15. return a.step>b.step;
  16. }
  17. };
  18. void BFS()
  19. {
  20. priority_queue<node> Q;
  21. node f=node(si,sj,p,);
  22. Q.push(f);
  23. memset(vis,false,sizeof(vis));
  24. vis[si][sj][p]=true;
  25. node temp;
  26. while(!Q.empty())
  27. {
  28. temp=Q.top();
  29. Q.pop();
  30. if(temp.step>t)
  31. return ;
  32. if(g[temp.x][temp.y]=='L')
  33. {
  34. ans=temp.step;
  35. return ;
  36. }
  37. for(int k=;k<;k++)
  38. {
  39. int i=dir[k][]+temp.x;
  40. int j=dir[k][]+temp.y;
  41. if(i<||i>n- || j< || j>m-||g[i][j]=='#')
  42. continue;
  43. if(temp.p!= && !vis[i][j][temp.p-])
  44. {
  45. vis[i][j][temp.p-]=true;
  46. Q.push(node(i,j,temp.p-,temp.step+));
  47. }
  48. if(g[temp.x][temp.y]!='@' && g[i][j]!='@'&&!vis[i][j][temp.p])
  49. {
  50. vis[i][j][temp.p]=true;
  51. Q.push(node(i,j,temp.p,temp.step+));
  52. }
  53. }
  54. }
  55. return ;
  56. }
  57. int main()
  58. {
  59. int cas=;
  60. while(scanf("%d %d %d %d",&n,&m,&t,&p)==)
  61. {
  62. for(int i=;i<n;i++)
  63. {
  64. scanf("%s",g[i]);
  65. for(int j=;j<m;j++)
  66. if(g[i][j]=='Y')
  67. si=i,sj=j;
  68. }
  69. ans=;
  70. BFS();
  71. printf("Case %d:\n",++cas);
  72. if(ans>t)
  73. printf("Poor Yifenfei, he has to wait another ten thousand years.\n");
  74. else printf("Yes, Yifenfei will kill Lemon at %d sec.\n",ans);
  75. }
  76. return ;
  77. }

hdu Waiting ten thousand years for Love的更多相关文章

  1. HDU 2653 - Waiting ten thousand years for Love

    首先,对于一个 '@' 飞上去,飞下来都要耗1点魔力,所以是两点= = 然后站在同一格 魔力可能不同,所以要增加一维. 还有当前搜到的不一定是最小. 别的也没啥. #include <iostr ...

  2. hdu 4541 Ten Googol

    http://acm.hdu.edu.cn/showproblem.php?pid=4541 打表找规律 #include <cstdio> #include <cstring> ...

  3. hdu-2619 Love you Ten thousand years

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2619 题目大意: 求出小于n的数的个数,满足ki mod n,1≤i≤n是模n的完全剩余系 解题思路 ...

  4. HDU2653 BFS+优先队列

    Waiting ten thousand years for Love Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/3 ...

  5. hdu2653之BFS

    Waiting ten thousand years for Love Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. HDU 2653 (记忆化BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...

  7. AssetBundle loading failed because.....已解决

    http://blog.csdn.net/ldghd/article/details/9632455 *****************************      一      ******* ...

  8. 【英语魔法俱乐部——读书笔记】 3 高级句型-简化从句&倒装句(Reduced Clauses、Inverted Sentences) 【完结】

    [英语魔法俱乐部——读书笔记] 3 高级句型-简化从句&倒装句(Reduced Clauses.Inverted Sentences):(3.1)从属从句简化的通则.(3.2)形容词从句简化. ...

  9. sentence patterns

    第四部分     推理题 1.世界上每个角落的每个人都有立场,都有背景,都有推理性,能推理出一个人语言的真意,才成就了真正的推理能力: 2.换言之,如果你能通过一个人的说话推理出其身份职业,你的推理能 ...

随机推荐

  1. 关于Xcode6 Segue 的疑问,没有解决!

    xcode6 的segue 变化了,如图 关于前3个选项,始终没有太明白,我试验结果如下,简单地把几个viewController连接起来时,无论用show,还是showdetail,还是Presen ...

  2. 【USACO】pprime

    开始看第一眼题就觉得问题会在超时上,果然写了个小代码运行到test 9时超时了 #include <stdio.h> #include <math.h> int isprime ...

  3. iphone越狱还原

    在Cydia 里安装Impactor 就行了 .在操作时需要全程联网: .请至少保证 % 的电量以防止在恢复过程出现断电的情况(建议将设备连接至电源): .设备将恢复至出厂状态,所有用户数据都将被清除 ...

  4. JQZoom 图片放大插件的使用

    QZoom是一个基于最流行的jQuery的图片放大器插件.它功能强大,使用简便.支持标准模式.反转模式.无镜头.无标题的放大,并可以自定义jQZoom的窗口位置和渐隐效果,修正IE6的select b ...

  5. C# Window Form解决播放amr格式音乐问题

    最近搞一个项目,需要获取微信端语音文件,下载之后发现是AMR格式的录音文件,这下把我搞晕了,C#中的4种播放模式不支持播放AMR,想到都觉得头痛,如何是好?最后找到的方案,其实也简单:windows ...

  6. wp8

     请问如何在应用内跳转到  显示  来自XX的更多应用?     Windows.System.Launcher.LaunchUriAsync(new Uri("zune:search?pu ...

  7. Struts2标签实现for循环

    感悟:但是不建议使用这种方法,按照MVC框架的思想 ,应该把业务更多放在后台.前台尽量只进行数据展示. 转自:http://blog.csdn.net/guandajian/article/detai ...

  8. C++的那些事:类的拷贝控制

    1,什么是类的拷贝控制 当我们定义一个类的时候,为了让我们定义的类类型像内置类型(char,int,double等)一样好用,我们通常需要考下面几件事: Q1:用这个类的对象去初始化另一个同类型的对象 ...

  9. 安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(四)码农网

    17. 安装 Webmin Webmin 是基于 Web 的 Linux 配置工具.它像一个中央系统,用于配置各种系统设置,比如用户.磁盘分配.服务以及 HTTP 服务器.Apache.MySQL 等 ...

  10. 前端JSON使用总结

    JSON: JavaScript Object Notation(JavaScript 对象表示法)的简称. 1. 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaS ...