Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25356   Accepted: 11849   Special Judge

Description

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task. 

Input

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

Sample Input

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

Source

 
 
数独转化为精确覆盖问题
https://www.cnblogs.com/grenet/p/3163550.html
这个讲的很详细
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int S[maxn], head[maxn], vis[maxn];
int U[maxn], D[maxn], L[maxn], R[maxn];
int C[maxn], X[maxn];
int n, m, ans, ret, ans1; void init()
{
for(int i = ; i <= m; i++)
D[i] = i, U[i] = i, R[i] = i + , L[i] = i - ;
L[] = m, R[m] = ;
mem(S, ), mem(head, -);
ans = m + ;
} void delc(int c)
{
L[R[c]] = L[c], R[L[c]] = R[c];
for(int i = D[c]; i != c; i = D[i])
for(int j = R[i]; j != i; j = R[j])
U[D[j]] = U[j], D[U[j]] = D[j], S[C[j]]--; } void resc(int c)
{
for(int i = U[c]; i != c; i = U[i])
for(int j = L[i]; j != i; j = L[j])
U[D[j]] = j, D[U[j]] = j, S[C[j]]++;
L[R[c]] = c, R[L[c]] = c;
} void add(int r, int c)
{
ans++, S[c]++, C[ans] = c, X[ans] = r;
D[ans] = D[c];
U[ans] = c;
U[D[c]] = ans;
D[c] = ans;
if(head[r] < ) head[r] = L[ans] = R[ans] = ans;
else L[ans] = head[r], R[ans] = R[head[r]],L[R[head[r]]] = ans, R[head[r]] = ans;
} bool dfs(int sh)
{
if(!R[])
{
sort(vis, vis + );
int cnt = ;
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
int num = vis[cnt++]; num=num - (i * + j) * ;
printf("%d", num);
// cout << 111 << endl; }
printf("\n");
} return true;
}
int c = R[];
for(int i = R[]; i; i = R[i]) if(S[c] > S[i]) c = i;
delc(c);
for(int i = D[c]; i != c; i = D[i])
{
vis[sh] = X[i];
for(int j = R[i]; j != i; j = R[j])
delc(C[j]);
if(dfs(sh + )) return true;
for(int j = L[i]; j != i; j = L[j])
resc(C[j]);
}
resc(c);
return false;
} char str[][]; void build(int x, int y, int k)
{
ans1 = (x * + y - ) * + k;
add(ans1, x * + k);
add(ans1, + (y - ) * + k);
add(ans1, + x * + y);
int block = (y - ) / * + x / ;
add(ans1, + block * + k); } int main()
{ int T;
rd(T);
while(T--)
{
//ans1 = 0;
m = * * ;
init(); for(int i = ; i < ; i++)
{
rs(str[i]);
for(int j = ; j <= ; j++)
{
if(str[i][j - ] == '')
for(int k = ; k <= ; k++) build(i, j, k);
else
build(i, j, str[i][j - ] - '');
}
}
dfs(); } return ;
}
 

Sudoku POJ - 2676(DLX)的更多相关文章

  1. Easy Finding POJ - 3740 (DLX)

    显然这是一道dfs简单题 或许匹配也能做 然而用了dancing links 显然这也是一道模板题 好的吧 调了一上午 终于弄好了模板 Easy Finding Time Limit: 1000MS ...

  2. 舞蹈链(DLX)

    舞蹈链(DLX) Tags:搜索 作业部落 评论地址 一.概述 特别特别感谢这位童鞋His blog 舞蹈链是一种优美的搜索,就像下面这样跳舞- 舞蹈链用于解决精确覆盖或者重复覆盖的问题 你可以想象成 ...

  3. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  4. POJ 3076 / ZOJ 3122 Sudoku(DLX)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  5. HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...

  6. Sudoku POJ - 3076 (dfs+剪枝)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  7. POJ题目(转)

    http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj29 ...

  8. NOIP 2009 靶形数独(DLX)

    小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向Z 博士请教,Z 博士拿出了他最近发明的“靶形数独”,作 ...

  9. Repeater POJ - 3768 (分形)

    Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...

随机推荐

  1. OSS网页上传和断点续传(OSS配置篇)

    OSS网页上传和断点续传主要根据BrowserJS-SDK和相关文档整理而得,快速构建OSS上传应用 一.Bucket设置 浏览器中直接访问OSS需要开通Bucket的CORS设置 将allowed ...

  2. <iOS开发>之App上架流程(2017)

    本文主要介绍了App上架流程,以及上架过程中会遇到的一些问题. 一.App上架前的准备. 上架前,需要开发人员有苹果开发者账号,具体请阅读苹果开发者账号注册申请流程.本文是在已经拥有开发者账号的前提下 ...

  3. 在java中怎样获得当前日期时间

    Calendar cal = Calendar.getInstance();    java.text.SimpleDateFormat sdf = new SimpleDateFormat(&quo ...

  4. VMware虚拟机与Windows文件共享

    开发中,我们经常的需求是这样的:我想再Windows中进行快捷开发,但是想在linux中运行,那么需要将文件方便在linux中管理,基本可以分成两种方式: 1. 使用网络工具:vmware_tool工 ...

  5. cmake : undefined reference to dlopen, dlclose, dlsym and dlerror

    链接出了问题 添加头文件 #include <dlfcn.h> 添加库 target_link_libraries(PROJECT_NAME ${CMAKE_DL_LIBS})

  6. socket通信原理三次握手和四次握手详解

    对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1.         什么是TCP/IP.UDP?2.         Sock ...

  7. 你不知道的JavaScript——第二章:this全面解析

    1调用位置 调用栈:为了到达当前执行位置所调用的所有函数. function baz(){ //当前调用栈:baz //因此,当前调用位置是全局作用域 console.log('baz'); bar( ...

  8. 【git】如何去解决fatal: refusing to merge unrelated histories

    我在Github新建一个仓库,写了License,然后把本地一个写了很久仓库上传. 先pull,因为两个仓库不同,发现refusing to merge unrelated histories,无法p ...

  9. Linux基础学习笔记3-用户权限

    本章内容 用户user 令牌token,identity Linux用户:Uername/UID 管理员:root,0 普通用户:1-65535 系统用户:1-499,1-999(Centos7) 对 ...

  10. python爬虫之MongoDB测试环境安装

    一.   下载 从http://www.mongodb.org/downloads地址中下载:mongodb-linux-x86_64-2.4.11.tar 二.  安装 1>设置mongoDB ...