蒟蒻a*+迭代加深55pts求调教
查看原帖
蒟蒻a*+迭代加深55pts求调教
754502
_AyachiNene楼主2023/5/23 16:18
#include<bits/stdc++.h>
using namespace std;
const int b[114][514]={{114,514,1919,810},{114514,1,2,3},{114514,8,0,4},{114514,7,6,5}};
const int dx[114]={0,1,-1,0},dy[114]={1,0,0,-1};
bool flag=0;
int a[114][514],cnt=-1,k;
int sx,sy;
string s;
bool cheak()
{
	for(int i=1;i<=3;i++)
		for(int j=1;j<=3;j++)
			if(a[i][j]!=b[i][j])
				return 0;
	return 1;
}
bool a_(int step)
{
	int sum=0;
	for(int i=1;i<=3;i++)
		for(int j=1;j<=3;j++)
			if(a[i][j]!=b[i][j])
				if(++sum+step>k)
					return 0;
	return 1;
}
void dfs(int x,int y,int step,int last)
{
	if(step==k)
	{
		if(cheak())
			flag=1;
		return;
	}
	if(flag)
		return;
	for(int i=0;i<4;i++)
	{
		int nx=x+dx[i],ny=y+dy[i];
		if(nx<1||ny<1||nx>3||ny>3||i+last==3)
			continue;
		swap(a[x][y],a[nx][ny]);
		if(a_(step))
			dfs(nx,ny,step+1,i);
		swap(a[x][y],a[nx][ny]);
	}
}
int main()
{
	cin>>s;
	for(int i=1;i<=3;i++)
		for(int j=1;j<=3;j++)
		{
			a[i][j]=s[++cnt]-'0';
			if(a[i][j]==0)
				sx=i,sy=i;
		}
	if(cheak())
	{
		cout<<0;
		return 0;
    } 
	while(++k)
	{
		
		dfs(sx,sy,0,-1);
		if(flag)
		{
			cout<<k;
			return 0;
		}
	}
}
2023/5/23 16:18
加载中...