hdu 1667 IDA*

ID*算法解决数独问题
本文介绍了一种使用ID*算法解决数独问题的方法,包括详细的代码实现和检查函数,以及通过递归旋转操作来搜索最优解的过程。

第一道IDA*。。。。。。

给链接:点击打开链接

AC代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

int num[7][7];
int maxdeep;
char record[1000];

bool check(){
	int temp = num[2][2];
	for( int i = 2; i <= 4; i++ ){
		if( num[2][i] != temp ){
			return false;
		}
	}
	if( num[3][2] != temp || num[3][4] != temp ){
		return false;
	}
	for( int i = 2; i <= 4; i++ ){
		if( num[4][i] != temp ){
			return false;
		}
	}
	return true;
}

int h(){
	int cnt[4] = { 0 };
	cnt[num[2][2]]++;
	cnt[num[2][3]]++;
	cnt[num[2][4]]++;
	cnt[num[3][2]]++;
	cnt[num[3][4]]++;
	cnt[num[4][2]]++;
	cnt[num[4][3]]++;
	cnt[num[4][4]]++;
	int ans = 0;
	for( int i = 1; i <= 3; i++ ){
		ans = max( ans, cnt[i] );
	}
	return 8 - ans;
}

int rotationA(){
	int temp = num[0][2];
	for( int i = 0; i < 6; i++ ){
		num[i][2] = num[i+1][2];
	}
	num[6][2] = temp;
	return 0;
}

int rotationB(){
	int temp = num[0][4];
	for( int i = 0; i < 6; i++ ){
		num[i][4] = num[i+1][4];
	}
	num[6][4] = temp;
	return 0;
}

int rotationC(){
	int temp = num[2][6];
	for( int i = 6; i > 0; i-- ){
		num[2][i] = num[2][i-1];
	}
	num[2][0] = temp;
	return 0;
}

int rotationD(){
	int temp = num[4][6];
	for( int i = 6; i > 0; i-- ){
		num[4][i] = num[4][i-1];
	}
	num[4][0] = temp;
	return 0;
}

int rotationE(){
	int temp = num[6][4];
	for( int i = 6; i > 0; i-- ){
		num[i][4] = num[i-1][4];
	}
	num[0][4] = temp;
	return 0;
}

int rotationF(){
	int temp = num[6][2];
	for( int i = 6; i > 0; i-- ){
		num[i][2] = num[i-1][2];
	}
	num[0][2] = temp;
	return 0;
}

int rotationG(){
	int temp = num[4][0];
	for( int i = 0; i < 6; i++ ){
		num[4][i] = num[4][i+1];
	}
	num[4][6] = temp;
	return 0;
}

int rotationH(){
	int temp = num[2][0];
	for( int i = 0; i < 6; i++ ){
		num[2][i] = num[2][i+1];
	}
	num[2][6] = temp;
	return 0;
}

bool DFS( int deep ){
	if( deep == maxdeep ){
		return check();
	}
	if( deep + h() > maxdeep ){
		return false;
	} 

	rotationA();
	record[deep] = 'A';
	if( DFS( deep + 1 ) ){
		return true;
	}
	rotationF();

	rotationB();
	record[deep] = 'B';
	if( DFS( deep + 1 ) ){
		return true;
	}
	rotationE();

	rotationC();
	record[deep] = 'C';
	if( DFS( deep + 1 ) ){
		return true;
	}
	rotationH();

	rotationD();
	record[deep] = 'D';
	if( DFS( deep + 1 ) ){
		return true;
	}
	rotationG();

	rotationE();
	record[deep] = 'E';
	if( DFS( deep + 1 ) ){
		return true;
	}
	rotationB();

	rotationF();
	record[deep] = 'F';
	if( DFS( deep + 1 ) ){
		return true;
	}
	rotationA();

	rotationG();
	record[deep] = 'G';
	if( DFS( deep + 1 ) ){
		return true;
	}
	rotationD();

	rotationH();
	record[deep] = 'H';
	if( DFS( deep + 1 ) ){
		return true;
	}
	rotationC();

	return false;
}

int main(){
	
	while( cin >> num[0][2] && num[0][2] ){
	
		cin >> num[0][4] >> num[1][2] >> num[1][4];
		for( int i = 0; i < 7; i++ ){
			cin >> num[2][i];
		}
		cin >> num[3][2] >> num[3][4];
		for( int i = 0; i < 7; i++ ){
			cin >> num[4][i];
		}
		for( int i = 5; i < 7; i++ ){
			cin >> num[i][2] >> num[i][4];
		}

		maxdeep = 0;
		while( 1 ){
			if( DFS( 0 ) ){
				break;
			}
			maxdeep++;
		}

		if( maxdeep == 0 ){
			cout << "No moves needed" << endl;
		}else{
			for( int i = 0; i < maxdeep; i++ ){
				cout << record[i];
			}
			cout << endl;
		}
		cout << num[2][2] << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值