大样例输出双-1求助
查看原帖
大样例输出双-1求助
1210321
zhangzhixu000001楼主2024/6/24 15:28
#include <bits/stdc++.h>
using namespace std;
bool judge;
int sx,sy;
char ch[10][10];
char ans[10][10]={
    {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 h()
{
	int cnt=0;
	for (int i=1;i<=5;i++)
		for(int j=1;j<=5;j++)
			if(ch[i][j]!=ans[i][j]) cnt++;
	return cnt;
}
int dx[8]={1,-1,1,-1,2,-2,2,-2},dy[8]={2,2,-2,-2,1,1,-1,-1};
void dfs(int x,int y,int step,int maxstep)
{
	if(step==maxstep){if(!h()){judge=1;return;}}
	for (int i=0;i<8;i++)
	{
		int nx=x+dx[i],ny=y+dy[i];
		if(nx>1&&ny>1&&nx<=5&&ny<=5)
		{
			swap(ch[x][y],ch[nx][ny]);
			if(step+h()<=maxstep) dfs(nx,ny,step+1,maxstep); 
			swap(ch[nx][ny],ch[x][y]);
		}
	}
}
int iddfs()
{
	for (int i=0;i<=15;i++)
	{
		dfs(sx,sy,0,i);
		if(judge) return i;
	}
	return -1;
}
int main()
{
	ios::sync_with_stdio(0);
	int t;cin>>t;
	for (int k=1;k<=t;k++)
	{
		for (int i=1;i<=5;i++)
		{
			for (int j=1;j<=5;j++)
			{
				cin>>ch[i][j];
				if(ch[i][j]=='*')sx=i,sy=j;
			}
		}
		cout<<iddfs()<<'\n';
	}
	return 0;
}
2024/6/24 15:28
加载中...