[IDA*] UVa1343 The Rotation Game 旋转游戏
2018-04-03 13:49
344 查看
代码
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 */ const int mymove[8][7] = { {0,2,6,11,15,20,22}, {1,3,7,12,16,21,23}, {10,9,8,7,6,5,4}, {19,18,17,16,15,14,13}, {23,21,16,12,7,3,1}, {22,20,15,11,6,2,0}, {13,14,15,16,17,18,19}, {4,5,6,7,8,9,10} }; const int myrev[8] = { 5,4,7,6,1,0,3,2 }; const int center[8] = { 6,7,8,12,17,16,15,11 }; const int maxn = 24; const int maxans = 100; int A[24], ans[maxans]; bool finaled() { for (int i = 1; i < 8; i++) if (A[center[i]] != A[center[0]]) return false; return true; } int diff(int v) { int ans = 0; for (int i = 0; i < 8; i++) if (A[center[i]] != v) ans++; return ans; } int h() { return min(min(diff(1), diff(2)), diff(3)); } bool dfs(int d, int maxd) { if (finaled()) return true; if (d >= maxd) return false; if (d + h() > maxd) return false; for (int i = 0; i < 8; i++) { int temp = A[mymove[i][0]]; for (int j = 0; j < 6; j++) A[mymove[i][j]] = A[mymove[i][j + 1]]; A[mymove[i][6]] = temp; ans[d] = i; if (dfs(d + 1, maxd)) return true; temp = A[mymove[myrev[i]][0]]; for (int j = 0; j < 6; j++) A[mymove[myrev[i]][j]] = A[mymove[myrev[i]][j + 1]]; A[mymove[myrev[i]][6]] = temp; } return false; } int main() { while (scanf("%d", &A[0]) == 1 && A[0]) { for (int i = 1; i < maxn; i++) scanf("%d", &A[i]); memset(ans, 0, sizeof(ans)); int maxd; for (maxd = 0; maxd < maxans; maxd++) { if (dfs(0, maxd)) break; } if (!maxd) printf("No moves needed\n"); else { for (int i = 0; i < maxd; i++) printf("%c", ans[i] + 'A'); printf("\n"); } printf("%d\n", A[6]); } return 0; }
体会
测评的时候还是WA,实在找不到是哪个点有问题,算了很多样例没问题吧大概,就先发上来了。好简单的IDA*, 剪枝条件为 maxd <= h() + d(),其中此题的h()很好算,就是中间那一块,不统一的数字个数(说不清,看代码),因为每次移动顶多只能改变一个不统一数字咯。
相关文章推荐
- 例题7-12 UVA - 1343 The Rotation Game 旋转游戏(IDA*)
- UVA1343(The rotation Game|旋转游戏)=》状态空间分析+迭代加深搜
- UVa 1343 The Rotation Game(IDA*)
- Uva1343 The Rotation Game 【IDA*】【例题7-12】
- UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]
- The Rotation Game UVA - 1343(IDA* 状态空间搜索)
- uva1343(The Rotation Game)=>IDA*
- UVA_Rotation Game<旋转游戏> UVA 1343
- UVa 1343:The Rotation Game(IDA*)
- UVA 1343 The Rotation Game 【IDA*】
- UVA-1343 The Rotation Game (IDA*)
- POJ 2286 UVA 1343 The Rotation Game IDA*
- UVA 1343 - The Rotation Game
- UVa 1343 The Rotation Game
- UVa 1343 旋转游戏(dfs+IDA*)
- UVA - 1343 The Rotation Game : IDA*
- 迭代加深搜索IDA*---uva1343 the rotation game
- UVa #1343 The Rotation Game (例题7-12)
- uva 1343 The Rotation Game_wrong
- The Rotation Game (UVA - 1343)