POJ 3074 Sudoku (DLX)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
System Crawler (2015-04-18)
Description
In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example,
. | 2 | 7 | 3 | 8 | . | . | 1 | . |
. | 1 | . | . | . | 6 | 7 | 3 | 5 |
. | . | . | . | . | . | . | 2 | 9 |
3 | . | 5 | 6 | 9 | 2 | . | 8 | . |
. | . | . | . | . | . | . | . | . |
. | 6 | . | 1 | 7 | 4 | 5 | . | 3 |
6 | 4 | . | . | . | . | . | . | . |
9 | 5 | 1 | 8 | . | . | . | 7 | . |
. | 8 | . | . | 6 | 5 | 3 | 4 | . |
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.
Input
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”.
Output
For each test case, print a line representing the completed Sudoku puzzle.
Sample Input
- .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
Sample Output
- 527389416819426735436751829375692184194538267268174593643217958951843672782965341
- 416837529982465371735129468571298643293746185864351297647913852359682714128574936
- 用dancing links解数独,DLX专题的终极目标,手机上的数独玩了两天后终于A了,等下把记录用这个程序刷一遍,想象下小伙伴看到我最高难度下的耗时记录的表情,啊哈哈~~
难点在于怎么建立模型,一共有9行,每行有9个数字,所以就是9 * 9,同理,列也是9 * 9,小格子也是有9个,每个也是9个数字,所以也是9 * 9,另外整个图有9 * 9 = 81的格子。所以要覆盖的列就是 9 * 9 + 9 * 9 + 9 * 9 + 81。至于行,一共有81个格子,每个格子有9种取法,所以就有81 * 9行,行和列相乘就是开的数组的大小。还有个剪枝要注意下,如果某个格子的数字已经给出,那么这一行,这一列,这一个小格子,就没必要再填这个数了,不剪枝的话会T。
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- #include <map>
- #include <cctype>
- using namespace std;
- const int HEAD = ;
- const int SIZE = ( * ) * ( * );
- const int COL = * ;
- int U[SIZE],D[SIZE],L[SIZE],R[SIZE],S[SIZE],C[SIZE],N[SIZE],P_H[SIZE],P_C[SIZE];
- int COUNT;
- int TEMP[][];
- bool VIS_I[][],VIS_J[][],VIS_G[][];
- struct Node
- {
- int i;
- int j;
- int num;
- }ANS[];
- void ini(void);
- void link(int,int,int,int,int,int,int);
- bool dancing(int);
- void remove(int);
- void resume(int);
- void debug(int);
- int main(void)
- {
- char s[];
- while(scanf(" %s",s + ) && strcmp(s + ,"end"))
- {
- ini();
- for(int i = ;i <= ;i ++)
- for(int j = ;j <= ;j ++)
- {
- int k = s[(i - ) * + j];
- int c_1,c_2,c_3,c_4;
- if(k != '.')
- {
- VIS_I[i][k - ''] = VIS_J[j][k - ''] = true;
- VIS_G[(i - ) / * + (j - ) / + ][k - ''] = true;
- c_1 = * + (i - ) * + k - '';
- c_2 = * + (j - ) * + k - '';
- c_3 = * + ((i - ) / * + (j - ) / ) * + k - '';
- c_4 = * + (i - ) * + j;
- link(c_1,c_2,c_3,c_4,k - '',i,j);
- }
- }
- for(int i = ;i <= ;i ++)
- for(int j = ;j <= ;j ++)
- {
- if(s[(i - ) * + j] != '.')
- continue;
- int c_1,c_2,c_3,c_4;
- for(int k = ;k <= ;k ++)
- {
- if(VIS_I[i][k] || VIS_J[j][k] ||
- VIS_G[(i - ) / * + (j - ) / + ][k])
- continue;
- c_1 = * + (i - ) * + k;
- c_2 = * + (j - ) * + k;
- c_3 = * + ((i - ) / * + (j - ) / ) * + k;
- c_4 = * + (i - ) * + j;
- link(c_1,c_2,c_3,c_4,k,i,j);
- }
- }
- dancing();
- }
- return ;
- }
- void ini(void)
- {
- L[HEAD] = COL;
- R[HEAD] = ;
- for(int i = ;i <= COL;i ++)
- {
- L[i] = i - ;
- R[i] = i + ;
- U[i] = D[i] = C[i] = i;
- S[i] = ;
- }
- R[COL] = HEAD;
- fill(&VIS_I[][],&VIS_I[][],false);
- fill(&VIS_J[][],&VIS_J[][],false);
- fill(&VIS_G[][],&VIS_G[][],false);
- COUNT = COL + ;
- }
- void link(int c_1,int c_2,int c_3,int c_4,int num,int r,int c)
- {
- int first = COUNT;
- int col;
- for(int i = ;i < ;i ++)
- {
- switch(i)
- {
- case :col = c_1;break;
- case :col = c_2;break;
- case :col = c_3;break;
- case :col = c_4;break;
- }
- L[COUNT] = COUNT - ;
- R[COUNT] = COUNT + ;
- U[COUNT] = U[col];
- D[COUNT] = col;
- D[U[col]] = COUNT;
- U[col] = COUNT;
- C[COUNT] = col;
- N[COUNT] = num;
- P_H[COUNT] = r;
- P_C[COUNT] = c;
- S[col] ++;
- COUNT ++;
- }
- L[first] = COUNT - ;
- R[COUNT - ] = first;
- }
- bool dancing(int k)
- {
- if(R[HEAD] == HEAD)
- {
- for(int i = ;i < k;i ++)
- TEMP[ANS[i].i][ANS[i].j] = ANS[i].num;
- int count = ;
- for(int i = ;i <= ;i ++)
- for(int j = ;j <= ;j ++)
- printf("%d",TEMP[i][j]);
- puts("");
- return true;
- }
- int c = R[HEAD];
- for(int i = R[HEAD];i != HEAD;i = R[i])
- if(S[c] > S[i])
- c = i;
- remove(c);
- for(int i = D[c];i != c;i = D[i])
- {
- ANS[k].i = P_H[i];
- ANS[k].j = P_C[i];
- ANS[k].num = N[i];
- for(int j = R[i];j != i;j = R[j])
- remove(C[j]);
- if(dancing(k + ))
- return true;
- for(int j = L[i];j != i;j = L[j])
- resume(C[j]);
- }
- resume(c);
- return false;
- }
- void remove(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])
- {
- D[U[j]] = D[j];
- U[D[j]] = U[j];
- S[C[j]] --;
- }
- }
- void resume(int c)
- {
- L[R[c]] = c;
- R[L[c]] = c;
- for(int i = D[c];i != c;i = D[i])
- for(int j = L[i];j != i;j = L[j])
- {
- D[U[j]] = j;
- U[D[j]] = j;
- S[C[j]] ++;
- }
- }
POJ 3074 Sudoku (DLX)的更多相关文章
- POJ 3074 Sudoku DLX精确覆盖
DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8336 Accepted: ...
- (简单) POJ 3074 Sudoku, DLX+精确覆盖。
Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgr ...
- POJ 3074 Sudoku (Dancing Links)
传送门:http://poj.org/problem?id=3074 DLX 数独的9*9的模板题. 具体建模详见下面这篇论文.其中9*9的数独怎么转化到精确覆盖问题,以及相关矩阵行列的定义都在下文中 ...
- (简单) POJ 3076 Sudoku , DLX+精确覆盖。
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- POJ 3076 Sudoku DLX精确覆盖
DLX精确覆盖模具称号..... Sudoku Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 4416 Accepte ...
- poj 3074 Sudoku(Dancing Links)
Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8152 Accepted: 2862 Descriptio ...
- POJ 3074 Sudoku(算竞进阶习题)
二进制优化+dfs 话说这题数据中真的丧心病狂..不加inline还过不去.. 因为不会DLX只好用二进制来优化了...万万没想到还是低空飘过 我们在行.列.格分别用一个9位二进制常数来记录什么数能放 ...
- POJ - 3074 Sudoku (搜索)剪枝+位运算优化
In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...
- POJ 3074 Sudoku (Dacing Links)
推荐一个写数独很好的博客:http://www.cnblogs.com/grenet/p/3163550.html 主要是把九宫格里的元素换到矩阵里面再求解dancing links 网上找的一模版 ...
随机推荐
- HiveContext VS SQLContext
There are two ways to create context in Spark SQL: SqlContext:scala> import org.apache.spark.sql. ...
- HDU 5833 Zhu and 772002 (高斯消元)
Zhu and 772002 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5833 Description Zhu and 772002 are b ...
- ActiveX控件是什么?
一.ActiveX的由来 ActiveX最初只不过是一个商标名称而已,它所涵盖的技术并不是各自孤立的,其中多数都与Internet和Web有一定的关联.更重要的是,ActiveX的整体技术是由Micr ...
- HTML中RGB颜色查询对照表
RGB颜色查询对照表 因为兼容性问题,色阶板功能只能在IE浏览器中运行 RGB颜色对照表 #FFFFFF #FFFFF0 #FFFFE0 #FFFF00 #FFFAFA ...
- flask前后台交互数据的几个思路
通过url进行参数传递: @app.route('/hello/<name>') # <name>为传递的参数 def hello(name=None): return ren ...
- ADUM1201在隔离RS232中的应用 【瓦特芯收藏】
ADUM1201在隔离RS232中的应用 引言: RS-232是PC机与工业通信中应用最广泛的一种串行接口.RS-232接口最初是由美国EIA(电子工业联合会)规定的用于计算机与终端设备之间通讯的一种 ...
- VBScript连接数据库
'access类型 dim strconn,objconn strconn="driver=microsoft access driver(*.mdb);dbq=" _ & ...
- java的集合类【转】
在JDK API中专门设计了一组类,这组类的功能就是实现各种各样方式的数据存储,这样一组专门用来存储其它对象的类,一般被称为对象容器类,简称容器类,这组类和接口的设计结构也被统称为集合框架(Colle ...
- Android实现图表绘制和展示
本文演示在Android平台中绘制和展示图表示例,本示例是基于RChart 2实现的. 在一个系统中经常要用到图表统计数据,在WEB开发中图表绘制是一件简单的事情,因为有比较多的开源方案.但在Andr ...
- aspose.cells 模版
aspose.cells 模版 http://www.cnblogs.com/whitewolf/archive/2011/03/21/Aspose_Cells_Template1.html