IDA* 25分求调
查看原帖
IDA* 25分求调
714936
_lingrui楼主2023/11/14 15:00
#include <bits/stdc++.h>
using namespace std;
int mp[15],sw;
int dw[]={-3,3,1,-1};
int goal[15]={0,1,2,3,8,0,4,7,6,5};
int check(){
	int x=0;
	for(int i=1;i<=9;i++){
		if(goal[i]!=mp[i]){
			x++;
		}
	}
	return x;
}
void IDA_star(int maxdep,int deep,int s,int pre){
	if(deep==maxdep){
		if(check()==0){
			cout << maxdep;
			exit(0);
		}
		return ;
	}
	for(int i=0;i<4;i++){
		int dx=s+dw[i];
		if(dx>=1&&dx<=9&&dw[i]+pre!=0){
			swap(mp[dx],mp[s]);
			if(deep+check()<=maxdep){
				IDA_star(maxdep,deep+1,dx,dw[i]);
			}
			swap(mp[dx],mp[s]);
		}
	}
}
int main(){
	char x;
	for(int i=1;i<=9;i++){
		cin >> x;
		mp[i]=x-'0';
		if(mp[i]==0){
			sw=i;
		}
	}
	for(int i=0;;i++){
		IDA_star(i,0,sw,0);
	}
	return 0;
}
2023/11/14 15:00
加载中...