UVA 1533 - Moving Pegs (广搜+hash判重)

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.

\epsfbox{p2093.eps}

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 integer which 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[N][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[N];
	int way[N][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[n] == 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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值