• 洛谷P1074 靶形数独(算竞进阶习题)


    还是数独。。

    比上一个多了个分数矩阵,其实没什么差别,但是数据好像水了许多。。。

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    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 a % b ? gcd(b, a % b) : b; }
    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 a[10][10], row[9], col[9], grid[9], cnt[512], num[512], tot, ans;
    
    const int s[10][10]{
        {6, 6, 6, 6, 6, 6, 6, 6, 6},
        {6, 7, 7, 7, 7, 7, 7, 7, 6},
        {6, 7, 8, 8, 8, 8, 8, 7, 6},
        {6, 7, 8, 9, 9, 9, 8, 7, 6},
        {6, 7, 8, 9, 10, 9, 8, 7, 6},
        {6, 7, 8, 9, 9, 9, 8, 7, 6},
        {6, 7, 8, 8, 8, 8, 8, 7, 6},
        {6, 7, 7, 7, 7, 7, 7, 7, 6},
        {6, 6, 6, 6, 6, 6, 6, 6, 6}
    };
    
    int g(int x, int y){ return (x / 3) * 3 +(y / 3); }
    
    void flip(int x, int y, int z){
        row[x] ^= (1 << z);
        col[y] ^= (1 << z);
        grid[g(x, y)] ^= (1 << z);
    }
    
    void dfs(int step, int score){
        if(step == 0){
            //cout << score << endl;
            //for(int i = 0; i < 9; i ++){for(int j = 0; j < 9; j ++)printf("%d ", a[i][j]);puts("");}
            ans = max(ans, score);
            return;
        }
        int x = -1, y = -1, val = INF;
        for(int i = 0; i < 9; i ++){
            for(int j = 0; j < 9; j ++){
                if(a[i][j] != 0) continue;
                int tmp = row[i] & col[j] & grid[g(i, j)];
                if(tmp == 0) return;
                if(cnt[tmp] < val) val = cnt[tmp], x = i, y = j;
            }
        }
        int tmp = row[x] & col[y] & grid[g(x, y)];
        for(; tmp; tmp -= lowbit(tmp)){
            int k = num[lowbit(tmp)];
            a[x][y] = k + 1;
            flip(x, y, k);
            dfs(step - 1, score + (s[x][y] * a[x][y]));
            flip(x, y, k);
            a[x][y] = 0;
        }
    }
    
    int main(){
    
        for(int i = 0; i < (1 << 9); i ++){
            for(int j = i; j; j -= lowbit(j)) cnt[i] ++;
        }
        for(int i = 0; i < 9; i ++) num[1 << i] = i;
        for(int i = 0; i < 9; i ++) row[i] = col[i] = grid[i] = (1 << 9) - 1;
        for(int i = 0; i < 9; i ++){
            for(int j = 0; j < 9; j ++) a[i][j] = read();
        }
        int temp = 0;
        for(int i = 0; i < 9; i ++){
            for(int j = 0; j < 9; j ++){
                if(a[i][j] != 0) flip(i, j, a[i][j] - 1), temp += (a[i][j] * s[i][j]);
                else tot ++;
            }
        }
        dfs(tot, temp);
        printf(ans == 0 ? "-1
    " : "%d
    ", ans);
        //for(int i = 0; i < 9; i ++){for(int j = 0; j < 9; j ++)printf("%d ", a[i][j]);puts("");}
        return 0;
    }
    
  • 相关阅读:
    每日站立会议02
    每日站立会议01
    构建之法阅读笔记02
    学习进度条06
    js获取当前时间戳
    带三角凸的框
    js或者jq的tab切换
    处理浏览器兼容 各个浏览器的标识 hack
    规定CSS的属性仅在IE下生效 在非IE浏览器下不生效
    滚动到某个点 返回顶部字样出来 点击返回顶部
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10554358.html
Copyright © 2020-2023  润新知