fork download
  1. /*
  2. Author:yew1eb Current Time: 2014.2.19
  3.  
  4. hdu1043 Eight
  5. 因为状态总数不多,只有不到40万种(9!),因此可以从目标节点开始,
  6. 进行一遍彻底的广搜,找出全部有解状态到目标节点的路径。
  7.  
  8. 状态判重:
  9. cantor展开
  10.  
  11. 注意有多解!!Special Judge
  12. */
  13.  
  14. #include <cstdio>
  15. #include <cstring>
  16. #include <queue>
  17. #include <string>
  18. #include <algorithm>
  19. using namespace std;
  20. const int MAXN = 400000;
  21. const int n = 9;
  22. struct node {
  23. int s[9];
  24. int loc;
  25. int hf;
  26. string path;
  27. } st;
  28.  
  29.  
  30. int fac[9]= {1,1,2,6,24,120,720,5040,40320};
  31.  
  32. int cantor(int *a)
  33. {
  34. int ans, i, j, tmp;
  35. ans = 0;
  36. for(i=0; i<n; ++i) {
  37. tmp = 0;
  38. for(j=i+1; j<n; ++j) if(a[j]<a[i]) tmp++;
  39. ans += fac[n-i-1]*tmp;
  40. }
  41. return ans + 1;
  42. }
  43.  
  44. string path[MAXN];
  45. bool vis[MAXN];
  46.  
  47. int dir[4][2]= {{-1,0},{1,0},{0,-1},{0,1}}; //u,d,l,r
  48. char index[5]="durl";//和上面的要相反,因为是反向搜索:便于输出
  49.  
  50. void bfs()
  51. {
  52. int i, j;
  53. queue<node> q;
  54. node cur, next;
  55. for(i=1; i<n; ++i) st.s[i-1] = i % n;
  56. st.loc = 8;
  57. st.path = "";
  58. st.hf = cantor(st.s);
  59. memset(vis, 0, sizeof vis );
  60. vis[st.hf] = 1;
  61. q.push(st);
  62. int tx, ty, dx, dy;
  63. while(!q.empty()) {
  64. cur = q.front();
  65. q.pop();
  66. tx = cur.loc / 3;
  67. ty = cur.loc % 3;
  68. for(i=0; i<4; ++i) {
  69. dx = tx + dir[i][0];
  70. dy = ty + dir[i][1];
  71. if(dx<0||dx>2||dy<0||dy>2) continue;
  72. next = cur;
  73. next.loc = dx*3 + dy;
  74. next.s[cur.loc] = next.s[next.loc];
  75. next.s[next.loc] = 0;
  76. next.hf = cantor(next.s);
  77. if(!vis[next.hf]) {
  78. vis[next.hf] = 1;
  79. next.path = index[i] + next.path;
  80. q.push(next);
  81. path[next.hf] = next.path;
  82. }
  83. }
  84. }
  85. }
  86.  
  87. int main()
  88. {
  89. char str[100];
  90. int len, i, j, cnt;
  91. node t;
  92. bfs();
  93. while(gets(str)) {
  94. len = strlen(str);
  95. cnt = 0;
  96. for(i=0; i<len; ++i) {
  97. if(str[i]>='1' && str[i]<='8')
  98. t.s[cnt++] = str[i] - '0';
  99. else if(str[i] == 'x') {
  100. t.s[cnt] = 0;
  101. t.loc = cnt++;
  102. }
  103. }
  104.  
  105. t.hf = cantor(t.s);
  106. if(vis[t.hf]) {
  107. printf("%s\n",path[t.hf].c_str());
  108. } else {
  109. printf("unsolvable\n");
  110.  
  111. }
  112. }
  113. return 0;
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:48:13: error: ‘char index [5]’ redeclared as different kind of symbol
 char index[5]="durl";//和上面的要相反,因为是反向搜索:便于输出
             ^
In file included from /usr/include/c++/4.8/cstring:42:0,
                 from prog.cpp:15:
/usr/include/string.h:478:1: error: previous declaration of ‘const char* index(const char*, int)’
 index (const char *__s, int __c) __THROW
 ^
prog.cpp: In function ‘void bfs()’:
prog.cpp:79:36: error: invalid types ‘<unresolved overloaded function type>[int]’ for array subscript
                 next.path = index[i] + next.path;
                                    ^
prog.cpp:52:12: warning: unused variable ‘j’ [-Wunused-variable]
     int i, j;
            ^
prog.cpp: In function ‘int main()’:
prog.cpp:93:11: warning: ‘char* gets(char*)’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
     while(gets(str)) {
           ^
prog.cpp:93:19: warning: ‘char* gets(char*)’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
     while(gets(str)) {
                   ^
prog.cpp:90:17: warning: unused variable ‘j’ [-Wunused-variable]
     int len, i, j, cnt;
                 ^
stdout
Standard output is empty