Description

A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells are filled with letters from A to P (the first 16 capital letters of the English alphabet), as shown in figure 1a. The game is to fill all the empty grid cells with letters from A to P such that each letter from the grid occurs once only in the line, the column, and the 4x4 square it occupies. The initial content of the grid satisfies the constraints mentioned above and guarantees a unique solution.


Write a Sudoku playing program that reads data sets from a text file.

Input

Each data set encodes a grid and contains 16 strings on 16 consecutive lines as shown in figure 2. The i-th string stands for the i-th line of the grid, is 16 characters long, and starts from the first position of the line. String characters are from the set {A,B,…,P,-}, where – (minus) designates empty grid cells. The data sets are separated by single empty lines and terminate with an end of file.

Output

The program prints the solution of the input encoded grids in the same format and order as used for input.

Sample Input

  1. --A----C-----O-I
  2. -J--A-B-P-CGF-H-
  3. --D--F-I-E----P-
  4. -G-EL-H----M-J--
  5. ----E----C--G---
  6. -I--K-GA-B---E-J
  7. D-GP--J-F----A--
  8. -E---C-B--DP--O-
  9. E--F-M--D--L-K-A
  10. -C--------O-I-L-
  11. H-P-C--F-A--B---
  12. ---G-OD---J----H
  13. K---J----H-A-P-L
  14. --B--P--E--K--A-
  15. -H--B--K--FI-C--
  16. --F---C--D--H-N-

Sample Output

  1. FPAHMJECNLBDKOGI
  2. OJMIANBDPKCGFLHE
  3. LNDKGFOIJEAHMBPC
  4. BGCELKHPOFIMAJDN
  5. MFHBELPOACKJGNID
  6. CILNKDGAHBMOPEFJ
  7. DOGPIHJMFNLECAKB
  8. JEKAFCNBGIDPLHOM
  9. EBOFPMIJDGHLNKCA
  10. NCJDHBAEKMOFIGLP
  11. HMPLCGKFIAENBDJO
  12. AKIGNODLBPJCEFMH
  13. KDEMJIFNCHGAOPBL
  14. GLBCDPMHEONKJIAF
  15. PHNOBALKMJFIDCEG
  16. IAFJOECGLDPBHMNK

题意:就是让每行,每列,每个4*4的十六宫格中A~P只出现一次。输出16*16的宫格信息。

思路:我总感觉这题用dancing links会好做很多

①肯定是选择可填入的字母最少的位置开始dfs,这样分支比较少

②如果一个位置只剩一个字母可以填,就填上这个字母(废话)

③如果所有的字母不能填在该行(列、十六宫格),立刻回溯

④如果某个字母只能填在该行(列、十六宫格)的某处,立刻填写

首先除了dfs的结束条件外,一次写下对位置的②剪枝,对行,列,十六宫格的③、④剪枝,因为②、④剪枝填了一个字母,需要再次判断结束条件,

然后就是对可能性最小的位置进行dfs

(代码参考网上,侵删)

  1. #include<iostream>
  2. #include<string.h>
  3. #include<cstdio>
  4.  
  5. using namespace std;
  6.  
  7. int maps[][];
  8. int table[][];
  9. int num;
  10.  
  11. void put_in(int x,int y,int k)
  12. {
  13. num++;
  14. maps[x][y] = k;
  15. table[x][y] |= <<k;
  16. for(int i=; i<=; i++)
  17. {
  18. table[i][y] |= <<k;
  19. table[x][i] |= <<k;
  20. }
  21. int r = (x-)/*+;
  22. int c = (y-)/*+;
  23. for(int i=; i<; i++)
  24. {
  25. for(int j=; j<; j++)
  26. {
  27. table[r+i][c+j] |= <<k;
  28. }
  29. }
  30. }
  31.  
  32. int _count(int x)
  33. {
  34. for(int i=; x; i++)
  35. {
  36. if(x & )
  37. {
  38. if(x>> == )
  39. return i;
  40. return -;
  41. }
  42. x >>= ;
  43. }
  44. return -;
  45. }
  46.  
  47. int row(int x,int k)
  48. {
  49. int t = -;
  50. for(int y=; y<=; y++)
  51. {
  52. if(maps[x][y] == k)
  53. return -;
  54. if(maps[x][y] >= )
  55. continue;
  56. if((table[x][y]&<<k) == )
  57. {
  58. if(t != -)
  59. return -;
  60. t = y;
  61. }
  62. }
  63. if(t != -)
  64. return t;
  65. return -;
  66. }
  67.  
  68. int col(int y,int k)
  69. {
  70. int t = -;
  71. for(int x=; x<=; x++)
  72. {
  73. if(maps[x][y] == k)
  74. return -;
  75. if(maps[x][y] >= )
  76. continue;
  77. if((table[x][y]&<<k) == )
  78. {
  79. if(t != -)
  80. return -;
  81. t = x;
  82. }
  83. }
  84. if(t != -)
  85. return t;
  86. return -;
  87. }
  88.  
  89. void cuble(int r,int c,int k,int &x,int &y)
  90. {
  91. x = -;
  92. for(int i=; i<; i++)
  93. {
  94. for(int j=; j<; j++)
  95. {
  96. if(maps[i+r][j+c] == k)
  97. {
  98. x=-;
  99. return;
  100. }
  101. if(maps[i+r][j+c] >= )
  102. continue;
  103. if((table[i+r][j+c]&<<k) == )
  104. {
  105. if(x != -)
  106. {
  107. x=-;
  108. return;
  109. }
  110. x = i;
  111. y = j;
  112. }
  113. }
  114. }
  115. }
  116.  
  117. int cal(int x)
  118. {
  119. int cnt = ;
  120. while(x)
  121. {
  122. if(x&)
  123. cnt++;
  124. x >>= ;
  125. }
  126. return cnt;
  127. }
  128.  
  129. bool dfs()
  130. {
  131. if(num == )
  132. {
  133. for(int i=; i<=; i++)
  134. {
  135. for(int j=; j<=; j++)
  136. {
  137. printf("%c",maps[i][j]+'A');
  138. }
  139. puts("");
  140. }
  141. puts("");
  142. return ;
  143. }
  144. for(int i=; i<=; i++)
  145. {
  146. for(int j=; j<=; j++)
  147. {
  148. if(maps[i][j] >= )
  149. continue;
  150. int k = _count(table[i][j]);
  151. if(k != -)
  152. put_in(i,j,k);
  153. }
  154. }
  155. for(int x=; x<=; x++)
  156. {
  157. for(int k=; k<; k++)
  158. {
  159. int y = row(x,k);
  160. if(y == -)
  161. return ;
  162. if(y != -)
  163. put_in(x,y,k);
  164.  
  165. }
  166. }
  167. for(int y=; y<=; y++)
  168. {
  169. for(int k=; k<; k++)
  170. {
  171. int x = col(y,k);
  172. if(x == -)
  173. return ;
  174. if(x != -)
  175. put_in(x,y,k);
  176.  
  177. }
  178. }
  179. for(int r=; r<=; r+=)
  180. {
  181. for(int c=; c<=; c+=)
  182. {
  183. for(int k=; k<; k++)
  184. {
  185. int x,y;
  186. cuble(r,c,k,x,y);
  187. if(x == -)
  188. return ;
  189. if(x != -)
  190. put_in(r+x,c+y,k);
  191.  
  192. }
  193. }
  194. }
  195. if(num == )
  196. {
  197. for(int i=; i<=; i++)
  198. {
  199. for(int j=; j<=; j++)
  200. {
  201. printf("%c",maps[i][j]+'A');
  202. }
  203. puts("");
  204. }
  205. puts("");
  206. return ;
  207. }
  208. int t_num;
  209. int t_maps[][];
  210. int t_table[][];
  211. t_num = num;
  212. for(int i=; i<=; i++)
  213. {
  214. for(int j=; j<=; j++)
  215. {
  216. t_maps[i][j] = maps[i][j];
  217. t_table[i][j] = table[i][j];
  218. }
  219. }
  220. int mx,my,mn=;
  221. for(int i=; i<=; i++)
  222. {
  223. for(int j=; j<=; j++)
  224. {
  225. if(maps[i][j]>=)
  226. continue;
  227. int r = - cal(table[i][j]);
  228. if(r < mn)
  229. {
  230. mn = r;
  231. mx = i,my = j;
  232. }
  233. }
  234. }
  235. for(int k=; k<; k++)
  236. {
  237. if((table[mx][my] & <<k) == )
  238. {
  239. put_in(mx,my,k);
  240. if(dfs())
  241. return ;
  242. num = t_num;
  243. for(int i=; i<=; i++)
  244. {
  245. for(int j=; j<=; j++)
  246. {
  247. maps[i][j] = t_maps[i][j];
  248. table[i][j] = t_table[i][j];
  249. }
  250. }
  251. }
  252. }
  253. return ;
  254. }
  255.  
  256. int main()
  257. {
  258. char s[];
  259. while(~scanf("%s",s))
  260. {
  261. num = ;
  262. memset(table,,sizeof(table));
  263. for(int j=; j<=; j++)
  264. {
  265. if(s[j-]!='-')
  266. put_in(,j,s[j-]-'A');
  267. else
  268. maps[][j] = -;
  269. }
  270. for(int i=; i<=; i++)
  271. {
  272. scanf("%s",s);
  273. for(int j=; j<=; j++)
  274. {
  275. if(s[j-]!='-')
  276. put_in(i,j,s[j-]-'A');
  277. else
  278. maps[i][j] = -;
  279. }
  280. }
  281. dfs();
  282. }
  283.  
  284. }

Sudoku POJ - 3076 (dfs+剪枝)的更多相关文章

  1. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  2. 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...

  3. Sudoku POJ - 3076

    Sudoku Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 5769   Accepted: 2684 Descripti ...

  4. HDU-6341 Problem J. Let Sudoku Rotate(dfs 剪枝)

    题目:有一个4*4*4*4的数独,每一横每一竖每一个小方块中都无重复的字母,即都为0-9,A-F..有一个已经填好的数独,若干个4*4的方块被逆时针拧转了若干次,问拧转回来至少需要多少次. 分析:很明 ...

  5. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  6. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

  7. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

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

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

  9. POJ 3076 Sudoku

    3076 思路: dfs + 剪枝 首先,如果这个位置只能填一种字母,那就直接填 其次,如果对于每一种字母,如果某一列或者某一行或者某一块只能填它,那就填它 然后,对于某个位置如果不能填字母了,或者某 ...

随机推荐

  1. 一篇文章教你读懂UI绘制流程

    最近有好多人问我Android没信心去深造了,找不到好的工作,其实我以一个他们进行回复,发现他们主要是内心比较浮躁,要知道技术行业永远缺少的是高手.建议先阅读浅谈Android发展趋势分析,在工作中, ...

  2. 信息摘要算法之六:HKDF算法分析与实现

    HKDF是一种特定的键衍生函数(KDF),即初始键控材料的功能,KDF从其中派生出一个或多个密码强大的密钥.在此我们想要描述的是基于HMAC的HKDF. 1.HKDF概述 密钥派生函数(KDF)是密码 ...

  3. mysql服务器没有响应

    第一步删除c:\windowns下面的my.ini(可以先改成其它的名字也行) 第二步打开对应安装目录下\mysql\bin\winmysqladmin.exe 输入用户名 和密码(也可以忽略此步) ...

  4. Zookeeper客户端Curator的使用,简单高效

    Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeeper客户端的开发量. 1.引入依赖: ...

  5. 【sqli-labs】Less17

    Less17: POST注入,UPDATE语句,有错误回显 新知识点: 1. update注入方法 参考:http://www.mamicode.com/info-detail-1665678.htm ...

  6. PDF裁剪页面,PDF怎么裁剪页面的方法

    PDF文件要怎么裁剪页面呢,是不是有很多的小伙们想知道呢,当打开一个PDF文件的时候如果一个页面中有很多的空白页面就会影响文件的美观与使用,今天小编就为大家分享一下小编的裁剪页面的方法. 操作软件:迅 ...

  7. Nginx详解二十一:Nginx深度学习篇之配置苹果要求的openssl后台HTTPS服务

    配置苹果要求的证书: 1.服务器所有的连接使用TLS1.2以上的版本(openssl 1.0.2) 2.HTTPS证书必须使用SHA256以上哈希算法签名 3.HTTPS证书必须使用RSA2048位或 ...

  8. js中json对象数组按对象属性排序(sort方法)---2(根据拼音排序汉字和排序英文)

    本例主要实现 中文汉字按拼音排序的方法和英文按照首字母排序的方法. 要排序的数据: //要排序的数据 let data = [ {chinese: '蔡司', english: 'Chase'}, { ...

  9. .tar.xz文件的解压方法

    废话不多说: 直接看 方法一: tar -xvJf ***.tar.gz 方法二: 先减压成 .tar 格式的文件, 再解压 .tar #xz是一个工具, 系统中没有安装,需要下载 xz -d *** ...

  10. c++ 链表基础功能实现

    #include<stack> struct ListNode { int m_nValue; ListNode* m_pNext; }; ListNode* CreateListNode ...