ida* 70pts
查看原帖
ida* 70pts
1145602
dongzhen楼主2024/9/18 13:26
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <string.h>
#include <queue>
#define lnt long long
#define inf 0x3f3f3f3f
#define reg(i,bg,ed,p) for(int (i)=bg;i<ed;i+=p)
using namespace std;
int xx;char ff,chh;inline int read(){
    xx=ff=0;while(!isdigit(chh)){if(chh=='-'){ff=1;}chh=getchar();}
    while(isdigit(chh)){xx=(xx<<1)+(xx<<3)+chh-'0';chh=getchar();}return ff? -xx: xx;
}
const int N=3;
int now[N][N],to[N][N]={1,2,3,8,0,4,7,6,5};
int fx[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int check();
int dfs(int,int,int,int);
int main(){
	int x,y;
	reg(i,0,3,1){
		reg(e,0,3,1){
			now[i][e]=getchar()-'0';
			if(now[i][e]==0){x=i;y=e;}
		}
	}
	reg(i,0,114514,1){
		if(dfs(x,y,0,i)){
			printf("%d",i);
			return 0;
		}
	}
	
    return 0;
}
int check(){
	int temp=0;
	reg(i,0,3,1){reg(e,0,3,1){temp+=(to[i][e]!=now[i][e]);}}
	return temp-1;
}
bool is(int x,int y){return x!=3 && x!=-1 && y!=3 && y!=-1;}
int dfs(int x,int y,int d,int md){
	int u=check();
	if(u<=0){return 1;}
	if(d>=md || u>md-d){return 0;}
	reg(i,0,4,1){
		int tox=x+fx[i][0],toy=y+fx[i][1];
		if(!is(tox,toy)){continue;}
		swap(now[x][y],now[tox][toy]);
		if(dfs(tox,toy,d+1,md)){return 1;}
		swap(now[x][y],now[tox][toy]);
	}
	return 0;
}





2024/9/18 13:26
加载中...