HDU 1667 百练 1288 POJ 2286

本文分享了一道北京大学暑期课程中的编程练习题,题目要求使用迭代加深搜索算法解决拼图游戏的问题。文章提供了完整的C++代码实现,并通过爱神博客的链接分享了更多背景资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

北大暑期课练习题,愚蠢的我企图暴力深搜,然而正解是 迭代加深搜索

附爱神博客链接http://blog.youkuaiyun.com/acm_cxlove/article/details/7745575

//高山仰止,景行行止。虽不能至,心向往之。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
#include<list>
#include<numeric>
#include<functional>
#define ll long long
#define MP make_pair
#define rep(a,b,c) for(int a=(b);a<(c);a++)
#define rerep(a,b,c) for(int a=(b);a>(c);a--)
#define mem(a,b) memset(a,b,sizeof(a))
#define lf else if
#define P puts("ppppppppppppppppppp")
using namespace std;
template<class T> T f_max(T a, T b){ return a > b ? a : b; }
template<class T> T f_min(T a, T b){ return a < b ? a : b; }
template<class T> T f_abs(T a){ return a > 0 ? a : -a; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T>T lcm(T a, T b){ return a / gcd(a, b)*b; }
template<class T> void swap(T *a, T *b){ T c; c = a; a = b; b = c; }
const int N = 24;
const int MAXN = 1000005;
const int MAX = 10005;
const int Inf = 1000000009;
const double precision = 1e-8;
const int hashsize = 1000003;
int s[N];
int flag;
int key[8] = { 6, 7, 8, 11, 12, 15, 16, 17 };
int road[MAXN];
int rev[8] = { 5, 4, 7, 6, 1, 0, 3, 2 };
int dir[8][7] = { { 0, 2, 6, 11, 15, 20, 22 },
{ 1, 3, 8, 12, 17, 21, 23 },
{ 10, 9, 8, 7, 6, 5, 4 },
{ 19, 18, 17, 16, 15, 14, 13 },
{ 23, 21, 17, 12, 8, 3, 1 },
{ 22, 20, 15, 11, 6, 2, 0 },
{ 13, 14, 15, 16, 17, 18, 19 },
{ 4, 5, 6, 7, 8, 9, 10 } };
void init(){
	flag = 0;
}
bool data_in(){
	if (~scanf("%d", &s[0])&&s[0]!=0){
		rep(i, 1, 24){
			scanf("%d", &s[i]);
		}
		return true;
	}
	return false;
}
int get_h(int a[]){
	int c3, c1, c2;
	c3 = c1 = c2 = 0;
	rep(i, 0, 8){
		if (a[key[i]] == 3)
			c3++;
		else if (a[key[i]] == 1)
			c1++;
		else
			c2++;
	}
	return 8 - f_max(f_max(c3, c1), c2);
}
void mov(int a[], int ins){
	int t = a[dir[ins][0]];
	rep(i, 0, 6)
		a[dir[ins][i]] = a[dir[ins][i + 1]];
	a[dir[ins][6]] = t;
}
void dfs(int a[], int depth,int tot, int father){
	if (get_h(a) > depth)
		return;
	if (flag)
		return;
	if (depth == 0 && get_h(a) == 0){
		flag = 1; 
		rep(i, 0, tot)
			printf("%c", 'A' + road[i]);
		puts("");
		printf("%d\n", a[6]);
		return;
	}
	rep(i, 0, 8){
		if (rev[i] == father)
			continue;
		int b[N];
		memcpy(b, a, N*sizeof(int));
//		rep(j, 0, N)
//			b[j] = a[j];
		mov(b, i);
		road[tot] = i;
		dfs(b, depth - 1, tot + 1, i);
	}
}
void solve(){
	if (get_h(s)==0){
		puts("No moves needed");
		printf("%d\n", s[6]);
		return;
	}
	init();
	for (int depth = get_h(s);; depth++){
		dfs(s, depth, 0, -1);
		if (flag)
			break;
	}
}
int main(){

	while (data_in()){
		solve(); 
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值