题目传送门1 2

题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0

分析:八数码经典问题。POJ是一次,HDOJ是多次。因为康托展开还不会,也写不了什么,HDOJ需要从最后的状态逆向搜索,这样才不会超时。判重康托展开,哈希也可。

POJ

  1. //#include <bits/stdc++.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<string>
  5. #include<stack>
  6. #include<queue>
  7. #include <cstring>
  8. #include<map>
  9. #include<stdio.h>
  10. #include<stdlib.h>
  11. #include<ctype.h>
  12. #include<time.h>
  13. #include<math.h>
  14. using namespace std;
  15.  
  16. const int N = 362880 + 5;
  17. const int MOD = 1e6 + 7;
  18. int dx[4] = {-1, 1, 0, 0};
  19. int dy[4] = {0, 0, -1, 1};
  20. char dir[4] = {'u', 'd', 'l', 'r'};
  21. struct Point {
  22. int s, d;
  23. string str;
  24. Point () {}
  25. Point (int s, int d, string str) : s (s), d (d), str (str) {}
  26. };
  27. struct Hash_table {
  28. struct Edge {
  29. int v, nex;
  30. }edge[MOD];
  31. int head[MOD], e;
  32. void init(void) {
  33. memset (head, -1, sizeof (head));
  34. e = 0;
  35. }
  36. bool insert(int x) {
  37. int u = (x % MOD + MOD) % MOD;
  38. for (int i=head[u]; ~i; i=edge[i].nex) {
  39. if (edge[i].v == x) return false;
  40. }
  41. edge[e].v = x; edge[e].nex = head[u];
  42. head[u] = e++;
  43. return true;
  44. }
  45. }ha;
  46. int vis[N], fact[9];
  47.  
  48. void decode(int x, int *b) {
  49. for (int i=8; i>=0; --i) {
  50. b[i] = x % 10;
  51. x /= 10;
  52. }
  53. }
  54.  
  55. int encode(int *b) {
  56. int ret = 0;
  57. for (int i=0; i<9; ++i) {
  58. ret = ret * 10 + b[i];
  59. }
  60. return ret;
  61. }
  62.  
  63. int find_0(int *b) {
  64. for (int i=0; i<9; ++i) {
  65. if (b[i] == 0) return i;
  66. }
  67. return -1;
  68. }
  69.  
  70. bool check(int x, int y) {
  71. if (x < 0 || x >= 3 || y < 0 || y >= 3) return false;
  72. else return true;
  73. }
  74.  
  75. void print(int *b) {
  76. for (int i=0; i<9; ++i) {
  77. printf ("%d ", b[i]);
  78. if (i == 2 || i == 5 || i == 8) puts ("");
  79. }
  80. }
  81.  
  82. void init(void) {
  83. fact[0] = 1;
  84. for (int i=1; i<9; ++i) fact[i] = fact[i-1] * i;
  85. memset (vis, false, sizeof (vis));
  86. }
  87.  
  88. bool can_insert(int *b) {
  89. int code = 0;
  90. for (int i=0; i<9; ++i) {
  91. int cnt = 0;
  92. for (int j=i+1; j<9; ++j) if (b[j] < b[i]) cnt++;
  93. code += fact[8-i] * cnt;
  94. }
  95. if (vis[code]) return false;
  96. else {
  97. vis[code] = true;
  98. return true;
  99. }
  100. }
  101.  
  102. void BFS(int *a) {
  103. init ();
  104. int ans[9] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
  105. int s = encode (a);
  106. queue<Point> que; que.push (Point (s, 0, ""));
  107. while (!que.empty ()) {
  108. Point u = que.front (); que.pop ();
  109. int b[9];
  110. decode (u.s, b);
  111. if (memcmp (ans, b, sizeof (b)) == 0) {
  112. int len = u.str.length ();
  113. for (int i=0; i<len; ++i) {
  114. printf ("%c", u.str[i]);
  115. }
  116. puts ("");
  117. return ;
  118. }
  119. int p = find_0 (b);
  120. int x = p / 3, y = p % 3;
  121. for (int i=0; i<4; ++i) {
  122. int tx = x + dx[i], ty = y + dy[i];
  123. if (!check (tx, ty)) continue;
  124. int p2 = tx * 3 + ty;
  125. int t[9];
  126. memcpy (t, b, sizeof (b));
  127. t[p] = t[p2]; t[p2] = 0;
  128. int v = encode (t);
  129. //if (!ha.insert (v)) continue;
  130. if (!can_insert (t)) continue;
  131. que.push (Point (v, u.d + 1, u.str + dir[i]));
  132. }
  133. }
  134. puts ("unsolvable");
  135. }
  136.  
  137. int main(void) {
  138. char c[9];
  139. int a[9];
  140. while (scanf ("%c %c %c %c %c %c %c %c %c", &c[0], &c[1], &c[2], &c[3], &c[4], &c[5], &c[6], &c[7], &c[8]) == 9) {
  141. for (int i=0; i<9; ++i) {
  142. if (c[i] >= '1' && c[i] <= '9') {
  143. a[i] = c[i] - '0';
  144. }
  145. else a[i] = 0;
  146. }
  147. BFS (a);
  148. }
  149.  
  150. return 0;
  151. }

  

HDOJ

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 362880 + 5;
  5. int dx[4] = {-1, 1, 0, 0};
  6. int dy[4] = {0, 0, -1, 1};
  7. char dir[4] = {'d', 'u', 'r', 'l'};
  8. struct Point {
  9. int s;
  10. int b[9];
  11. };
  12. struct Ans {
  13. char dir;
  14. int fa;
  15. }ans[N];
  16. int fact[9];
  17.  
  18. int find_0(int *b) {
  19. for (int i=0; i<9; ++i) {
  20. if (b[i] == 9) return i;
  21. }
  22. return -1;
  23. }
  24.  
  25. bool check(int x, int y) {
  26. if (x < 0 || x >= 3 || y < 0 || y >= 3) return false;
  27. else return true;
  28. }
  29.  
  30. void init(void) {
  31. fact[0] = 1;
  32. for (int i=1; i<9; ++i) fact[i] = fact[i-1] * i;
  33. for (int i=0; i<N; ++i) ans[i].fa = -1;
  34. }
  35.  
  36. int Cantor(int *b) {
  37. int code = 0;
  38. for (int i=0; i<9; ++i) {
  39. int cnt = 0;
  40. for (int j=i+1; j<9; ++j) if (b[j] < b[i]) cnt++;
  41. code += fact[8-i] * cnt;
  42. }
  43. return code;
  44. }
  45.  
  46. void BFS() {
  47. init ();
  48. Point sta;
  49. for (int i=0; i<9; ++i) {
  50. sta.b[i] = i + 1;
  51. }
  52. sta.s = 0; ans[sta.s].fa = 0;
  53. queue<Point> que; que.push (sta);
  54. while (!que.empty ()) {
  55. Point u = que.front (); que.pop ();
  56. int p = find_0 (u.b);
  57. int x = p / 3, y = p % 3;
  58. for (int i=0; i<4; ++i) {
  59. Point v = u;
  60. int tx = x + dx[i], ty = y + dy[i];
  61. if (!check (tx, ty)) continue;
  62. int p2 = tx * 3 + ty;
  63. swap (v.b[p], v.b[p2]);
  64. v.s = Cantor (v.b);
  65. if (ans[v.s].fa != -1) continue;
  66. ans[v.s].dir = dir[i];
  67. ans[v.s].fa = u.s;
  68. que.push (v);
  69. }
  70. }
  71. }
  72.  
  73. int main(void) {
  74. BFS ();
  75. char c[55];
  76. int a[9];
  77. while (gets (c)) {
  78. int j = 0;
  79. for (int i=0; c[i]; ++i) {
  80. if (c[i] >= '0' && c[i] <= '8') {
  81. a[j++] = c[i] - '0';
  82. }
  83. else if (c[i] == 'x') a[j++] = 9;
  84. }
  85. int s = Cantor (a);
  86. if (ans[s].fa == -1) puts ("unsolvable");
  87. else {
  88. while (s != 0) {
  89. printf ("%c", ans[s].dir);
  90. s = ans[s].fa;
  91. }
  92. puts ("");
  93. }
  94. }
  95.  
  96. return 0;
  97. }

  

BFS(八数码) POJ 1077 || HDOJ 1043 Eight的更多相关文章

  1. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  2. POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3

    http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...

  3. POJ 1077 HDU 1043 Eight (IDA*)

    题意就不用再说明了吧......如此经典 之前想用双向广搜.a*来写,但总觉得无力,现在用IDA*感觉其他的解法都弱爆了..............想法活跃,时间,空间消耗很小,给它跪了 启发式搜索关 ...

  4. Poj 1077 eight(BFS+全序列Hash解八数码问题)

    一.题意 经典的八数码问题,有人说不做此题人生不完整,哈哈.给出一个含数字1~8和字母x的3 * 3矩阵,如: 1  2  X            3 4  6            7  5  8 ...

  5. hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...

  6. poj 1077 Eight (八数码问题——A*+cantor展开+奇偶剪枝)

    题目来源: http://poj.org/problem?id=1077 题目大意: 给你一个由1到8和x组成的3*3矩阵,x每次可以上下左右四个方向交换.求一条路径,得到12345678x这样的矩阵 ...

  7. HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  8. HDU 1043 Eight (BFS&#183;八数码&#183;康托展开)

    题意  输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数  即这个排列在全部排列中的字典序  然后就是基础的BFS了 #include <bits/stdc++.h ...

  9. HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

随机推荐

  1. Vector_h

    #ifndef VECTOR_H #define VECTOR_H #include <algorithm> template<typename Object> class V ...

  2. NYOJ题目112指数运算

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAs0AAAIICAIAAAAaCETRAAAgAElEQVR4nO3drW7jWtwv4PcmwnMhxb ...

  3. 2.2 顺序容器-list

    list(双向链表) 1) *  :包含头文件list **:不支持随机存取:增删元素时间是常数,只需要修改指针 2)成员函数 *  :vector的成员函数list基本都有 **:以下是部分独有成员 ...

  4. Linux(CentOS)系统下设置nginx开机自启动

    Nginx 是一个很强大的高性能Web和反向代理服务器.下面介绍在linux下安装后,如何设置开机自启动.首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令:vi ...

  5. 20145206邹京儒《Java程序设计》实验报告一:Java开发环境的熟悉(Windows+IDEA)

    20145206<Java程序设计>实验报告一:Java开发环境的熟悉(Windows+IDEA) 实验内容及步骤 1.使用JDK编译.运行简单的Java程序: 建立实验目录: 在IDEA ...

  6. AJAX 三级联动

    新的封装类 <?php class DBDA { public $host="localhost";//服务器地址 public $uid="root"; ...

  7. 微信支付 - V3支付问题

    参考资料:http://www.2cto.com/weixin/201506/407690.html   1.微信公众号支付出错: 当前页面的URL未注册: get_brand_wcpay_reque ...

  8. Newtonsoft.Json(Json.Net)学习笔记(转)

    概述 Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库,通过Nuget获取.(查看原文) 下面是Json序列化和反序列化的简单封装: /// <summary&g ...

  9. I-number

    以下是真坑爹题目: 此题必须输出前导零,否则永远a不了 I-number Time Limit: 5000MS Memory limit: 65536K 题目描述 The I-number of x ...

  10. 解决mysql无法插入中文数据及插入后显示乱码的问题

    (1)废话不多说就是使用mysql数据库的时候无法输入中文,可以输入中文后显示的又是乱码!! (2开始解决问题: 第一步:找到安装mysql的目录找到 my.ini 文件: 第二步:使用记事本打开my ...