1 solutions

  • 0
    @ 2021-11-13 17:03:55

    视频讲解地址:https://www.bilibili.com/video/BV1bY411x7qy?share_source=copy_web

    #include<iostream>
    #include<string.h>
    using namespace std;
    char a[6][6],b[6][6];
    int fx[6]={1,-1,0,0,0};
    int fy[6]={0,0,1,-1,0};
    void turn (int x,int y){
    	for(int i=0;i<5;i++){
    		if(x+fx[i]>=0&&x+fx[i]<5&&y+fy[i]>=0&&y+fy[i]<5)
    			a[x+fx[i]][y+fy[i]]^=1;
    	}
    
    	return ;
    }
    int main(){
    	int t;
    	cin>>t;
    	while(t--){
    		for(int i=0;i<5;i++) cin>>a[i];
    		int cou,ans=0x3f3f3f3f;
    		for(int op=0;op<32;op++){
    			memcpy(b,a,sizeof(a));
    			cou=0;
    
    			for(int i=0;i<5;i++){
    				if(op>>i&1){
    					turn(0,i);
    					cou++;
    				}
    			}
    			for(int i=1;i<5;i++){
    				for(int j=0;j<5;j++){
    					if(a[i-1][j]=='0'){
    						turn(i,j);
    						cou++;
    					}
    				}
    			}
    			int flag=0;
    			for(int i=0;i<5;i++){
    				if(a[4][i]=='0'){
    					flag=1;
    					break;
    				}
    			}
    			if(!flag){
    				if(cou<ans)ans=cou;
    			}
    			memcpy(a,b,sizeof(b));
    		}
    		if(ans>777)ans=-1;
    		cout<<ans<<endl;		
    	}
    	
    	
    	
    	
    	
    	return 0;
    }
    
    
    
    • 1

    Information

    ID
    157
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    9
    Tags
    # Submissions
    24
    Accepted
    4
    Uploaded By