Question 2 POJ 2965

I did the first question and saw the previous release. I found that the same method is the same. 4x4 has not changed. I just added an output conversion path. I didn’t set zero to one. I wrote a big simulation directly and recorded it after flipping it. The number of steps is recorded, wonderful, and I have a little more understanding of recursion.
Write it again tomorrow, come on.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[5][5];
int ansx[20],ansy[20];
int tx[20],ty[20];
int ans=50;
bool check(){
    
    
    for(int i=0;i<4;i++){
    
    
        for(int j=0;j<4;j++){
    
    
            if(str[i][j]!='-') return 0;
        }
    }
    return 1;
}
void change(int x){
    
    
    int xx=x/4;
    int yy=x%4;
    for(int i=0;i<4;i++){
    
    
        if(str[i][yy]=='-') str[i][yy]='+';
        else str[i][yy]='-';
        if(str[xx][i]=='-') str[xx][i]='+';
        else str[xx][i]='-';
    }
    if(str[xx][yy]=='-') str[xx][yy]='+';
    else str[xx][yy]='-';
}
void dfs(int x,int step){
    
    
    if(check()){
    
    
        if(step<ans){
    
    
            ans=step;
            for(int i=1;i<=ans;i++){
    
    
                ansx[i]=tx[i];
                ansy[i]=ty[i];
            }
        }
        return ;
    }
    if(x>=16) return ;
    dfs(x+1,step);
    change(x);
    tx[step+1]=x/4+1;
    ty[step+1]=x%4+1;
    dfs(x+1,step+1);
    change(x);
}
int main(){
    
    
    for(int i=0;i<4;i++){
    
    
        scanf("%s",str[i]);
    }
    dfs(0,0);
    printf("%d\n",ans);
    for(int i=1;i<=ans;i++){
    
    
        printf("%d% d\n",ansx[i],ansy[i]);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/iuk11/article/details/109086319