• 2018 Multi-University Training Contest 4


    dfs

    每个4x4的宫格转了4次就变回原样了,所以最多转3次。

    枚举没4行内的子矩阵,旋转后判断每行每列数是否冲突,在每次循环结束后子矩阵相当于没有转,因此遍历了每种情况。

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    #define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    int _, ans;
    char s[20][20];
    bool vis[100];
    
    int h(char ch){
        return isdigit(ch) ? ch - '0' : ch - 'A' + '0';
    }
    
    void rotate(int x, int y){
        char t[5][5];
        for(int i = 1, a = x; i <= 4; i ++, a ++){
            for(int j = 1, b = y; j <= 4; j ++, b ++) t[i][j] = s[a][b];
        }
        for(int j = 1, a = x; j <= 4; j ++, a ++){
            for(int i = 4, b = y; i >= 1; i --, b ++) s[a][b] = t[i][j];
        }
    }
    
    bool check(int row){
        for(int i = row; i <= row + 3; i ++){
            full(vis, false);
            for(int j = 1; j <= 16; j ++){
                if(vis[h(s[i][j])]) return false;
                vis[h(s[i][j])] = true;
            }
        }
        for(int j = 1; j <= 16; j ++){
            full(vis, false);
            for(int i = 1; i <= row + 3; i ++){
                if(vis[h(s[i][j])]) return false;
                vis[h(s[i][j])] = true;
            }
        }
        return true;
    }
    
    void dfs(int k, int step){
        if(step > ans) return;
        if(k > 16){
            ans = min(ans, step);
            return;
        }
        for(int a = 0; a <= 3; a ++, rotate(k, 1)){
            for(int b = 0; b <= 3; b ++, rotate(k, 5)){
                for(int c = 0; c <= 3; c ++, rotate(k, 9)){
                    for(int d = 0; d <= 3; d ++, rotate(k, 13)){
                        if(!check(k)) continue;
                        dfs(k + 4, a + b + c + d + step);
                    }
                }
            }
        }
    }
    
    int main(){
        for(_ = read(); _; _ --){
            for(int i = 1; i <= 16; i ++) scanf("%s", s[i] + 1);
            ans = INF;
            dfs(1, 0);
            printf("%d
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    联表查询更新
    SQLServer 中多行数据合并成一行数据(一个字段)
    换行和回车的区别
    SQL语句中使用回车换行符
    g2o使用总结
    求导总结
    ubuntu14.04 升级gcc
    如何入门SLAM
    imu和canmera标定
    使用velodyne16线激光雷达跑loam-velodyne------包括激光雷达和imu的标定
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10909131.html
Copyright © 2020-2023  润新知