题意 求迷宫中从a的位置到r的位置须要的最少时间  经过'.'方格须要1s  经过‘x’方格须要两秒  '#'表示墙

因为有1s和2s两种情况  须要在基础迷宫bfs上加些推断

令到达每一个点的时间初始为无穷大  当从一个点到达该点用的时间比他本来的时间小时  更新这个点的时间并将这个点入队  扫描全然图就得到答案咯

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. using namespace std;
  5.  
  6. const int N = 205;
  7. char mat[N][N];
  8. int time[N][N], sx, sy;
  9. int dx[4] = {0, 0, -1, 1};
  10. int dy[4] = { -1, 1, 0, 0};
  11. struct grid
  12. {
  13. int x, y;
  14. grid(int xx = 0, int yy = 0): x(xx), y(yy) {}
  15. };
  16.  
  17. void bfs()
  18. {
  19. memset(time, 0x3f, sizeof(time));
  20. time[sx][sy] = 0;
  21. queue<grid> g;
  22. g.push(grid(sx, sy));
  23.  
  24. while(!g.empty())
  25. {
  26. grid cur = g.front();
  27. g.pop();
  28. int cx = cur.x, cy = cur.y, ct = time[cx][cy];
  29. for(int i = 0; i < 4; ++i)
  30. {
  31. int nx = cx + dx[i], ny = cy + dy[i];
  32. if(mat[nx][ny] && mat[nx][ny] != '#')
  33. {
  34. int tt = ct + 1;
  35. if(mat[cx][cy] == 'x') ++tt;
  36. if(tt < time[nx][ny])
  37. {
  38. time[nx][ny] = tt;
  39. g.push(grid(nx, ny));
  40. }
  41. }
  42. }
  43. }
  44. }
  45.  
  46. int main()
  47. {
  48. int n, m, ex, ey;
  49. while (~scanf("%d%d", &n, &m))
  50. {
  51. memset(mat, 0, sizeof(mat));
  52. for(int i = 1; i <= n; ++i)
  53. scanf("%s", mat[i] + 1);
  54. for(int i = 1; i <= n; ++i)
  55. for(int j = 1; j <= m; ++j)
  56. if(mat[i][j] == 'a') sx = i, sy = j;
  57. else if(mat[i][j] == 'r') ex = i, ey = j;
  58. bfs();
  59. if(time[ex][ey] != time[0][0])
  60. printf("%d\n", time[ex][ey]);
  61. else
  62. printf("Poor ANGEL has to stay in the prison all his life.\n");
  63. }
  64.  
  65. return 0;
  66. }

ZOJ 1649 Rescue(有敌人迷宫BFS)的更多相关文章

  1. zoj 1649 Rescue (BFS)(转载)

    又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...

  2. zoj 1649 Rescue

    BFS..第一次使用C++ STL的队列来写广搜. #include<stdio.h> #include<string.h> #include<math.h> #i ...

  3. BFS zoj 1649

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 //hnldyhy(303882171) 11:12:46 // z ...

  4. ZOJ 1649:Rescue(BFS)

    Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...

  5. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  6. ZOJ 649 Rescue(优先队列+bfs)

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

  7. zoj 1649 bfs

    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...

  8. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  9. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

随机推荐

  1. [Apple开发者帐户帮助]三、创建证书(4)创建Safari签名证书

    您的Safari扩展程序必须由Apple颁发的证书签名,您可以在开发者帐户中创建和下载该证书. 在“ 证书”,“标识符和配置文件”中,从左侧的弹出菜单中选择“Safari扩展”. 在“证书”下,选择“ ...

  2. [Apple开发者帐户帮助]二、管理你的团队(3)删除团队成员

    如果您已加入Apple开发者计划,您将在App Store Connect中管理团队成员.有关详细信息,请转到App Store Connect帮助中的添加和编辑用户. 如果您已加入Apple Dev ...

  3. Java.Cas4.0客户端接入配置

    1.本示例中使用cas4.0+cas-client-core3.4版本,ide则是myeclipse2014 blue版本,mvn使用3.9版本 2.注意事项:请先实现破解cas-client-cor ...

  4. HDU 1054 Hungary

    Strategic Game Problem Description Bob enjoys playing computer games, especially strategic games, bu ...

  5. Spring Boot (13) druid监控

    druid druid是和tomcat jdbc一样优秀的连接池,出自阿里巴巴.除了连接池,druid哈hi有一个很实用的监控功能. pom.xml 添加了以下依赖后,会自动用druid连接池替代默认 ...

  6. Linux的那点事

    1.重启nginx服务器 注意,修改了nginx配置文件后最好先检查一下修改过的配置文件是否正确,以免重启后Nginx出现错误影响服务器稳定运行. 判断Nginx配置是否正确命令如下: nginx - ...

  7. node函数buf.readDoubleBE详解

    offset {Number} 0 noAssert {Boolean} 默认:false 返回:{Number} 从该 Buffer 指定的带有特定尾数格式(readDoubleBE() 返回一个较 ...

  8. Table is specified twice, both as a target for 'UPDATE' and as a separate source

    UPDATE Bins b SET b.ShopSn =’111201611111168706’ WHERE b.Id IN (SELECT b.Id FROM Bins b JOIN BinInve ...

  9. 安卓代码迁移:Make.exe: *** [libs/armabi-v7a/gdbserver] Error 1

    解决办法1:安装ndk和eclipse修改为x86操作系统 解决办法2:降低更换NDK版本

  10. 实现Android-JNI本地C++调试

    1.       原文链接:NDK单步调试方法 如有问题或者版权要求,请拜访原作者或者通知本人. 最近为了性能需求,开始搞JNI,白手起搞真心不容易.中间差点崩溃了好几次,最终总算得到一点心得. JN ...