题目链接

单向bfs就是水题

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. using namespace std;
  7. const int INF = 0x3f3f3f3f;
  8. const int Max = + ;
  9. struct Node
  10. {
  11. int x, y;
  12. };
  13. int g[Max][Max];
  14. int vis[Max][Max];
  15. int n, sx, sy, ex, ey;
  16. int gx[] = {-, -, -, -, , , , };
  17. int gy[] = {-, -, , , , , -, -};
  18. bool in_bound(int x, int y)
  19. {
  20. if (x >= && y >= && x < n && y < n)
  21. return true;
  22. return false;
  23. }
  24. int bfs(int sx, int sy)
  25. {
  26. Node node, temp;
  27. node.x = sx;
  28. node.y = sy;
  29. vis[sx][sy] = ;
  30. queue<Node> q;
  31. q.push(node);
  32. while (!q.empty())
  33. {
  34. node = q.front();
  35. q.pop();
  36. if (node.x == ex && node.y == ey)
  37. return vis[ex][ey];
  38. for (int i = ; i < ; i++)
  39. {
  40. int fx = node.x + gx[i];
  41. int fy = node.y + gy[i];
  42. if (in_bound(fx, fy) && vis[fx][fy] > vis[node.x][node.y] + )
  43. {
  44. temp.x = fx;
  45. temp.y = fy;
  46. vis[fx][fy] = vis[node.x][node.y] + ;
  47. q.push(temp);
  48. }
  49. }
  50. }
  51. return -;
  52. }
  53. int main()
  54. {
  55. int test;
  56. scanf("%d", &test);
  57. while (test--)
  58. {
  59. scanf("%d", &n);
  60. scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  61. memset(vis, INF, sizeof(vis));
  62. printf("%d\n", bfs(sx, sy));
  63. }
  64. return ;
  65. }

单向bfs

做这题主要是学着写双向bfs;

分别从起点和终点开始搜,如果重合即找到

从这个博客学会的

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. using namespace std;
  7. const int INF = 0x3f3f3f3f;
  8. const int Max = + ;
  9. struct Node
  10. {
  11. int x, y;
  12. bool step;
  13. };
  14. // 这个step的意思之前没搞明白,他其实就是指的 在某一步下可以走到点
  15. //开始讲 start.step设为true,因此在只能走一次 8 个点,然后8个点都是第一步走的,然后把最后一个点的step设为true,当你走第二部时候,就是 do { 以这个点扩展 8 步} while ( !current.step) step控制了层数。
  16. int g[Max][Max];
  17. int vis[Max][Max];
  18. int n, sx, sy, ex, ey;
  19. int gx[] = {-, -, -, -, , , , };
  20. int gy[] = {-, -, , , , , -, -};
  21. bool in_bound(int x, int y)
  22. {
  23. if (x >= && y >= && x < n && y < n)
  24. return true;
  25. return false;
  26. }
  27. int bfs()
  28. {
  29. if (sx == ex && sy == ey)
  30. return ;
  31. Node start, finish;
  32. start.x = sx;
  33. start.y = sy;
  34. start.step = true;
  35. finish.x = ex;
  36. finish.y = ey;
  37. finish.step = true;
  38. vis[sx][sy] = ;
  39. vis[ex][ey] = ;
  40. queue<Node> frontSearch;
  41. queue<Node> backSearch;
  42. int fstep = , bstep = ;
  43. frontSearch.push(start);
  44. backSearch.push(finish);
  45. Node current;
  46. while (!frontSearch.empty() || !backSearch.empty())
  47. {
  48. if (!frontSearch.empty())
  49. {
  50. do
  51. {
  52. current = frontSearch.front();
  53. frontSearch.pop();
  54. for (int i = ; i < ; i++)
  55. {
  56. int fx = current.x + gx[i];
  57. int fy = current.y + gy[i];
  58. if (in_bound(fx, fy))
  59. {
  60. if (vis[fx][fy] == )
  61. {
  62. return fstep + bstep + ;
  63. }
  64. if (!vis[fx][fy])
  65. {
  66. vis[fx][fy] = ;
  67. Node temp;
  68. temp.x = fx;
  69. temp.y = fy;
  70. temp.step = false;
  71. frontSearch.push(temp);
  72. }
  73. }
  74. }
  75. }while(current.step == false);
  76. fstep++;
  77. current = frontSearch.front();
  78. frontSearch.pop();
  79. current.step = true; // 为了让最后队列中最后一个数step为true,先将队首拿出来,修改step,然后在入队
  80. frontSearch.push(current);
  81. }
  82.  
  83. if (!backSearch.empty())
  84. {
  85. do
  86. {
  87. current = backSearch.front();
  88. backSearch.pop();
  89. for (int i = ; i < ; i++)
  90. {
  91. int fx = current.x + gx[i];
  92. int fy = current.y + gy[i];
  93. if (in_bound(fx, fy))
  94. {
  95. if (vis[fx][fy] == )
  96. {
  97. return bstep + fstep + ;
  98. }
  99. if (!vis[fx][fy])
  100. {
  101. vis[fx][fy] = ;
  102. Node temp;
  103. temp.x = fx;
  104. temp.y = fy;
  105. temp.step = false;
  106. backSearch.push(temp);
  107. }
  108. }
  109. }
  110. } while(current.step == false);
  111. bstep++;
  112. current = backSearch.front();
  113. backSearch.pop();
  114. current.step = true;
  115. backSearch.push(current);
  116. }
  117. }
  118. return -;
  119. }
  120. int main()
  121. {
  122. int test;
  123. scanf("%d", &test);
  124. while (test--)
  125. {
  126. scanf("%d", &n);
  127. scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  128. memset(vis, , sizeof(vis));
  129. printf("%d\n", bfs());
  130. }
  131. return ;
  132. }

第二种 双向bfs写法:

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. using namespace std;
  7. const int INF = 0x3f3f3f3f;
  8. const int Max = + ;
  9. struct Node
  10. {
  11. int x, y;
  12. bool step;
  13. };
  14. int g[Max][Max];
  15. int fvis[Max][Max], bvis[Max][Max];
  16. int n, sx, sy, ex, ey;
  17. int gx[] = {-, -, -, -, , , , };
  18. int gy[] = {-, -, , , , , -, -};
  19. bool in_bound(int x, int y)
  20. {
  21. if (x >= && y >= && x < n && y < n)
  22. return true;
  23. return false;
  24. }
  25. int bfs()
  26. {
  27. if (sx == ex && sy == ey)
  28. return ;
  29. Node start, finish;
  30. start.x = sx;
  31. start.y = sy;
  32. start.step = true;
  33. finish.x = ex;
  34. finish.y = ey;
  35. finish.step = true;
  36. fvis[sx][sy] = ;
  37. bvis[ex][ey] = ;
  38. queue<Node> frontSearch;
  39. queue<Node> backSearch;
  40. int fstep = , bstep = ;
  41. frontSearch.push(start);
  42. backSearch.push(finish);
  43. Node current;
  44. while (!frontSearch.empty() || !backSearch.empty())
  45. {
  46. int frontSize = (int) frontSearch.size();
  47. while (frontSize--) // 直接将这一个队 全都 拿出来更新,就相当于上一中的step一样,控制搜索的层次
  48. {
  49. current = frontSearch.front();
  50. frontSearch.pop();
  51.  
  52. for (int i = ; i < ; i++)
  53. {
  54. int fx = current.x + gx[i];
  55. int fy = current.y + gy[i];
  56. if (in_bound(fx, fy))
  57. {
  58. if (bvis[fx][fy] != -) // 如果 倒着搜 已经搜到了,返回
  59. return fvis[current.x][current.y] + + bvis[fx][fy];
  60. if (fvis[fx][fy] == -) //否则正着+1
  61. {
  62. Node temp;
  63. temp.x = fx;
  64. temp.y = fy;
  65. fvis[fx][fy] = fvis[current.x][current.y] + ;
  66. frontSearch.push(temp);
  67. }
  68. }
  69. }
  70. }
  71. int backSize = (int) backSearch.size();
  72. while (backSize--)
  73. {
  74. current = backSearch.front();
  75. backSearch.pop();
  76.  
  77. for (int i = ; i < ; i++)
  78. {
  79. int fx = current.x + gx[i];
  80. int fy = current.y + gy[i];
  81. if (in_bound(fx, fy))
  82. {
  83. if (fvis[fx][fy] != -)
  84. {
  85. return bvis[current.x][current.y] + + fvis[fx][fy];
  86. }
  87. if (bvis[fx][fy] == -)
  88. {
  89. Node temp;
  90. temp.x = fx;
  91. temp.y = fy;
  92. bvis[fx][fy] = bvis[current.x][current.y] + ;
  93. backSearch.push(temp);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. return -;
  100. }
  101. int main()
  102. {
  103. int test;
  104. scanf("%d", &test);
  105. while (test--)
  106. {
  107. scanf("%d", &n);
  108. scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  109. memset(fvis, -, sizeof(fvis));
  110. memset(bvis, -, sizeof(bvis));
  111. printf("%d\n", bfs());
  112. }
  113. return ;
  114. }

双向bfs 方法二

  1.  

POJ1915Knight Moves(单向BFS + 双向BFS)的更多相关文章

  1. UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)

    题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...

  2. POJ 3126 Prime Path 解题报告(BFS & 双向BFS)

    题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...

  3. UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)

    题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...

  4. POJ1915 BFS&双向BFS

    俩月前写的普通BFS #include <cstdio> #include <iostream> #include <cstring> #include <q ...

  5. bfs(双向bfs加三维数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others)     ...

  6. BFS、双向BFS和A*

    BFS.双向BFS和A* Table of Contents 1. BFS 2. 双向BFS 3. A*算法 光说不练是无用的.我们从广为人知的POJ 2243这道题谈起:题目大意:给定一个起点和一个 ...

  7. 双向BFS和启发式搜索的应用

    题目链接 P5507 机关 题意简述   有12个旋钮,每个旋钮开始时处于状态 \(1\) ~ \(4\) ,每次操作可以往规定方向转动一个旋钮 (\(1\Rightarrow2\Rightarrow ...

  8. 洛谷 P1379 八数码难题(map && 双向bfs)

    题目传送门 解题思路: 一道bfs,本题最难的一点就是如何储存已经被访问过的状态,如果直接开一个bool数组,空间肯定会炸,所以我们要用另一个数据结构存,STL大法好,用map来存,直接AC. AC代 ...

  9. POJ 1915-Knight Moves (单向BFS &amp;&amp; 双向BFS 比)

    主题链接:Knight Moves 题意:8个方向的 马跳式走法 ,已知起点 和终点,求最短路 研究了一下双向BFS,不是非常难,和普通的BFS一样.双向BFS只是是从 起点和终点同一时候開始搜索,可 ...

随机推荐

  1. 记、基于react-router的单页应用

    现在用react写单页应用基本上都是用react-router做前端路由了吧!最近在使用react-router的过程中遇到了不少问题,在这里总结一下. 浏览器url react-router默认提供 ...

  2. Crowdsourcing(众包)

    群众外包(英语:crowdsourcing)是互联网带来的新的生产组织形式.<连线>(Wired)杂志记者Jeff Howe于2006年发明的一个专业术语,用来描述一种新的商业模式,即企业 ...

  3. matlab 中randn randi rand randsrc的用法以及区别

    1,rand 生成均匀分布的伪随机数.分布在(0~1)之间 主要语法:rand(m,n)生成m行n列的均匀分布的伪随机数                      rand(m,n,'double') ...

  4. 1031MVCC和事务浅析

    转自 http://blog.csdn.net/sofia1217/article/details/50778906 关于MVCC浅析,有些难度http://xuebinbin212.blog.163 ...

  5. SVG的使用

    一,svg可以在浏览器中直接打开 二,在html使用<img/>标签引用 三,直接在html中使用svg标签 四,作为css背景 SVG支持ie9+ ,chrome 33.0+,firef ...

  6. Qt5.3.0 for Android开发环境配置

    1.去官网下载Qt5.3.0 for Android 2.去http://developer.android.com下载Ndk 和SDk            3.去http://ant.apache ...

  7. C/C++中NULL的涵义

    参考:百度知道NULL表示空指针,用于表示一个无效的指针,它的值为0(早期C语言的实现中可能有非0空指针,现在已经不用).对指针置NULL即标记指针无效,避免“野指针”的恶果.NULL在C/C++标准 ...

  8. [转]使用Sencha Ext JS 6打造通用应用程序

    原文地址:http://www.uedsc.com/using-sencha-ext-js-6-to-build-universal-apps.html 在Sencha和整个Ext JS团队的支持下, ...

  9. maven2-snapshot快照库和release发布库的应用

    在项目中应用snapshot和release库,应用snapshot和release库达到不同环境下发布不同的版本的目的,首先看一个pom文件的定义: <project> <mode ...

  10. iis Server Error in '/' Application

    1.开始-运行-cmd-输入cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727-回车-aspnet_regiis.exe -i 回车 2.如果不是检查链接 ...