Problem J. Let Sudoku Rotate

PS:注意啊啊啊啊啊啊,Minato是逆时针旋转,那我们为了旋转回去,应该顺时针。没注意,debug花了好长时间。check的时候是判断当前的旋转是不是与先前的矛盾,比如将黑色那块旋转,就判断旋转后是不是与红色部分矛盾。

//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<stack>
#define ll long long
#define P pair<int, int>
#define PP pair<int,pair<int, int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define lson root << 1
#define INF (int)2e9 + 7
#define rson root << 1 | 1
#define LINF (unsigned long long int)1e18
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;

int T, res;
int a[20][20], c[4][4], d[4][4], vis[20], g[20][20], use[20];

void Get(int st, int ed) {
    for (int i = st, q = ed; i < st + 4; ++i, ++q) {
        for (int j = ed, p = st + 3; j < ed + 4; ++j, --p) g[i][j] = a[p][q];
    }
    for (int i = st; i < st + 4; ++i) {
        for (int j = ed; j < ed + 4; ++j) a[i][j] = g[i][j];
    }
}

bool check(int st, int ed) {
    for(int i = st; i < st + 4; ++i) {
        mem(use, 0);
        for(int j = 0; j < ed + 4; ++j) {
            if(use[a[i][j]]) return false;
            use[a[i][j]] = 1;
        }
    }
    for(int i = ed; i < ed + 4; ++i) {
        mem(use, 0);
        for(int j = 0; j < st + 4; ++j) {
            if(use[a[j][i]]) return false;
            use[a[j][i]] = 1;
        }
    }
    return true;
}

void DFS(int deep, int cnt) {
    if (deep > 15) {
        res = min(res, cnt);
        return;
    }
    if(cnt >= res) return;
    int st = (deep / 4) * 4;
    int ed = (deep % 4) * 4;
    for (int i = 0; i < 4; ++i) {
        if(i) Get(st, ed);
        if(check(st, ed)) DFS(deep + 1, cnt + i);
    }
    Get(st, ed);
}

int main()
{
    cin >> T;
    while (T--) {
        char ch;
        for (int i = 0; i < 16; ++i) {
            for (int j = 0; j < 16; ++j) {
                cin >> ch;
                a[i][j] = isdigit(ch) ? ch - '0' : ch - 'A' + 10;
            }
        }
        res = 3000;
        DFS(0, 0);
        printf("%d
", res);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zgglj-com/p/9414297.html