爆搜水题的样例过不去,求调
查看原帖
爆搜水题的样例过不去,求调
482730
aSunnyDay楼主2022/6/8 22:15

谢谢各位大佬,不好意思那么晚打扰了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,Ans,bx,by,d=0,dx[8]={2,2,1,1,-1,-1,-2,-2},dy[8]={1,-1,2,-2,2,-2,1,-1},b[6][6],a[6][6]={
0,0,0,0,0,0,
0,1,1,1,1,1,
0,0,1,1,1,1,
0,0,0,-1,1,1,
0,0,0,0,0,1,
0,0,0,0,0,0};
bool dfs(ll step,ll u,ll bx,ll by){//步数 不同的数量 
	if(step==Ans+1){
		if(u) return 0;
		return 1;
	}
	if(u>Ans-step+2) return 0;//测试过了,这句话删除还是有问题,说明问题在爆搜上? 
//	cout<<"step:"<<step<<" dif:"<<u<<"\n";
//	for(ll i=1;i<=5;++i,cout<<"\n")
//		for(ll j=1;j<=5;++j)
//			cout<<b[i][j]<<" ";
//	cout<<"======================\n";
	for(ll i=0;i<8;++i){
		ll nx=bx+dx[i],ny=by+dy[i];
		if(nx>=1&&nx<=5&&ny>=1&&ny<=5){
			ll nd=d-(a[nx][ny]==b[bx][by])-(a[bx][by]==b[nx][ny])+(a[nx][ny]==b[nx][ny])+(a[bx][by]==b[bx][by]);
			swap(b[nx][ny],b[bx][by]);
			if(dfs(step+1,nd,nx,ny)) return 1;
			swap(b[nx][ny],b[bx][by]);
		}
	}
	return 0;
}
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin>>T;
	while(T--){
		bool ok=1;d=0; 
		for(ll i=1;i<=5;++i)
			for(ll j=1;j<=5;++j){
				char c;
				cin>>c;
				if(c>='0'&&c<='9')
					b[i][j]=(c^48);
				else b[i][j]=-1,bx=i,by=j;
				d+=(bool)(b[i][j]^a[i][j]);
			}
//		cout<<"dif:"<<d<<"\n";
		for(Ans=0;Ans<=15;++Ans){
//			cout<<"NOW:"<<Ans<<"\n"; 
			if(dfs(1,d,bx,by)){cout<<Ans<<"\n",ok=0;break;}
		}
		if(ok)cout<<"-1\n";
	}
	return 0;
} 
2022/6/8 22:15
加载中...