WA#9求助!!!
查看原帖
WA#9求助!!!
1073457
Rinnosuke楼主2024/3/25 21:33
#include<bits/stdc++.h>
using namespace std;
char st[6][6],ed[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'},
	{'0','0','0','0','0','1'},
	{'0','0','0','0','0','0'},
};
int depth=0;
int h(){
	int ans=0;
	for(int i=1;i<=5;i++)
		for(int j=1;j<=5;j++){
			if(st[i][j]!=ed[i][j])ans++;
		}
	return ans;
}
const int dx[8]={2,1,-1,-2,-2,-1,1,2},dy[8]={1,2,2,1,-1,-2,-2,-1};
bool dfs(int now,int x,int y){
	int tmp=h();
	int xx=0,yy=0;
	if(!tmp)return 1; 
	if(now+tmp>depth)return 0;
	// cout<<x<<" "<<y<<'\n';
	for(int i=0;i<8;i++){
		xx=x+dx[i],yy=y+dy[i];
		if(xx>5 || xx<1 || yy>5 || yy<1)continue;
		swap(st[x][y],st[xx][yy]);
		if (dfs(now + 1,xx,yy)) return 1;
		swap(st[x][y],st[xx][yy]);
	}
	return 0;
}
int T;
int main(){
	cin>>T;
	while(T--)
	{
	for(int i=1;i<=5;i++)
		for(int j=1;j<=5;j++)
			cin>>st[i][j];

	depth=h();
	int x,y;
	for(int i=1;i<=5;i++)
		for(int j=1;j<=5;j++)
			if(st[i][j]=='*'){
				x=i,y=j;
				break;
			}
	while(depth && depth<=15 && !dfs(0,x,y))depth++;
	cout<<(depth>15 ? -1 : depth-1)<<'\n';
	}
	return 0;
}
2024/3/25 21:33
加载中...