UVA 1533 - Moving Pegs (广搜+hash判重)
2013-12-28 19:49
127 查看
Venture MFG Company, Inc. has made a game board. This game board has 15 holes and these holes are filled with pegs except one hole. A peg can jump over one or more consecutive peg s to the nearest empty hole along
the straight line. As a peg jump over the pegs you remove them from the board. In the following figure, the peg at the hole number 12 or the peg at the hole number 14 can jump to the empty hole number 5. If the peg at the hole number 12 is moved then the peg
at the hole number 8 is removed. Instead, if the peg at the hole number 14 is moved then the peg at the hole number 9 is removed.
Write a program which find a shortest sequence of moving pegs to leave the last peg in the hole that was initially empty. If such a sequence does not exist the program should write a message ``IMPOSSIBLE".
which means an empty hole number.
A peg movement consists of a pair o f integers separated by a space. The first integer of the pair denotes the hole number of the peg that is moving, and the second integer denotes a destination (empty) hole number.
If there are multiple solutions, output the lexicographically smallest one.
题意:一个跳棋,给定一个空位。求最少步数字典序最小使得跳到最后棋盘只剩一个旗子且在空位上。
思路:BFS + hash判重,状态为2^15。妥妥的。。
代码:
BFS + hash判重:
附加一个打表过的:
the straight line. As a peg jump over the pegs you remove them from the board. In the following figure, the peg at the hole number 12 or the peg at the hole number 14 can jump to the empty hole number 5. If the peg at the hole number 12 is moved then the peg
at the hole number 8 is removed. Instead, if the peg at the hole number 14 is moved then the peg at the hole number 9 is removed.
Write a program which find a shortest sequence of moving pegs to leave the last peg in the hole that was initially empty. If such a sequence does not exist the program should write a message ``IMPOSSIBLE".
Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case is a single integerwhich means an empty hole number.
Output
For each test case, the first line of the output file contains an integer which is the number of jumps in a shortest sequence of moving pegs. In the second line of the output file, print a sequence of peg movements.A peg movement consists of a pair o f integers separated by a space. The first integer of the pair denotes the hole number of the peg that is moving, and the second integer denotes a destination (empty) hole number.
If there are multiple solutions, output the lexicographically smallest one.
Sample Input
1 5
Sample Output
10 12 5 3 8 15 12 6 13 7 9 1 7 10 8 7 9 11 14 14 5
题意:一个跳棋,给定一个空位。求最少步数字典序最小使得跳到最后棋盘只剩一个旗子且在空位上。
思路:BFS + hash判重,状态为2^15。妥妥的。。
代码:
BFS + hash判重:
#include <stdio.h> #include <string.h> #include <queue> #define INF 0x3f3f3f3f using namespace std; const int N = 20; const int MAXN = 66666; const int next [6] = { {-1, -1, -1, -1, 2, 3}, {-1, 1, -1, 3, 4, 5}, {1, -1, 2, -1, 5, 6}, {-1, 2, -1, 5, 7, 8}, {2, 3, 4, 6, 8, 9}, {3, -1, 5, -1, 9, 10}, {-1, 4, -1, 8, 11, 12}, {4, 5, 7, 9, 12, 13}, {5, 6, 8, 10, 13, 14}, {6, -1, 9, -1, 14, 15}, {-1, 7, -1, 12, -1, -1}, {7, 8, 11, 13, -1, -1}, {8, 9, 12, 14, -1, -1}, {9, 10, 13, 15, -1, -1}, {10, -1, 14, -1, -1, -1} }; int T, n, vis[MAXN]; struct State { int s ; int way [2]; int wayn; int num; }p; void init() { scanf("%d", &n); memset(vis, INF, sizeof(vis)); p.wayn = 0; p.num = 14; memset(p.way, 0, sizeof(p.way)); } int hash(int *s) { int sum = 0; for (int i = 1; i <= 15; i ++) { if (s[i]) sum = sum * 2 + 1; else sum = sum * 2; } return sum; } void tra(State p, queue<State> &Q) { for (int i = 1; i <= 15; i ++) { if (p.s[i] == 1) { for (int j = 0; j < 6; j ++) { if (next[i - 1][j] != -1 && p.s[next[i - 1][j]] == 1) { int t = next[i - 1][j]; State q = p; q.s[i] = 0; q.s[t] = 0; while (next[t - 1][j] != -1 && p.s[next[t - 1][j]] == 1) { q.num--; q.s[next[t - 1][j]] = 0; t = next[t - 1][j]; } if (next[t - 1][j] == -1) continue; q.s[next[t - 1][j]] = 1; q.num--; q.way[q.wayn][0] = i; q.way[q.wayn++][1] = next[t - 1][j]; int num = hash(q.s); if (vis[num] > q.wayn) { vis[num] = q.wayn; Q.push(q); } } } } } } bool bfs() { for (int i = 1; i <= 15; i++) { if (i == n) p.s[i] = 0; else p.s[i] = 1; } queue<State>Q; Q.push(p); while (!Q.empty()) { p = Q.front(); Q.pop(); if (p.num == 1 && p.s == 1) return true; tra(p, Q); } return false; } void solve() { if (!bfs()) { printf("IMPOSSIBLE\n"); return; } printf("%d\n%d %d", p.wayn, p.way[0][0], p.way[0][1]); for (int i = 1; i < p.wayn; i++) printf(" %d %d", p.way[i][0], p.way[i][1]); printf("\n"); } int main() { scanf("%d", &T); while (T--) { init(); solve(); } return 0; }
附加一个打表过的:
#include <stdio.h> #include <string.h> const int ansn[15] = {9, 9, 9, 9, 10, 9, 9, 10, 10, 9, 9, 9, 9, 9, 9}; const int ans[15][25] = { {4, 1, 6, 4, 11, 2, 14, 11, 15, 6, 1, 10, 10, 7, 11, 4, 4, 1}, {7, 2, 1, 4, 10, 1, 14, 2, 1, 7, 11, 14, 15, 13, 13, 4, 7, 2}, {10, 3, 1, 6, 7, 1, 12, 3, 1, 10, 14, 12, 11, 13, 13, 6, 15, 3}, {1, 4, 6, 1, 13, 6, 10, 3, 11, 13, 3, 12, 15, 11, 11, 2, 1, 4}, {12, 5, 3, 8, 15, 12, 6, 13, 7, 9, 1, 7, 10, 8, 7, 9, 11, 14, 14, 5}, {1, 6, 4, 1, 13, 4, 7, 2, 15, 13, 2, 14, 11, 15, 15, 3, 1, 6}, {1, 7, 6, 1, 11, 4, 9, 7, 14, 11, 11, 2, 15, 6, 6, 4, 1, 7}, {3, 8, 12, 5, 15, 3, 13, 6, 1, 10, 2, 9, 11, 2, 14, 5, 2, 9, 10, 8}, {2, 9, 11, 2, 12, 5, 3, 8, 13, 4, 1, 7, 14, 5, 15, 3, 3, 8, 7, 9}, {1, 10, 4, 1, 11, 4, 4, 6, 12, 5, 10, 8, 15, 12, 12, 3, 1, 10}, {4, 11, 1, 4, 10, 1, 14, 2, 1, 7, 11, 14, 15, 13, 13, 4, 4, 11}, {14, 12, 2, 14, 7, 2, 1, 4, 15, 13, 3, 15, 11, 14, 4, 13, 15, 12}, {4, 13, 1, 4, 11, 2, 13, 11, 15, 13, 2, 14, 3, 15, 15, 12, 11, 13}, {11, 14, 2, 11, 3, 12, 10, 3, 1, 6, 11, 13, 15, 12, 6, 13, 12, 14}, {6, 15, 1, 6, 7, 1, 12, 3, 1, 10, 15, 12, 11, 13, 13, 6, 6, 15} }; int T, n; int main() { scanf("%d", &T); while (T--) { scanf("%d", &n); if (n < 1 || n > 15) printf("IMPOSSIBLE\n"); else { printf("%d\n%d %d", ansn[n - 1], ans[n - 1][0], ans[n - 1][1]); for (int i = 1; i < ansn[n - 1]; i ++) printf(" %d %d", ans[n - 1][i * 2], ans[n - 1][i * 2 + 1]); printf("\n"); } } return 0; }
相关文章推荐
- Moving Pegs UVA - 1533
- UVa 1533:Moving Pegs(迭代加深搜索)
- UVa #1533 Moving Pegs (习题7-12)
- UVA - 1533 Moving Pegs
- UVA-1533 Moving Pegs (路径寻找问题)
- UVA - 1533 (Moving Pegs)
- UVa 1533 Moving Pegs 解题报告(暴力+码力)
- UVA 1533 - Moving Pegs 状态压缩 + bfs
- UVa 1533 - Moving Pegs (隐式图搜索 + 状态压缩)
- UVA 1533 Moving Pegs(bfs+hash)
- uva 1533 - Moving Pegs(BFS)
- 1533 Moving Pegs[暴力+打表]
- uva 1533(bfs+状态压缩)
- UVa 10499 - The Land of Justice
- UVA 10795 A Different Task(汉诺塔 递归))
- UVA 11388 - GCD LCM
- UVA10006- Carmichael Numbers
- uva 1160 - X-Plosives(并查集)
- UVa 1363 (数论 数列求和) Joseph's Problem
- UVA 662 Fast Food(DP)