很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M);

UVA 11624 写的比较挫

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. using namespace std;
  6. struct node{
  7. int ft;
  8. int sta;
  9. }flo[][];
  10. int vis[][];
  11. struct person{
  12. int x,y,t,fx,fy;
  13. };
  14. int R,C;
  15. int dir[][]={{,},{,-},{,},{-,}};
  16. typedef pair<int,int> pill;
  17. queue<pill> v;
  18. queue<person> q;
  19. void init()
  20. {
  21. memset(vis,,sizeof vis);
  22. while (!v.empty()){
  23. pill x=v.front();
  24. v.pop();
  25. for (int i=;i<;i++){
  26. int nx=x.first+dir[i][];
  27. int ny=x.second+dir[i][];
  28. int tmp=flo[x.first][x.second].ft+;;
  29. if (nx< || ny< || nx>=R || ny>=C) continue;
  30. if (flo[nx][ny].ft>= && flo[nx][ny].ft<=tmp || flo[nx][ny].sta==) continue;
  31. flo[nx][ny].ft=flo[x.first][x.second].ft+;
  32. pill b=make_pair(nx,ny);
  33. if (!vis[nx][ny])
  34. v.push(b);
  35. vis[nx][ny]=;
  36. }
  37. }
  38. }
  39. int bfs(person x)
  40. {
  41. memset(vis,,sizeof vis);
  42. while (!q.empty()) q.pop();
  43. q.push(x);
  44. int s=<<;
  45. while (!q.empty()){
  46. person u=q.front();
  47. q.pop();
  48. if (u.t>=s) continue;
  49. if (u.x== || u.y== || u.x==R- || u.y==C-) {s=u.t;break;}
  50. for (int i=;i<;i++){
  51. int xx=u.x+dir[i][];
  52. int yy=u.y+dir[i][];
  53. if (xx< || yy< || xx>=R || yy>=C) continue;
  54. if (xx==u.fx && yy==u.fy) continue;
  55. if (flo[xx][yy].sta!= || flo[xx][yy].ft>= && flo[xx][yy].ft<=u.t+) continue;
  56. person b=(person){xx,yy,u.t+,u.x,u.y};
  57. if (!vis[xx][yy]) q.push(b);
  58. vis[xx][yy]=;
  59. }
  60. }
  61. return s;
  62. }
  63. int main()
  64. {
  65. int t,sx,sy;char ch;
  66. scanf("%d",&t);
  67. while (t--){
  68. while (!v.empty()) v.pop();
  69. scanf("%d%d",&R,&C);
  70. getchar();
  71. for (int i=;i<R;i++){
  72. for (int j=;j<C;j++){
  73. scanf("%c",&ch);
  74. //cout<<ch<<endl;
  75. if (ch=='.') {flo[i][j].sta=;flo[i][j].ft=-;}
  76. else if (ch=='#'){flo[i][j].sta=;flo[i][j].ft=-;}
  77. else if (ch=='F'){
  78. flo[i][j].sta=flo[i][j].ft=;
  79. pill a;a.first=i;a.second=j;v.push(a);
  80. }
  81. else if (ch=='J') sx=i,sy=j;
  82. }
  83. getchar();
  84. }
  85. init();
  86. person a=(person){sx,sy,,-,-};
  87. int ans=bfs(a);
  88. if (ans<(<<)) printf("%d\n",ans+);
  89. else puts("IMPOSSIBLE");
  90. }
  91. return ;
  92. }

UVA 10047

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. using namespace std;
  6. int R,C;
  7. int vis[][][][];
  8. int mat[][];
  9. int sx,sy,ex,ey;
  10. int dir[][]={{-,},{,},{,},{,-}};
  11. struct t1{
  12. int x,y,d,c,t;
  13. };
  14. int bfs(t1 a)
  15. {
  16. queue<t1> q;
  17. q.push(a);
  18. memset(vis,,sizeof vis);
  19. while (!q.empty()){
  20. t1 u=q.front();
  21. q.pop();
  22. if (u.x==ex && u.y==ey && u.c==){
  23. //cout<<" pass "<<u.x<<" "<<u.y<<endl;
  24. return u.t;
  25. }
  26. vis[u.x][u.y][u.d][u.c]=;
  27. int nd=u.d+;
  28. if (nd>) nd=;
  29. t1 nx=u;
  30. nx.d=nd;
  31. nx.t=u.t+;
  32. if (!vis[nx.x][nx.y][nx.d][nx.c]) q.push(nx);
  33. vis[nx.x][nx.y][nx.d][nx.c]=;
  34. nd=u.d-;
  35. if (nd<) nd=;
  36. nx=u; nx.d=nd; nx.t=u.t+;
  37. if (!vis[nx.x][nx.y][nx.d][nx.c]) q.push(nx);
  38. vis[nx.x][nx.y][nx.d][nx.c]=;
  39. int xx=u.x+dir[u.d][];
  40. int yy=u.y+dir[u.d][];
  41. if (xx< || yy< || xx>=R || yy>=C) continue;
  42. if (mat[xx][yy]==) continue;
  43. int nc=u.c+;
  44. if (nc>) nc=;
  45. t1 b=(t1){xx,yy,u.d,nc,u.t+};
  46. if (!vis[b.x][b.y][b.d][b.c]) q.push(b);
  47. vis[b.x][b.y][b.d][b.c]=;
  48. }
  49. return -;
  50. }
  51. int main()
  52. {
  53. char ch;
  54. int kase=;
  55. while (scanf("%d%d",&R,&C)){
  56. if (R==) break;
  57. getchar();
  58. memset(mat,,sizeof mat);
  59. for (int i=;i<R;i++){
  60. for (int j=;j<C;j++){
  61. ch=getchar();
  62. if (ch!='#') mat[i][j]=;
  63. if (ch=='S') sx=i,sy=j;
  64. if (ch=='T') ex=i,ey=j;
  65. }
  66. getchar();
  67. }
  68. //cout<<ex<<" exy "<<ey<<endl;
  69. t1 a=(t1){sx,sy,,,};
  70. int ans=bfs(a);
  71. if (kase) puts("");
  72. printf("Case #%d\n",++kase);
  73. if (ans==-)puts("destination not reachable");
  74. else printf("minimum time = %d sec\n",ans);
  75. }
  76. return ;
  77. }

UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题的更多相关文章

  1. UVA 11624 Fire!(两次BFS+记录最小着火时间)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  2. UVA 11624 Fire!【两点BFS】

    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...

  3. UVa 11624,两次BFS

    题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...

  4. Fire! UVA - 11624 (两步bfs)

    题目链接 题意 人要从迷宫走出去,火会向四个方向同时扩散 分析 两步bfs,先出火到达各地时的时间(设初始时间为0,人每走一步为1s,在着一步内火可以向四周可触及的方向同时扩散),然后在bfs人,人能 ...

  5. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  6. BFS(两点搜索) UVA 11624 Fire!

    题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...

  7. E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)

    E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...

  8. UVA 11624 - Fire! 图BFS

    看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...

  9. UVa 11624 (BFS) Fire!

    也是一个走迷宫的问题,不过又有了点变化. 这里迷宫里有若干把火,而且火每秒也是向四个方向蔓延的.问人是否能走出迷宫. 我用了两遍BFS,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间. 第 ...

随机推荐

  1. Spring任务调度实战之Quartz Cron Trigger

    在Quartz中除了使用最简单的Simple Trigger以外,也可以使用类似Linux上Cron作业的CronTrigger的方式来运行Job,下面是一个小例子: 1. 首先是一个任务类,这个类没 ...

  2. leetcode844 Backspace String Compare

    """ Given two strings S and T, return if they are equal when both are typed into empt ...

  3. docker-jenkins SSH Publishers时踩的坑

    source相对路径问题,不是 /var/jenkins_home/workspace/build-renren/target/renren-fast.war  或环境变量,而是  target/re ...

  4. HDU1880 魔咒词典

    题目大意:对应的输入多行,每行两个字符串,两个字符串互相映射.接下来询问的时候,如果这个字符串出现过,输出其对应的字符串. 分析:二重哈希来判断字符串是否存在,输出其对应的字符串就行.二重哈希的入门题 ...

  5. C++中数据类型表示范围

    32位机器环境下结果如下: Type Size 数值范围 无值型void 0 byte 无值域 布尔型bool 1 byte true   false 有符号短整型short [int] /signe ...

  6. Andorid 搭建 Linux服务器(一)

    00.搭建环境 电脑系统:MacOS下Win7虚拟机 手机型号:红米Note5A 手机系统:MIUI10开发版 软件: SuperSU      --通过recovery刷入,管理ROOT权限 Bus ...

  7. 困惑我的x++和++x;

    刚学习C语言时X++和++X非常不解 目前有了新的领悟 1.X++ int x=0; int z=x++; 此时z?x? 这个问题可以分两步思考 第一步:先把x的值赋予z,此时z=x=0; 第二步:x ...

  8. 004、Java的多行注释

    代码如下: package TIANPAN; public class TestDemo { public static void main(String args[]) { /* * 我是多行注释 ...

  9. 12 MySQL存储过程与函数

    存储过程和函数     存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句的集合.     调用存储过程和函数可以简化应用开发人员的工作,减少数据在数据库和应用服务器之间的传输,提高数据处理 ...

  10. LeetCode1217 玩筹码(贪心)

    题目: 数轴上放置了一些筹码,每个筹码的位置存在数组 chips 当中. 你可以对 任何筹码 执行下面两种操作之一(不限操作次数,0 次也可以): 将第 i 个筹码向左或者右移动 2 个单位,代价为 ...