Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
Sudoku is a logic-based, combinatorial number-placement puzzle, which is popular around the world.
In this problem, let us focus on puzzles with 

Yesterday, Kazari solved a sudoku and left it on the desk. However, Minato played a joke with her - he performed the following operation several times.
* Choose a region and rotate it by 90 degrees counterclockwise.
She burst into tears as soon as she found the sudoku was broken because of rotations.
Could you let her know how many operations her brother performed at least?
 

 

Input
The first line of the input contains an integer 16 characters each, describing a broken sudoku.
 

 

Output
For each test case, print a non-negative integer indicating the minimum possible number of operations.
 

 

Sample Input
1 681D5A0C9FDBB2F7 0A734B62E167D9E5 5C9B73EF3C208410 F24ED18948A5CA63 39FAED5616400B74 D120C4B7CA3DEF38 7EC829A085BE6D51 B56438F129F79C2A 5C7FBC4E3D08719F AE8B1673BF42A58D 60D3AF25619C30BE 294190D8EA57264C C7D1B35606835EAB AF52A1E019BE4306 8B36DC78D425F7C9 E409492FC7FA18D2
 

 

Sample Output
5
Hint
The original sudoku is same as the example in the statement.
HDU - 6341 多校4 Let Sudoku Rotate(状压dfs)
 

 

Statistic | Submit | Clarifications | Back

 

 

#include <bits/stdc++.h>

const int MAX = 20;
const int INF = 0x3f3f3f3f;
typedef long long ll;

char s[MAX][MAX];
int b[MAX][MAX],h[MAX],l[MAX];

ll minn;

int huan(char c){
    if('0'<=c&&c<='9') return c-'0';
    return c-'A'+10;
}
int biao(int x,int y,int k){
    int i,j,ii,jj;
    int xb=x%4*4;
    int yb=y%4*4;
    if(k==0){
        for(i=xb;i<xb+4;i++){
            for(j=yb;j<yb+4;j++){
                if(h[i]&(1<<huan(s[i][j]))) return 0;
                if(l[j]&(1<<huan(s[i][j]))) return 0;
            }
        }
        for(i=xb;i<xb+4;i++){
            for(j=yb;j<yb+4;j++){
                h[i]|=1<<huan(s[i][j]);
                l[j]|=1<<huan(s[i][j]);
            }
        }
    }
    else if(k==3){
        for(j=yb+3,ii=xb;j>=yb;j--,ii++){
            for(i=xb,jj=yb;i<xb+4;i++,jj++){
                if(h[ii]&(1<<huan(s[i][j]))) return 0;
                if(l[jj]&(1<<huan(s[i][j]))) return 0;
            }
        }
        for(j=yb+3,ii=xb;j>=yb;j--,ii++){
            for(i=xb,jj=yb;i<xb+4;i++,jj++){
                h[ii]|=1<<huan(s[i][j]);
                l[jj]|=1<<huan(s[i][j]);
            }
        }
    }
    else if(k==2){
        for(i=xb+3,ii=xb;i>=xb;i--,ii++){
            for(j=yb+3,jj=yb;j>=yb;j--,jj++){
                if(h[ii]&(1<<huan(s[i][j]))) return 0;
                if(l[jj]&(1<<huan(s[i][j]))) return 0;
            }
        }
        for(i=xb+3,ii=xb;i>=xb;i--,ii++){
            for(j=yb+3,jj=yb;j>=yb;j--,jj++){
                h[ii]|=1<<huan(s[i][j]);
                l[jj]|=1<<huan(s[i][j]);
            }
        }
    }
    else{
        for(j=yb,ii=xb;j<yb+4;j++,ii++){
            for(i=xb+3,jj=yb;i>=xb;i--,jj++){
                if(h[ii]&(1<<huan(s[i][j]))) return 0;
                if(l[jj]&(1<<huan(s[i][j]))) return 0;
            }
        }
        for(j=yb,ii=xb;j<yb+4;j++,ii++){
            for(i=xb+3,jj=yb;i>=xb;i--,jj++){
                h[ii]|=1<<huan(s[i][j]);
                l[jj]|=1<<huan(s[i][j]);
            }
        }
    }
    return 1;
}

void shan(int x,int y,int k){
    int i,j,ii,jj;
    int xb=x%4*4;
    int yb=y%4*4;
    if(k==0){
        for(i=xb;i<xb+4;i++){
            for(j=yb;j<yb+4;j++){
                h[i]^=1<<huan(s[i][j]);
                l[j]^=1<<huan(s[i][j]);
            }
        }
    }
    else if(k==3){
        for(j=yb+3,ii=xb;j>=yb;j--,ii++){
            for(i=xb,jj=yb;i<xb+4;i++,jj++){
                h[ii]^=1<<huan(s[i][j]);
                l[jj]^=1<<huan(s[i][j]);
            }
        }
    }
    else if(k==2){
        for(i=xb+3,ii=xb;i>=xb;i--,ii++){
            for(j=yb+3,jj=yb;j>=yb;j--,jj++){
                h[ii]^=1<<huan(s[i][j]);
                l[jj]^=1<<huan(s[i][j]);
            }
        }
    }
    else{
        for(j=yb,ii=xb;j<yb+4;j++,ii++){
            for(i=xb+3,jj=yb;i>=xb;i--,jj++){
                h[ii]^=1<<huan(s[i][j]);
                l[jj]^=1<<huan(s[i][j]);
            }
        }
    }
}
void dfs(int x,int ss){
    int i,j,k;
    if(ss>=16){
        if(x<minn) minn=x;
        return;
    }
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
            if(b[i][j]==0){
                for(k=0;k<4;k++){
                    if(!biao(i,j,k)) continue;
                    b[i][j]=1;
                    dfs(x+k,ss+1);
                    shan(i,j,k);
                    b[i][j]=0;
                }
                return;
            }
        }
    }
}

int main(void)
{
    int t,n,i;
    scanf("%d",&t);
    while(t--){
        for(i=0;i<16;i++){
            scanf(" %s",s[i]);
        }
        minn=1000000000000;
        dfs(0,0);
        printf("%d\n",minn);
    }
    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2022-02-17
  • 2022-12-23
  • 2022-01-05
  • 2021-11-10
  • 2022-12-23
  • 2022-01-22
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-11
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
相关资源
相似解决方案