hdu 2514 Another Eight Puzzle 图填数字 next_permutation+暴力

题目已经给出八个点以及各点的连接情况,要求两个相连的点的值不能连续,输入部分点的值,让你填其他点的值,检查能否填出已经是否有唯一解。

只有八个点,把要填的数值排序填入点中,然后检查一下就可以了。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 9;

int que[maxn], pre[maxn], pos[maxn], rec[maxn];
bool app[maxn];
int cnt = 0;
int a[17] = {1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7};
int b[17] = {2, 3, 4, 3, 5, 6, 4, 5, 6, 7, 6, 7, 6, 8, 7, 8, 8};

void check() {
	for (int i = 0; i < 17; i++)
		if (pos[a[i]] - pos[b[i]] == 1 || pos[b[i]] - pos[a[i]] == 1)
			return;
	if (cnt++ > 1)
		return;
	for (int i = 1; i <= 9; i++)
		rec[i] = pos[i];
}

int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		cnt = 0;
		memset(app, 0, sizeof(app));
		for (int j = 1; j <= 8; j++) {
			scanf("%d", &pre[j]);
			pos[j] = pre[j];
			app[pre[j]] = true;
		}
		int t = 0;
		for (int j = 1; j <= 8; j++)
			if (!app[j])
				que[t++] = j;
		do {
			int p = 0;
			for (int j = 1; j <= 8; j++) {
				if (pre[j] == 0)
					pos[j] = que[p++];
//				printf("%d ", pos[j]);
			}
//			printf("\n");
			check();
		} while (next_permutation(que, que + t));
		printf("Case %d:", i);
		if (cnt == 1)
			for (int j = 1; j <= 8; j++)
				printf(" %d", rec[j]);
		else if (cnt > 1)
			printf(" Not unique");
		else
			printf(" No answer");
		printf("\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值