欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【HDU-6341】 Let Sudoku Rotate【DFS + 剪枝】

程序员文章站 2022-07-07 22:53:46
...

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.
【HDU-6341】 Let Sudoku Rotate【DFS + 剪枝】
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(1≤T≤103)T(1≤T≤103) 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 statemen
【HDU-6341】 Let Sudoku Rotate【DFS + 剪枝】

分析在代码中:
代码

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N = (int)1e5 + 11;
const int M = (int)15e6 + 11;
const int MOD = (int)1e9 + 7;
const int INF = (int) 0x3f3f3f3f;

char mp[100][100];
void rot(int i, int j){ // 将(i, j)大块顺时针旋转90
    i++; j++;
    int I = (i - 1) * 4 ; int J = (j - 1) * 4;
    int n = 4;
   for (int i = 0; i < n; ++i)
    for (int j = 0; j < n - i; ++j)
        swap(mp[i + I][j + J], mp[n - 1 - j + I][J + n - 1 - i]);

    for (int i = 0; i < n / 2; ++i)
    for (int j = 0; j < n; ++j)
        swap(mp[i + I][J + j], mp[n - 1 - i + I][J + j]);
}
bool vis[300];
bool ok(int id){
    for(int i = 0; i < id / 4 * 4; i++){
        memset(vis, false, sizeof(vis));
        for(int j = 0; j < 16; j++){
            if(vis[mp[i][j]]) return false;
            vis[mp[i][j]] = true;
        }
    }
    for(int i = id / 4 * 4; i < id / 4 * 4 + 4; i++){
        memset(vis, false, sizeof(vis));
        for(int j = 0; j < id % 4 * 4 + 4; j++){
            if(vis[mp[i][j]]) return false;
            vis[mp[i][j]] = true;
        }
    }

    for(int j = id % 4 * 4 + 4; j < 16; j++){
        memset(vis, false, sizeof(vis));
        for(int i = 0; i < id / 4 * 4; i++){
            if(vis[mp[i][j]]) return false;
            vis[mp[i][j]] = true;
        }
    }

    for(int j = 0; j < id % 4 * 4 + 4; j++){
        memset(vis, false, sizeof(vis));
        for(int i = 0; i < id / 4 * 4 + 4; i++){
            if(vis[mp[i][j]]) return false;
            vis[mp[i][j]] = true;
        }
    }
    return true;
}
int ans;
void dfs(int id, int cnt){ // 将二维的16块转换为一维的16块,然后每块可以旋转4次,这样递归下去。
    if(cnt >= ans) return ; // 最优性剪枝
    if(id == 16){
        ans = min(ans, cnt);
        return ;
    }
    for(int i = 0; i < 4; i++){
        if(ok(id)) dfs(id + 1, cnt + i); // 可行性剪枝,对通过DFS确定的部分进行可行性判定
        rot(id / 4, id % 4);
    }
}
int main(){
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int _;for(scanf("%d", &_); _; _--){
        for(int i = 0; i < 16; i++) scanf("%s", mp[i]);
        ans = INF;
        dfs(0, 0);
        printf("%d\n", ans);
    }
    return 0;
}