dfs

https://www.bnuoj.com/v3/problem_show.php?pid=4049

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std ;
const int N=6;//10+1e2;
int f[20][20];
string ans[N];
int same(int a,int b,int c,int d)
{
    for(int i=a;i<=b;i++)
    for(int j=c;j<=d;j++)
        if(f[i][j]!=f[a][c])    return 0;
    return 1;
}
int dfs(int x,int y,int i,int j,int d)
{
    if(same(x,y,i,j))
    {
        if(f[x][i]==1)  ans[d]+="01";
        else            ans[d]+="00";
        return 0;
    }
    ans[d]+="1";
    d++;
    //1
    dfs(x,(x+y)/2,i,(i+j)/2,d);
    //2
    dfs(x,(x+y)/2,(i+j)/2+1,j,d);
    //3
    dfs((x+y)/2+1,y,i,(i+j)/2,d);
    //4
    dfs((x+y)/2+1,y,(i+j)/2+1,j,d);
    return 0;
}
int main()
{
    int n,i,j;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)    cin>>f[i][j];
        for(i=0;i<N;i++)    ans[i]="";
        dfs(1,n,1,n,0);
        for(i=0;i<N;i++)    cout<<ans[i];
        cout<<endl;
    }
    return 0 ;
}


你可能感兴趣的:(dfs)