UVa 1533 - Moving Pegs (隐式图搜索 + 状态压缩)

题意

15个格子组成的三角形,一开始给出一个空位置,问能不能最后剩下一个棋子并且正好在这个位置上。

思路

一开始想用回溯,不过这跳来跳去的实在没办法表示。后来看到题目说固定的15个位置,想到每个棋子能走的地方都可以列出来。那就干脆都列出来一一判断呗。

这题是一个隐式图遍历问题。

15个格子,可以状态压缩一下。

先把原始的状态图作为基准,然后就开始跳吧,如果能达到新的状态,入队,直到找到结果或者队伍为空

我知道位运算的优先级很低,没想到比等号还低,漏了一个括号从早上调到现在TAT

代码

  
  
  
  
  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <string>
  13. #include <map>
  14. #include <cmath>
  15. #define LL long long
  16. #define SZ(x) (int)x.size()
  17. #define Lowbit(x) ((x) & (-x))
  18. #define MP(a, b) make_pair(a, b)
  19. #define MS(arr, num) memset(arr, num, sizeof(arr))
  20. #define PB push_back
  21. #define F first
  22. #define S second
  23. #define ROP freopen("input.txt", "r", stdin);
  24. #define MID(a, b) (a + ((b - a) >> 1))
  25. #define LC rt << 1, l, mid
  26. #define RC rt << 1|1, mid + 1, r
  27. #define LRT rt << 1
  28. #define RRT rt << 1|1
  29. #define BitCount(x) __builtin_popcount(x)
  30. const double PI = acos(-1.0);
  31. const int INF = 0x3f3f3f3f;
  32. using namespace std;
  33. const int MAXN = 100 + 10;
  34. const int MOD = 1e9 + 7;
  35. const int dir[][6] = { {-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,2,3}, {-1,1,-1,3,4,5}, {1,-1,2,-1,5,6},
  36. {-1,2,-1,5,7,8}, {2,3,4,6,8,9}, {3,-1,5,-1,9,10},
  37. {-1,4,-1,8,11,12}, {4,5,7,9,12,13}, {5,6,8,10,13,14}
  38. , {6,-1,9,-1,14,15}, {-1,7,-1,12,-1,-1}, {7,8,11,13,-1,-1}
  39. , {8,9,12,14,-1,-1}, {9,10,13,15,-1,-1}, {10,-1,14,-1,-1,-1} };
  40. typedef pair<int, int> pii;
  41. typedef vector<int>::iterator viti;
  42. typedef vector<pii>::iterator vitii;
  43. struct STATE
  44. {
  45. int state, rem, path[MAXN][2];
  46. int pos;
  47. };
  48. set<int> mp;
  49. int emp;
  50. queue<STATE> Q;
  51. STATE ans;
  52. void Solve()
  53. {
  54. int iniState = 0;
  55. STATE st;
  56. for (int i = 1; i <= 15; i++)
  57. if (i != emp) iniState |= (1 << i);
  58. st.state = iniState; st.rem = 14; st.pos = 0;
  59. Q.push(st);
  60. while (!Q.empty())
  61. {
  62. STATE cur = Q.front(); Q.pop();
  63. const int tmpState = cur.state;
  64. for (int i = 1; i <= 15; i++)
  65. {
  66. if ((tmpState & (1 << i)) == 0) continue; //如果这个位置是黑色
  67. for (int j = 0; j < 6; j++)
  68. {
  69. STATE newState = cur;
  70. newState.state ^= (1 << i);
  71. if (dir[i][j] != -1 && (tmpState & (1 << dir[i][j])))
  72. {
  73. int t = dir[i][j]; //接下来找这个方向白色的
  74. newState.state -= (1 << t);
  75. while (dir[t][j] != -1 && (tmpState & (1 << dir[t][j])))
  76. {
  77. newState.state -= (1 << dir[t][j]); newState.rem--;
  78. t = dir[t][j];
  79. }
  80. if (dir[t][j] == -1) continue;
  81. newState.state |= (1 << dir[t][j]); newState.rem--;
  82. newState.path[newState.pos][0] = i; newState.path[newState.pos++][1] = dir[t][j];
  83. if (mp.count(newState.state)) continue;
  84. mp.insert(newState.state);
  85. Q.push(newState);
  86. //if (newState.rem == 1) printf("%d\n", dir[t][j]);
  87. if (newState.rem == 1 && newState.state & (1 << emp)) //如果正好跳到空白点而且只剩一个
  88. {
  89. ans = newState;
  90. return;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. void Init()
  98. {
  99. while (!Q.empty()) Q.pop();
  100. mp.clear();
  101. ans.pos = INF;
  102. }
  103. int main()
  104. {
  105. //ROP;
  106. int T, i, j;
  107. scanf("%d", &T);
  108. while (T--)
  109. {
  110. Init();
  111. scanf("%d", &emp);
  112. if (emp < 1 || emp > 15) puts("IMPOSSIBLE");
  113. Solve();
  114. if (ans.pos == INF) puts("IMPOSSIBLE");
  115. else
  116. {
  117. printf("%d\n", ans.pos);
  118. for (i = 0; i < ans.pos; i++)
  119. {
  120. if (i) printf(" %d %d", ans.path[i][0], ans.path[i][1]);
  121. else printf("%d %d", ans.path[i][0], ans.path[i][1]);
  122. }
  123. }
  124. puts("");
  125. }
  126. return 0;
  127. }

你可能感兴趣的:(ACM,uva)