The Rotation Game UVA - 1343(IDA* 状态空间搜索)
2017-04-27 21:47
351 查看
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int line[8][7] = // lines E~H are computed with the help of rev[] //用一系列“指针”将一维数组构造为特殊形态数据结构储存数据//修改时通过存储的特殊形态“指针”查询一维数组修改其内数据 { { 0, 2, 6,11,15,20,22 }, // A { 1, 3, 8,12,17,21,23 }, // B { 10, 9, 8, 7, 6, 5, 4 }, // C { 19,18,17,16,15,14,13 }, // D }; const int rev[8] = { 5, 4, 7, 6, 1, 0, 3, 2 }; // reverse lines of each line const int center[8] = { 6, 7, 8, 11, 12, 15, 16, 17 }; int a[25]; //状态 bool final() { for (int i = 0; i < 8; i++) if (a[center[i]] != a[center[0]]) return false; return true; } char ans[1000]; inline int diff(int n) { int ans = 0; for (int i = 0; i < 8; i++) { if (a[center[i]] != n) ans++; } return ans; } inline int h() { return min(min(diff(1), diff(2)), diff(3)); //中间8个格子中最少的相同格子数 } inline void move(int dir) { int tmp = a[line[dir][0]]; for (int i = 0; i < 6; i++) { a[line[dir][i]] = a[line[dir][i + 1]]; } a[line[dir][6]] = tmp; } bool dfs(int d, int maxd) { if (final()) { ans[d] = '\0'; //以字符串格式符输出,需在字符串数组尾部添加'\0' printf("%s\n", ans); return true; } if (d + h() > maxd) return false; for (int i = 0; i < 8; i++) { ans[d] = 'A' + i; //打印路径,用数组存储解 move(i); if(dfs(d+1, maxd)) return true; move(rev[i]); //借助rev数组实现回溯操作 } return false; } int main() { for (int i = 4; i < 8; i++) { for (int j = 0; j < 7; j++) { line[i][j] = line[rev[i]][6 - j]; //补全line数组 } } while (scanf("%d", &a[0]) && a[0]) { for (int i = 1; i < 24; i++) scanf("%d", &a[i]); if (final()) { printf("No moves needed\n"); } else { for (int maxd = 1; ; maxd++) //IDA*不知最大深度,但一定有解时,不设max_ans if (dfs(0, maxd)) break; } printf("%d\n", a[6]);// } }
相关文章推荐
- Uva1343 The Rotation Game 【IDA*】【例题7-12】
- UVA 1343 The Rotation Game 【IDA*】
- UVa 1343:The Rotation Game(IDA*)
- uva1343(The Rotation Game)=>IDA*
- UVa 1343 The Rotation Game(IDA*)
- POJ 2286 UVA 1343 The Rotation Game IDA*
- 例题7-12 UVA - 1343 The Rotation Game 旋转游戏(IDA*)
- [IDA*] UVa1343 The Rotation Game 旋转游戏
- UVA-1343 The Rotation Game (IDA*)
- UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]
- 【C++心路历程34】【HDU1667】【POJ2286】【UVA1343紫书210例题】the rotation game
- UVa 1343 The Rotation Game
- UVA 1343(p210)----The Rotation Game
- The Rotation Game UVA - 1343
- UVA - 1343 The Rotation Game
- The Rotation Game (UVA - 1343)
- UVA 1343 The Rotation Game
- UVA - 1343 The Rotation Game : IDA*
- 迭代加深搜索IDA*---uva1343 the rotation game
- UVa1343 - The Rotation Game