题目链接:https://ac.nowcoder.com/acm/contest/1014/B

题目描述

In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example,

Given some of the numbers in the grid, your goal is to determine the remaining numbers such that the numbers 1 through 9 appear exactly once in (1) each of nine 3 × 3 subgrids, (2) each of the nine rows, and (3) each of the nine columns.

输入描述:

The input test file will contain multiple cases. Each test case consists of a single line containing 81 characters, which represent the 81 squares of the Sudoku grid, given one row at a time. Each character is either a digit (from 1 to 9) or a period (used to indicate an unfilled square). You may assume that each puzzle in the input will have exactly one solution. The end-of-file is denoted by a single line containing the word “end”.

输出描述:

For each test case, print a line representing the completed Sudoku puzzle.

示例1

输入

.2738..1..1...6735.......293.5692.8...........6.1745.364.......9518...7..8..6534.
......52..8.4......3...9...5.1...6..2..7........3.....6...1..........7.4.......3.
end

输出

527389416819426735436751829375692184194538267268174593643217958951843672782965341
416837529982465371735129468571298643293746185864351297647913852359682714128574936

分析

  • 可以从左上角一行一行扫描到右下角,对于每一个块列举每一种可能,然后从每个可能出发继续深度遍历直到发现有一个块没有数字可以填时停止
  • 如何储存每一块可以填写的数字?可以利用九位二进制数来表示每一行,每一列,每个九宫格的数字填写情况,然后直接对这三个数字做按位与运算就可以得到某一具体块可以填的数字了。
  • 这里直接用bitset,对于每一个结果,直接遍历一下就可以了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char str[10][10];
int row[9], col[9], grid[9], cnt[512], num[512], tot; inline int g(int x, int y) {
return ((x / 3) * 3) + (y / 3);
} inline void flip(int x, int y, int z) {
row[x] ^= 1 << z;
col[y] ^= 1 << z;
grid[g(x, y)] ^= 1 << z;
} bool dfs(int now) {
if (now == 0) return 1;
int temp = 10, x, y;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) {
if (str[i][j] != '.') continue;
int val = row[i] & col[j] & grid[g(i, j)];
if (!val) return 0;
if (cnt[val] < temp) {
temp = cnt[val];
x = i, y = j;
}
}
int val = row[x] & col[y] & grid[g(x, y)];
for (; val; val -= val&-val) {
int z = num[val&-val];
str[x][y] = '1' + z;
flip(x, y, z);
if (dfs(now - 1)) return 1;
flip(x, y, z);
str[x][y] = '.';
}
return 0;
} int main() {
for (int i = 0; i < 1 << 9; i++)
for (int j = i; j; j -= j&-j) cnt[i]++;
for (int i = 0; i < 9; i++)
num[1 << i] = i;
char s[100];
while (~scanf("%s", s) && s[0] != 'e') {
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) str[i][j] = s[i * 9 + j];
for (int i = 0; i < 9; i++) row[i] = col[i] = grid[i] = (1 << 9) - 1;
tot = 0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (str[i][j] != '.') flip(i, j, str[i][j] - '1');
else tot++;
dfs(tot);
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) s[i * 9 + j] = str[i][j];
puts(s);
}
}

POJ 2726、POJ3074 :数独(二进制DFS)的更多相关文章

  1. POJ 2676/2918 数独(dfs)

    思路:记录每行每列每一个宫已经出现的数字就可以.数据比較弱 另外POJ 3074 3076 必须用剪枝策略.但实现较麻烦,还是以后学了DLX再来做吧 //Accepted 160K 0MS #incl ...

  2. POJ - 2676 Sudoku 数独游戏 dfs神奇的反搜

    Sudoku Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smalle ...

  3. poj Sudoku(数独) DFS

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13665   Accepted: 6767   Special ...

  4. POJ 1321-棋盘问题(DFS 递归)

    POJ 1321-棋盘问题 K - DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  5. poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析

    题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...

  6. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

  7. poj 2676 数独问题 dfs

    题意:完成数独程序,数独要求每行每列且每个3*3矩阵都必须是1~9的数字组成. 思路:dfs 用row[i][n] 记录第i行n存在  用col[j][n] 记录第j列n存在 grid[k][n] 记 ...

  8. 数独求解 DFS && DLX

    题目:Sudoku 题意:求解数独.从样例和结果来看应该是简单难度的数独 思路:DFS 设置3个数组,row[i][j] 判断第i行是否放了j数字,col[i][j] 判断第i列是否放了j数字.squ ...

  9. POJ 2386——Lake Counting(DFS)

    链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...

  10. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

随机推荐

  1. 🔥🔥Java开发者的Python快速实战指南:探索向量数据库之文本搜索

    前言 如果说Python是跟随我的步伐学习的话,我觉得我在日常开发方面已经没有太大的问题了.然而,由于我没有Python开发经验,我思考着应该写些什么内容.我回想起学习Java时的学习路线,直接操作数 ...

  2. Educational Codeforces Round 101 (Rated for Div. 2) E - A Bit Similar

    题目传送门 很巧妙的一道题.对于一个 \(n\)位的 \(01\)字符串,一共有 \(2^n\)种不同字符排列,对于任意一个固定排列,在 \(2^n\)种排列中只有一种排列与该固定排列处处不等,而题干 ...

  3. linux防火墙使用及配置

    Linux防火墙使用及配置 介绍 防火墙是网络安全的重要组成部分,它帮助保护服务器和计算机免受未经授权访问.恶意攻击和各种网络威胁.在Linux系统中,有一些工具和技术可用于设置和配置防火墙,其中最常 ...

  4. JavaWeb-JS基础

    4.JS基础 (1)JS的引入方式 HTML内部引入 将JS代码放在"< script >< /script >"标签之间 在HTML文档中,可以在任意地方 ...

  5. 在CPF里使用OpenGL做跨平台桌面应用开发

    CPF 是开源的C#跨平台UI框架,支持使用OpenGL来渲染,可以用来硬件加速播放视频或者显示3D模型 实现原理其实就是Skia用OpenGL后端,Skia里绑定GLView的OpenGL纹理,将纹 ...

  6. 用Spring导致的无法运行Java文件的问题的解决方案

    一般来说,这个问题主要是出现在用idea社区版的Spring Initializr这个插件来创建Spring项目.接下来我会以图的方式进行解说. 第一步创建Spring项目: 这里用maven的方式进 ...

  7. out.print()

    在学习过程中发现跟着视频打out.print报错 查阅资料知道 新建Java工程时,应选择Java Enterprise而非Java里的webapplication(Java Enterprise会自 ...

  8. 探秘扫雷游戏的C语言实现

    1 引言 1.1 为什么写这篇文章? 项目仓库地址:基于 C 语言实现的扫雷游戏 我决定写这篇文章的初衷是想分享我在使用C语言开发扫雷游戏的经验和心得.通过这篇文章,我希望能够向读者展示我是如何利用C ...

  9. Map的特性(有序和无序)讨论

    目录 什么是红黑树? 在 Java 中,基础java.util.Map 接口本身并不保证元素的顺序.具体的实现类 HashMap 和 TreeMap 的行为(无序.有序)有所不同: HashMap 类 ...

  10. 2023-09-13:用go语言,给定一个整数数组 nums 和一个正整数 k, 找出是否有可能把这个数组分成 k 个非空子集,其总和都相等。 输入: nums = [4, 3, 2, 3, 5,

    2023-09-13:用go语言,给定一个整数数组 nums 和一个正整数 k, 找出是否有可能把这个数组分成 k 个非空子集,其总和都相等. 输入: nums = [4, 3, 2, 3, 5, 2 ...