HDU - 6341 多校4 Let Sudoku Rotate(状压dfs)

本文探讨了一个特别的16x16数独问题,即如何通过最少次数的区域逆时针旋转使一个被破坏的数独恢复到原始状态。文中提供了一种通过回溯算法来寻找最优解的方法。

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\n",minn);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/yzm10/p/9403302.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值