首页 > 其他分享 >[AcWing 166] 数独

[AcWing 166] 数独

时间:2022-08-18 00:22:17浏览次数:61  
标签:cnt int long ++ str 166 数独 AcWing

image
image

DFS + 剪枝 + 位运算优化


点击查看代码
#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 9, M = 1 << N;

int ones[M]; // ones[i]表示i的二进制数中1的个数 
int map2[M]; // map2[i]表示log_2(i)
int row[N], col[N], cell[3][3]; // 二进制数,1代表没填数
string str;

// 将三个数组全部设置为未填数字
void init()
{
    for (int i = 0; i < N; i ++)
        row[i] = col[i] = (1 << N) - 1;
    for (int i = 0; i < 3; i ++)
        for (int j = 0; j < 3; j ++)
            cell[i][j] = (1 << N) - 1;
}

// 在(x,y)位置填数字(t+1)
// is_set为true表示填,为false表示不填(用于恢复现场)
void draw(int x, int y, int t, bool is_set)
{
    if (is_set)
        str[x * N + y] = '1' + t;
    else
        str[x * N + y] = '.';
    int v = 1 << t;
    if (!is_set)
        v = -v;
    row[x] -= v;
    col[y] -= v;
    cell[x / 3][y / 3] -= v;
}

int get(int x, int y)
{
    return row[x] & col[y] & cell[x / 3][y / 3];
}

int lowbit(int x)
{
    return x & -x;
}

bool dfs(int cnt)
{
    if (!cnt)
        return true;
    // 找到分支情况最少的格子
    int minv = N + 1;
    int x, y;
    for (int i = 0; i < N; i ++)
        for (int j = 0; j < N; j ++)
            if (str[i * N + j] == '.') {
                int state = get(i, j);
                if (ones[state] < minv) {
                    minv = ones[state];
                    x = i, y = j;
                }
            }
    int state = get(x, y);
    // 在1的位置填数字
    for (int i = state; i; i -= lowbit(i)) {
        int t = map2[lowbit(i)];
        draw(x, y, t, true);
        if (dfs(cnt - 1))
            return true;
        draw(x, y, t, false);
    }
    return false;
}

void solve()
{
    for (int i = 0; i < N; i ++)
        map2[1 << i] = i;
    for (int i = 0; i < 1 << N; i ++)
        for (int j = 0; j < N; j ++)
            ones[i] += i >> j & 1;
    while (cin >> str, str != "end") {
        init();
        // 记录空格子的个数
        int cnt = 0;
        // (i,j)是二维坐标 k是一维坐标
        for (int i = 0, k = 0; i < N; i ++)
            for (int j = 0; j < N; j ++, k ++) {
                if (str[k] != '.') {
                    // 将字符1-9映射到数字0-8
                    int t = str[k] - '1';
                    draw(i, j, t, true);
                }
                else
                    cnt ++;
            }
        dfs(cnt);
        cout << str << endl;
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

标签:cnt,int,long,++,str,166,数独,AcWing
From: https://www.cnblogs.com/wKingYu/p/16597331.html

相关文章

  • acwing204.表达整数的奇怪方式(中国剩余定理)
    中国剩余定理中国剩余定理百度百科不定方程\(ax+by=gcd(a,b)\)的解先用扩展欧几里得算法求得不定方程的一组特解:\(x_0,y_0\)则不定方程的通解为\[\left\{\begin......
  • [AcWing 165] 小猫爬山
    DFS剪枝点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;constintN=50+10;intn,m;intw[N];intsum[N];//每组......
  • acwing2022秋招每日一题 1302. 层数最深叶子节点的和
    题目给你一棵二叉树的根节点 root ,请你返回 层数最深的叶子节点的和 。思路根据层序遍历,访问所有节点。将每一层上的结点,进行统计,直到最后一层时,统计所有节点数。......
  • [AcWing 1118] 分成互质组
    DFS点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;constintN=50+10;intn;inta[N];intlen,ans=1e9;vector<i......
  • [AcWing 1117] 单词接龙
    DFS点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;constintN=50+10;intn;stringword[N];intg[N][N];//g[i][......
  • [AcWing 258] 石头剪刀布
    带扩展域的并查集点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;constintN=1e6+10;intn,m;intp[N];structN......
  • 36.有效的数独
      输入:board=[["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".","."......
  • [AcWing 145] 超市
    贪心+小根堆点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;typedefpair<int,int>PII;constintN=1e6+10;intn;......
  • NC16681 [NOIP2003]加分二叉树
    题目链接题目题目描述​设一个n个节点的二叉树tree的中序遍历为(l,2,3,…,n),其中数字1,2,3,…,n为节点编号。每个节点都有一个分数(均为正整数),记第j个节点的分数为di,t......
  • NC16645 [NOIP2007]矩阵取数游戏
    题目链接题目题目描述帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数。游戏规则如下:1.每次取数时须从每行各取走一个元素,......