1 条题解

  • 0
    @ 2024-12-24 9:54:32

    C++ :

    /*
    	Name: fliptile
    	Author: 靳帅祥
    	Date: 22/10/14 08:58
    	Description:
    */
    #include <cstdio>
    #include <climits>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <cstdlib>
    using namespace std;
    
    int dx[4]={0,1,-1, 0};
    int dy[4]={1,0, 0,-1};
    
    int H=0,L=0;
    
    bool jsx=false;
    
    class node{
    	public:
    		int Step;
    		int G[20][20];
    		bool operator < (const node& b) const {
    			if(Step==b.Step){
    				for(int i=1;i<=H;++i){
    					for(int j=1;j<=L;++j){
    						if(G[i][j] < b.G[i][j]) return true;
    						else return false;
    					}
    				}
    				return true;
    			}
    			return Step < b.Step;
    		}		
    }E,Map,Ans;
    
    inline bool PD(int x,int y){
    	if(x>0 && y>0 && x<=H && y<=L) return true;
    	return false;
    }
    
    inline bool Full(node b){
    	for(int i=1;i<=H;++i){
    		for(int j=1;j<=L;++j){
    			if(b.G[i][j]!=0) return false; 
    		}
    	}
    	return true;
    }
    
    inline void Turn(int x,int y){
    	Map.G[x][y]++;	Map.Step++;
    	E.G[x][y]=!E.G[x][y];
    	for(int i=0;i<4;++i){
    		int xx=x+dx[i],yy=y+dy[i];
    		if(PD(xx,yy))	E.G[xx][yy]=!E.G[xx][yy];
    	}
    }
    
    inline void DEAL(){
    	node temp1=E;
    	node temp2=Map;//储存状态 
    	for(int i=2;i<=H;++i){
    		for(int j=1;j<=L;++j){
    			if(E.G[i-1][j]==1)	Turn(i,j);
    		}
    	}
    	if(Full(E)){
    		if(!jsx){	Ans=Map; jsx=true; }
    		else{
    			if(Map < Ans){  Ans=Map; }
    		}
    	}
    	E=temp1;	Map=temp2;//还原 
    }
    
    void DFS(int pos,int F){
    	if(pos==L+1){ DEAL(); return; }
    	
    	if(F==0){	DFS(pos+1,0);	DFS(pos+1,1);	}
    	else{
    		node t1=E,t2=Map;
    		Turn(1,pos);	DFS(pos+1,0);	DFS(pos+1,1);
    		E=t1; Map=t2;//还原 
    	}
    }
    
    int main(){
    	//freopen("fliptile.in","r",stdin);
    	//freopen("fliptile.out","w",stdout);
    	
    	scanf("%d%d",&H,&L);
    	for(int i=1;i<=H;++i){
    		for(int j=1;j<=L;++j){
    			scanf("%d",&E.G[i][j]);
    		}
    	}
    	
    	//1.第一行不翻转
    	//2.枚举第一行的状态 2^15.....
    	DFS(1,0);	DFS(1,1);
    	
    	if(jsx==true){
    		for(int i=1;i<=H;++i){
    			printf("%d",Ans.G[i][1]);
    			for(int j=2;j<=L;++j){
    				printf(" %d",Ans.G[i][j]);
    			}
    			printf("\n");
    		}
    	}
    	else{
    		printf("IMPOSSIBLE\n");
    	}
    	
    	fclose(stdin);
    	fclose(stdout);
    	return 0;
    }
    
    • 1

    信息

    ID
    1222
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者