zoukankan      html  css  js  c++  java
  • HDU

    Problem J. Let Sudoku Rotate

    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 16×16 grids, which consist of 4×4 regions. The objective is to fill the whole grid with hexadecimal digits, i.e. 0123456789ABCDEF, so that each column, each row, and each region contains all hexadecimal digits. The figure below shows a solved sudoku.



    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 T (1T103) denoting the number of test cases.
    Each test case consists of exactly 16 lines with 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.
     
    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
    ",minn);
        }
        return 0;
    }
  • 相关阅读:
    2015年要做的150件事
    再见2014,你好2015
    页面滚动事件的使用
    简单的进度条演示
    mybatis foreach批量处理
    mysql执行顺序
    spring声明式事务 同一类内方法调用事务失效
    Semaphore
    springmvc使用JSR-303对复杂对象进行校验
    springmvc全局异常后返回JSON异常数据
  • 原文地址:https://www.cnblogs.com/yzm10/p/9403302.html
Copyright © 2011-2022 走看看