1 条题解

  • 1
    @ 2023-11-29 21:13:09
    #include<bits/stdc++.h>
    using namespace std;
    const int N=5,M=2e5+5;
    const int dx[4]={-1,0,1,0};
    const int dy[4]={0,1,0,-1};
    char str[N][N];
    int st[N][N],ed[N][N],a[N][N],f;
    void init(){
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			a[i][j]=st[i][j];
    		}
    	}
    	f=0;
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			if(a[i][j]^ed[i][j])f++;
    		}
    	}
    }
    bool check(){
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			if(a[i][j]^ed[i][j])return 0;
    		}
    	}
    	return 1;
    }
    bool dfs(int u,int dep){
    	if(u+(f+1)/2>dep)return 0;
    	if(check())return 1;
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			if(!a[i][j])continue;
    			for(int k=0;k<4;k++){
    				int x=i+dx[k],y=j+dy[k];
    				if(0<x&&x<=4&&0<y&&y<=4&&!a[x][y]){
    					if(a[i][j]!=ed[i][j])f--;
    					if(a[x][y]!=ed[x][y])f--;
    					swap(a[i][j],a[x][y]);
    					if(a[i][j]!=ed[i][j])f++;
    					if(a[x][y]!=ed[x][y])f++;
    					if(dfs(u+1,dep))return 1;
    					if(a[i][j]!=ed[i][j])f--;
    					if(a[x][y]!=ed[x][y])f--;
    					swap(a[i][j],a[x][y]);
    					if(a[i][j]!=ed[i][j])f++;
    					if(a[x][y]!=ed[x][y])f++;
    				}
    			}
    		}
    	}
    	return 0;
    }
    int main(){
    	for(int i=1;i<=4;i++){
    		scanf("%s",str[i]+1);
    	}
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			st[i][j]=str[i][j]-'0';
    		}
    	}
    	for(int i=1;i<=4;i++){
    		scanf("%s",str[i]+1);
    	}
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			ed[i][j]=str[i][j]-'0'; 
    		}
    	}
    	int dep=0;
    	init();
    	while(dep<17&&!dfs(0,dep)){
    		dep++;
    		init();
    	}
    	printf("%d",dep);
    	return 0;
    }
    
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N=5;
    const int dx[4]={-1,0,1,0};
    const int dy[4]={0,1,0,-1};
    char str[N][N];
    int st[N][N],ed[N][N],a[N][N];
    map<LL,bool>Hash;
    void init(){
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			a[i][j]=st[i][j];
    		}
    	}
    	Hash.clear();
    }
    int f(){
    	int cnt=0;
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			if(a[i][j]!=ed[i][j])cnt++;
    		}
    	}
    	return (cnt+1)/2;
    }
    bool check(){
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			if(a[i][j]!=ed[i][j])return 0;
    		}
    	}
    	return 1;
    }
    bool get(int u){
    	LL res=u;
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			res=res*10LL+(a[i][j]==1);
    		}
    	}
    	if(!Hash[res]){
    		Hash[res]=1;
    		return 0;
    	}
    	return 1;
    }
    bool dfs(int u,int dep){
    	if(u+f()>dep)return 0;
    	if(get(u))return 0;
    	if(check())return 1;
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			if(!a[i][j])continue;
    			for(int k=0;k<4;k++){
    				int x=i+dx[k],y=j+dy[k];
    				if(0<x&&x<=4&&0<y&&y<=4&&!a[x][y]){
    					swap(a[i][j],a[x][y]);
    					if(dfs(u+1,dep))return 1;
    					swap(a[i][j],a[x][y]);
    				}
    			}
    		}
    	}
    	return 0;
    }
    int main(){
    	for(int i=1;i<=4;i++){
    		scanf("%s",str[i]+1);
    	}
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			st[i][j]=str[i][j]-'0';
    		}
    	}
    	for(int i=1;i<=4;i++){
    		scanf("%s",str[i]+1);
    	}
    	for(int i=1;i<=4;i++){
    		for(int j=1;j<=4;j++){
    			ed[i][j]=str[i][j]-'0'; 
    		}
    	}
    	int dep=0;
    	init();
    	while(dep<17&&!dfs(0,dep)){
    		dep++;
    		init();
    	}
    	printf("%d\n",dep);
    	return 0;
    }
    
    • 1

    信息

    ID
    1054
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    (无)
    递交数
    19
    已通过
    8
    上传者