Solution - UVA1343

题目传送门

Solution.#

IDA* + 模拟

对于这道题,我们的估价函数为中间 8 个数数值数量最多的数字个数与 8 的差值。然后再加上每种移动的模拟即可。没什么思维难度,纯靠模拟能力。

Code.#

有点长,将就看吧。

#include <bits/stdc++.h>
//#define int long long
using namespace std;
template <typename T>
void read (T &x) {
    x = 0; T f = 1;
    char ch = getchar ();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar ();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar ();
    }
    x *= f;
}
char For_Print[25];
template <typename T>
void write (T x) {
    if (x == 0) { putchar ('0'); return; }
    if (x < 0) { putchar ('-'); x = -x; }
    int poi = 0;
    while (x) {
        For_Print[++poi] = x % 10 + '0';
        x /= 10;
    }
    while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
    write (x); putchar (ch);
}

int a[11][11];
int now[11][11];
//以下为四种操作的函数
void A() {
	int t = a[1][3];
	for (int i = 1; i < 7; i++) {
		a[i][3] = a[i + 1][3];
	}
	a[7][3] = t;
}
void B() {
	int t = a[1][5];
	for (int i = 1; i < 7; i++) {
		a[i][5] = a[i + 1][5];
	}
	a[7][5] = t;
}
void C() {
	int t = a[3][7];
	for (int i = 7; i >= 2; i--) {
		a[3][i] = a[3][i - 1];
	}
	a[3][1] = t;
}
void D() {
	int t = a[5][7];
	for (int i = 7; i >= 2; i--) {
		a[5][i] = a[5][i - 1];
	}
	a[5][1] = t;
}
void E() {
	int t = a[7][5];
	for (int i = 7; i >= 2; i--) {
		a[i][5] = a[i - 1][5];
	}
	a[1][5] = t;
}
void F() {
	int t = a[7][3];
	for (int i = 7; i >= 2; i--) {
		a[i][3] = a[i - 1][3];
	}
	a[1][3] = t;
}
void G() {
	int t = a[5][1];
	for (int i = 1; i < 7; i++) {
		a[5][i] = a[5][i + 1];
	}
	a[5][7] = t;
}
void H() {
	int t = a[3][1];
	for (int i = 1; i < 7; i++) {
		a[3][i] = a[3][i + 1];
	}
	a[3][7] = t;
}
int dep;
int A_star() {
	int t = 0;
	int b[4] = {};
	b[a[3][3]]++, b[a[3][4]]++, b[a[3][5]]++;
	b[a[4][3]]++, b[a[4][5]]++;
	b[a[5][3]]++, b[a[5][4]]++, b[a[5][5]]++;
	return 8 - max(max(b[1], b[2]), b[3]);
}
char ans[10005];
bool IDA_DFS(int t) {
	int dif = A_star();
	if (dif == 0) {
		return true;	
	}
	if (dif + t - 1 > dep) return false;//A*优化
	A();
	ans[t] = 'A';
	if (IDA_DFS(t + 1)) return true;
	F();
	B();
	ans[t] = 'B';
	if (IDA_DFS(t + 1)) return true;
	E();
	C();
	ans[t] = 'C';
	if (IDA_DFS(t + 1)) return true;
	H();
	D();
	ans[t] = 'D';
	if (IDA_DFS(t + 1)) return true;
	G();
	E();
	ans[t] = 'E';
	if (IDA_DFS(t + 1)) return true;
	B();
	F();
	ans[t] = 'F';
	if (IDA_DFS(t + 1)) return true;
	A();
	G();
	ans[t] = 'G';
	if (IDA_DFS(t + 1)) return true;
	D();
	H();
	ans[t] = 'H';
	if (IDA_DFS(t + 1)) return true;
	C();
	return false;
}
signed main() {
	while (scanf("%d", &a[1][3]) != EOF) {
		if (a[1][3] == 0) return 0;
		read(a[1][5]);
		read(a[2][3]), read(a[2][5]);
		for (int i = 1; i <= 7; i++) read(a[3][i]);
		read(a[4][3]), read(a[4][5]);
		for (int i = 1; i <= 7; i++) read(a[5][i]);
		read(a[6][3]), read(a[6][5]);
		read(a[7][3]), read(a[7][5]);
		for (int i = 1; i <= 7; i++) {
			for (int j = 1; j <= 7; j++) {
				now[i][j] = a[i][j];
			}
		}
		if (A_star() == 0) {
			puts("No moves needed");
			print(a[3][3], '\n');
		} else {
			for (dep = 1; ; dep++) {//枚举深度
				for (int i = 1; i <= 7; i++) {
					for (int j = 1; j <= 7; j++) {
						a[i][j] = now[i][j];
					}
				}
				if (IDA_DFS(1)) {
					for (int i = 1; i <= dep; i++) {
						putchar(ans[i]);
					}
					putchar('\n');
					print(a[3][3], '\n');
					break;
				}
			}	
		}
		
	}
    return 0;
}
posted @   cqbzjyh  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示
主题色彩