当前位置:   article > 正文

八数码问题(广搜)_一、问题描述 一个九宫格,有八个数字1-8已经确定位置,剩下一个空格以0表示,0可以

一、问题描述 一个九宫格,有八个数字1-8已经确定位置,剩下一个空格以0表示,0可以

题目:

描述:

在九宫格里放在1到8共8个数字还有一个是空格,与空格相邻的数字可以移动到空格的位置,问给定的状态最少需要几步能到达目标状态(用0表示空格):
1 2 3
4 5 6
7 8 0

输入:

输入一个给定的状态。

输出:

输出到达目标状态的最小步数。不能到达时输出-1。

输入样例:

1 2 3
4 0 6
7 5 8

输出样例:

2

 

思想:广搜,将每一次扩展后的节点的状态用一个9位十进制数来表示,目标状态是123456780,判重用set,之后就是全裸的广搜;

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <queue>
  5. #include <set>
  6. #include <stdlib.h>
  7. using namespace std;
  8. const int maxn = 400000;
  9. const int status = 123456780;
  10. int vis[maxn];
  11. int dir[4] = {-1,1,-3,3};
  12. char mapp[20];
  13. char tmp[20];
  14. struct state
  15. {
  16. int new_status;
  17. int step;
  18. };
  19. int new_status(int statuss,int n)
  20. {
  21. //char tmp[10];
  22. int zero_loc;
  23. sprintf(tmp,"%09d",statuss);
  24. for(int i = 0;i < 9;i ++)
  25. {
  26. if(tmp[i] == '0')
  27. zero_loc = i;
  28. }
  29. switch (n){
  30. case -3: //第一行
  31. if(zero_loc - 3 < 0) return -1;
  32. else
  33. {
  34. tmp[zero_loc] = tmp[zero_loc + n];
  35. tmp[zero_loc + n] = '0';
  36. }
  37. break;
  38. case 3://第3行
  39. if(zero_loc + 3 > 8)
  40. return -1;
  41. else {
  42. tmp[zero_loc] = tmp[zero_loc + n];
  43. tmp[zero_loc + n] = '0';
  44. }
  45. break;
  46. case -1: //第一列
  47. if(zero_loc % 3 == 0)
  48. return -1;
  49. else
  50. {
  51. tmp[zero_loc] = tmp[zero_loc + n];
  52. tmp[zero_loc + n] = '0';
  53. }
  54. break;
  55. case 1: //第三列
  56. if(zero_loc % 3 == 2)
  57. return -1;
  58. else
  59. {
  60. tmp[zero_loc] = tmp[zero_loc + n];
  61. tmp[zero_loc + n] = '0';
  62. }
  63. break;
  64. }
  65. return atoi(tmp);
  66. }
  67. int bfs(state st)
  68. {
  69. queue <state> que;
  70. state now ,next;
  71. set<int>expand;
  72. st.step = 0;
  73. que.push(st);
  74. expand.insert(st.new_status);
  75. while(!que.empty())
  76. {
  77. now = que.front();
  78. que.pop();
  79. if(now.new_status == status)
  80. {
  81. return now.step;
  82. }
  83. for(int i = 0;i < 4;i ++)
  84. {
  85. next.new_status = new_status(now.new_status,dir[i]);
  86. next.step = now.step + 1;
  87. if(next.new_status == -1) //不满足
  88. continue;
  89. if(expand.find(next.new_status) != expand.end())//判重
  90. continue;
  91. expand.insert(next.new_status);
  92. que.push(next);
  93. }
  94. }
  95. return -1;
  96. }
  97. int main()
  98. {
  99. int mmap[3][3];
  100. int t = 0;
  101. for(int i = 0;i < 3;i ++)
  102. for(int j = 0;j < 3;j ++)
  103. {
  104. cin >> mmap[i][j];
  105. mapp[t ++] = mmap[i][j] + '0';
  106. }
  107. //cout << mapp << endl;
  108. state st;
  109. st.new_status = atoi(mapp);
  110. int step = bfs(st);
  111. cout << step << endl;
  112. //cout << tmp << endl;
  113. return 0;
  114. }
  115. /*被我们qiwang老师聪明的才智所折服*/

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/786740
推荐阅读
相关标签
  

闽ICP备14008679号