Sudoku

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Appoint description: 
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

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

Sample Output

  1. 527389416819426735436751829375692184194538267268174593643217958951843672782965341
  2. 416837529982465371735129468571298643293746185864351297647913852359682714128574936
  3.  
  4. 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
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <map>
  8. #include <cctype>
  9. using namespace std;
  10.  
  11. const int HEAD = ;
  12. const int SIZE = ( * ) * ( * );
  13. const int COL = * ;
  14. int U[SIZE],D[SIZE],L[SIZE],R[SIZE],S[SIZE],C[SIZE],N[SIZE],P_H[SIZE],P_C[SIZE];
  15. int COUNT;
  16. int TEMP[][];
  17. bool VIS_I[][],VIS_J[][],VIS_G[][];
  18. struct Node
  19. {
  20. int i;
  21. int j;
  22. int num;
  23. }ANS[];
  24.  
  25. void ini(void);
  26. void link(int,int,int,int,int,int,int);
  27. bool dancing(int);
  28. void remove(int);
  29. void resume(int);
  30. void debug(int);
  31. int main(void)
  32. {
  33. char s[];
  34. while(scanf(" %s",s + ) && strcmp(s + ,"end"))
  35. {
  36. ini();
  37. for(int i = ;i <= ;i ++)
  38. for(int j = ;j <= ;j ++)
  39. {
  40. int k = s[(i - ) * + j];
  41. int c_1,c_2,c_3,c_4;
  42. if(k != '.')
  43. {
  44. VIS_I[i][k - ''] = VIS_J[j][k - ''] = true;
  45. VIS_G[(i - ) / * + (j - ) / + ][k - ''] = true;
  46. c_1 = * + (i - ) * + k - '';
  47. c_2 = * + (j - ) * + k - '';
  48. c_3 = * + ((i - ) / * + (j - ) / ) * + k - '';
  49. c_4 = * + (i - ) * + j;
  50. link(c_1,c_2,c_3,c_4,k - '',i,j);
  51. }
  52. }
  53.  
  54. for(int i = ;i <= ;i ++)
  55. for(int j = ;j <= ;j ++)
  56. {
  57. if(s[(i - ) * + j] != '.')
  58. continue;
  59. int c_1,c_2,c_3,c_4;
  60. for(int k = ;k <= ;k ++)
  61. {
  62. if(VIS_I[i][k] || VIS_J[j][k] ||
  63. VIS_G[(i - ) / * + (j - ) / + ][k])
  64. continue;
  65. c_1 = * + (i - ) * + k;
  66. c_2 = * + (j - ) * + k;
  67. c_3 = * + ((i - ) / * + (j - ) / ) * + k;
  68. c_4 = * + (i - ) * + j;
  69. link(c_1,c_2,c_3,c_4,k,i,j);
  70. }
  71. }
  72. dancing();
  73. }
  74.  
  75. return ;
  76. }
  77.  
  78. void ini(void)
  79. {
  80. L[HEAD] = COL;
  81. R[HEAD] = ;
  82. for(int i = ;i <= COL;i ++)
  83. {
  84. L[i] = i - ;
  85. R[i] = i + ;
  86. U[i] = D[i] = C[i] = i;
  87. S[i] = ;
  88. }
  89. R[COL] = HEAD;
  90.  
  91. fill(&VIS_I[][],&VIS_I[][],false);
  92. fill(&VIS_J[][],&VIS_J[][],false);
  93. fill(&VIS_G[][],&VIS_G[][],false);
  94. COUNT = COL + ;
  95. }
  96.  
  97. void link(int c_1,int c_2,int c_3,int c_4,int num,int r,int c)
  98. {
  99. int first = COUNT;
  100. int col;
  101.  
  102. for(int i = ;i < ;i ++)
  103. {
  104. switch(i)
  105. {
  106. case :col = c_1;break;
  107. case :col = c_2;break;
  108. case :col = c_3;break;
  109. case :col = c_4;break;
  110. }
  111.  
  112. L[COUNT] = COUNT - ;
  113. R[COUNT] = COUNT + ;
  114. U[COUNT] = U[col];
  115. D[COUNT] = col;
  116.  
  117. D[U[col]] = COUNT;
  118. U[col] = COUNT;
  119. C[COUNT] = col;
  120. N[COUNT] = num;
  121. P_H[COUNT] = r;
  122. P_C[COUNT] = c;
  123. S[col] ++;
  124. COUNT ++;
  125. }
  126. L[first] = COUNT - ;
  127. R[COUNT - ] = first;
  128. }
  129.  
  130. bool dancing(int k)
  131. {
  132. if(R[HEAD] == HEAD)
  133. {
  134. for(int i = ;i < k;i ++)
  135. TEMP[ANS[i].i][ANS[i].j] = ANS[i].num;
  136. int count = ;
  137. for(int i = ;i <= ;i ++)
  138. for(int j = ;j <= ;j ++)
  139. printf("%d",TEMP[i][j]);
  140. puts("");
  141. return true;
  142. }
  143.  
  144. int c = R[HEAD];
  145. for(int i = R[HEAD];i != HEAD;i = R[i])
  146. if(S[c] > S[i])
  147. c = i;
  148.  
  149. remove(c);
  150. for(int i = D[c];i != c;i = D[i])
  151. {
  152. ANS[k].i = P_H[i];
  153. ANS[k].j = P_C[i];
  154. ANS[k].num = N[i];
  155. for(int j = R[i];j != i;j = R[j])
  156. remove(C[j]);
  157. if(dancing(k + ))
  158. return true;
  159. for(int j = L[i];j != i;j = L[j])
  160. resume(C[j]);
  161. }
  162. resume(c);
  163.  
  164. return false;
  165. }
  166.  
  167. void remove(int c)
  168. {
  169. L[R[c]] = L[c];
  170. R[L[c]] = R[c];
  171. for(int i = D[c];i != c;i = D[i])
  172. for(int j = R[i];j != i;j = R[j])
  173. {
  174. D[U[j]] = D[j];
  175. U[D[j]] = U[j];
  176. S[C[j]] --;
  177. }
  178. }
  179.  
  180. void resume(int c)
  181. {
  182. L[R[c]] = c;
  183. R[L[c]] = c;
  184. for(int i = D[c];i != c;i = D[i])
  185. for(int j = L[i];j != i;j = L[j])
  186. {
  187. D[U[j]] = j;
  188. U[D[j]] = j;
  189. S[C[j]] ++;
  190. }
  191. }

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

  1. POJ 3074 Sudoku DLX精确覆盖

    DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8336   Accepted: ...

  2. (简单) POJ 3074 Sudoku, DLX+精确覆盖。

    Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgr ...

  3. POJ 3074 Sudoku (Dancing Links)

    传送门:http://poj.org/problem?id=3074 DLX 数独的9*9的模板题. 具体建模详见下面这篇论文.其中9*9的数独怎么转化到精确覆盖问题,以及相关矩阵行列的定义都在下文中 ...

  4. (简单) POJ 3076 Sudoku , DLX+精确覆盖。

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

  5. POJ 3076 Sudoku DLX精确覆盖

    DLX精确覆盖模具称号..... Sudoku Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 4416   Accepte ...

  6. poj 3074 Sudoku(Dancing Links)

    Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8152   Accepted: 2862 Descriptio ...

  7. POJ 3074 Sudoku(算竞进阶习题)

    二进制优化+dfs 话说这题数据中真的丧心病狂..不加inline还过不去.. 因为不会DLX只好用二进制来优化了...万万没想到还是低空飘过 我们在行.列.格分别用一个9位二进制常数来记录什么数能放 ...

  8. POJ - 3074 Sudoku (搜索)剪枝+位运算优化

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

  9. POJ 3074 Sudoku (Dacing Links)

    推荐一个写数独很好的博客:http://www.cnblogs.com/grenet/p/3163550.html 主要是把九宫格里的元素换到矩阵里面再求解dancing links 网上找的一模版 ...

随机推荐

  1. HiveContext VS SQLContext

    There are two ways to create context in Spark SQL: SqlContext:scala> import org.apache.spark.sql. ...

  2. HDU 5833 Zhu and 772002 (高斯消元)

    Zhu and 772002 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5833 Description Zhu and 772002 are b ...

  3. ActiveX控件是什么?

    一.ActiveX的由来 ActiveX最初只不过是一个商标名称而已,它所涵盖的技术并不是各自孤立的,其中多数都与Internet和Web有一定的关联.更重要的是,ActiveX的整体技术是由Micr ...

  4. HTML中RGB颜色查询对照表

    RGB颜色查询对照表     因为兼容性问题,色阶板功能只能在IE浏览器中运行 RGB颜色对照表   #FFFFFF   #FFFFF0   #FFFFE0   #FFFF00   #FFFAFA   ...

  5. flask前后台交互数据的几个思路

    通过url进行参数传递: @app.route('/hello/<name>') # <name>为传递的参数 def hello(name=None): return ren ...

  6. ADUM1201在隔离RS232中的应用 【瓦特芯收藏】

    ADUM1201在隔离RS232中的应用 引言: RS-232是PC机与工业通信中应用最广泛的一种串行接口.RS-232接口最初是由美国EIA(电子工业联合会)规定的用于计算机与终端设备之间通讯的一种 ...

  7. VBScript连接数据库

    'access类型 dim strconn,objconn strconn="driver=microsoft access driver(*.mdb);dbq=" _ & ...

  8. java的集合类【转】

    在JDK API中专门设计了一组类,这组类的功能就是实现各种各样方式的数据存储,这样一组专门用来存储其它对象的类,一般被称为对象容器类,简称容器类,这组类和接口的设计结构也被统称为集合框架(Colle ...

  9. Android实现图表绘制和展示

    本文演示在Android平台中绘制和展示图表示例,本示例是基于RChart 2实现的. 在一个系统中经常要用到图表统计数据,在WEB开发中图表绘制是一件简单的事情,因为有比较多的开源方案.但在Andr ...

  10. aspose.cells 模版

    aspose.cells 模版  http://www.cnblogs.com/whitewolf/archive/2011/03/21/Aspose_Cells_Template1.html