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

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(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
这里写图片描述

分析在代码中:
代码

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值