求助为什么会T啊呜呜呜
查看原帖
求助为什么会T啊呜呜呜
614725
masonpop楼主2022/10/18 16:35

蒟蒻写了个IDA*,结果两个点加了O2也过不去,是哪里写错了吗?求大佬帮忙看看

#include <bits/stdc++.h>
using namespace std;
#define rx register
const int maxn=10;
const int dx[maxn]={0,1,1,-1,-1,2,2,-2,-2};
const int dy[maxn]={0,2,-2,2,-2,1,-1,1,-1};//步数 
const int mason[maxn][maxn]={//2是空格,0白,1黑 
	{0,0,0,0,0,0},
	{0,1,1,1,1,1},
	{0,0,1,1,1,1},
	{0,0,0,2,1,1},
	{0,0,0,0,0,1},
	{0,0,0,0,0,0}
};
int n,m,t,now[maxn][maxn],sx,sy,flag;//flag是否成功
inline int f()//估价函数
{
	int tot=0;
	for(int i=1;i<=5;i++)
	{
		for(int j=1;j<=5;j++)
		{
			if(now[i][j]!=mason[i][j])tot++;
		}
	}
	return tot;
} 
void IDA_star(int step,int x,int y,int mdep)//mdep最大深度
{
	if(step==mdep)
	{
		if(!f())flag=1;//全部归位 
		return;
	}
	for(rx int i=1;i<=8;i++)
	{
		rx int nx=x+dx[i],ny=y+dy[i];
		if(nx<1 || ny>5 || ny<1 || ny>5)continue;
		swap(now[x][y],now[nx][ny]);
		if(step+f()<=mdep)
		{
			IDA_star(step+1,nx,ny,mdep);	
		}	
		swap(now[x][y],now[nx][ny]);//回溯 
	}
} 
char ch;
signed main()
{
	scanf("%d",&t);
	while(t--)
	{
		flag=0;
		for(rx int i=1;i<=5;i++)
		{
			for(rx int j=1;j<=5;j++)
			{
				cin>>ch;
				if(ch=='*')
				{
					sx=i,sy=j;
					now[i][j]=2;
				}
				else now[i][j]=ch-'0';
			}
		}
		if(!f())
		{
			printf("0\n");
			continue;
		}
		for(rx int mdep=1;mdep<=15;mdep++)
		{
			IDA_star(0,sx,sy,mdep);
			if(flag)
			{
				printf("%d\n",mdep);
				break;
			}
		}
		if(!flag)printf("-1\n");
	}
	return 0;
}
2022/10/18 16:35
加载中...