WA on #9求助(玄关)
查看原帖
WA on #9求助(玄关)
1083592
a_round_thing楼主2024/5/31 19:34
#include<bits/stdc++.h>
using namespace std;
const int N=10;
char start[6][6]={
	{"11111"},{"01111"},{"00*11"},{"00001"},{"00000"}
};
string q[N];
pair<int,int> k;

int dx[8]={-1,-2,-2,-1,1,2,2,1},dy[8]={-2,-1,1,2,2,1,-1,-2};
int f(){
	int cnt=0;
	for(int i=0;i<5;i++){
		for(int j=0;j<5;j++){
			if(q[i][j]!=start[i][j]) cnt++;
		}
	}
	return cnt;
}
bool check(){
	for(int i=0;i<5;i++){
		for(int j=0;j<5;j++){
			if(q[i][j]!=start[i][j]) return false;
		}
	}
	return true;
}
bool dfs(int depth,int max_depth,int x,int y){
	if(depth+f()>max_depth) return false;
	if(check()) return true;
	for(int i=0;i<8;i++)
	{
		int a=x+dx[i],b=y+dy[i];
		if(a<0||a>=5||b<0||b>=5) continue;
		swap(q[x][y],q[a][b]);
		if(f()+depth<max_depth){
				if(dfs(depth+1,max_depth,a,b)) return true;
		}
		swap(q[x][y],q[a][b]);
	}
	return false;
}

int main(){
	int T;
	cin>>T;
	
	while(T--){
		for(int i=0;i<5;i++) cin>>q[i];
		//puts("run");
	for(int i=0;i<5;i++){
		for(int j=0;j<5;j++)
		{
			if(q[i][j]=='*') k={i,j};
		}
	}
	if(check()) {
		cout<<0<<endl;
		continue;
	}
	int depth=1;
	while(depth<=15&&!dfs(0,depth,k.first,k.second)) depth++;
	if(depth>15) puts("-1");
	else printf("%d\n",depth-1);
	}
	
}
2024/5/31 19:34
加载中...