题意:

给你9*9的矩阵。对于每一个数字。能减16代表上面有墙,能减32代表以下有墙。

。。

最后剩下的数字是0代表这个位置数要求,不是0代表这个数已知了。

然后通过墙会被数字分成9块。

然后做数独,这里的数独不是分成9个3*3的小块而是通过墙分成的。

思路:

首先通过数字作出墙。

然后bfs求连通块。dfs也能够。目的是分块。

然后就是dlx数独模板题了。

这里要注意的是假设找到答案2次就说明有多组解了。就应该停止返回了。不然会TLE。

代码:

  1. #include"stdio.h"
  2. #include"algorithm"
  3. #include"string.h"
  4. #include"iostream"
  5. #include"cmath"
  6. #include"queue"
  7. #include"map"
  8. #include"vector"
  9. #include"string"
  10. using namespace std;
  11. #define RN 9*9*9+5
  12. #define CN 4*9*9+5
  13. #define N 9*9*9*4+5
  14. int wall[12][12][12][12];
  15. int mp[12][12],used[12][12];
  16. int dis[4][2]= {{0,1},{0,-1},{-1,0},{1,0}};
  17. int kx,ff;
  18. template<class T>inline void getd(T &x)
  19. {
  20. int ch = getchar();
  21. bool minus = false;
  22. while(!isdigit(ch) && ch != '-')ch = getchar();
  23. if(ch == '-')minus = true, ch = getchar();
  24. x = ch - '0';
  25. while(isdigit(ch = getchar()))x = x * 10 - '0' + ch;
  26. if(minus)x = -x;
  27. }
  28.  
  29. struct node
  30. {
  31. int x,y;
  32. };
  33. void bfs(int x,int y)
  34. {
  35. node cur,next;
  36. cur.x=x;
  37. cur.y=y;
  38. queue<node>q;
  39. q.push(cur);
  40. used[cur.x][cur.y]=kx;
  41. while(!q.empty())
  42. {
  43. cur=q.front();
  44. q.pop();
  45. for(int i=0; i<4; i++)
  46. {
  47. next.x=cur.x+dis[i][0];
  48. next.y=cur.y+dis[i][1];
  49. if(used[next.x][next.y]!=0 || wall[cur.x][cur.y][next.x][next.y]==1 ) continue;
  50. used[next.x][next.y]=kx;
  51. q.push(next);
  52. }
  53. }
  54. kx++;
  55. return ;
  56. }
  57.  
  58. struct DLX
  59. {
  60. int n,m,C;
  61. int U[N],D[N],L[N],R[N],Row[N],Col[N];
  62. int H[RN],S[CN],cnt,ans[RN];
  63. void init(int _n,int _m)
  64. {
  65. n=_n;
  66. m=_m;
  67. for(int i=0; i<=m; i++)
  68. {
  69. U[i]=D[i]=i;
  70. L[i]=(i==0?
  71.  
  72. m:i-1);
  73. R[i]=(i==m?0:i+1);
  74. S[i]=0;
  75. }
  76. C=m;
  77. for(int i=1; i<=n; i++) H[i]=-1;
  78. }
  79. void link(int x,int y)
  80. {
  81. C++;
  82. Row[C]=x;
  83. Col[C]=y;
  84. S[y]++;
  85. U[C]=U[y];
  86. D[C]=y;
  87. D[U[y]]=C;
  88. U[y]=C;
  89. if(H[x]==-1) H[x]=L[C]=R[C]=C;
  90. else
  91. {
  92. L[C]=L[H[x]];
  93. R[C]=H[x];
  94. R[L[H[x]]]=C;
  95. L[H[x]]=C;
  96. }
  97. }
  98. void del(int x)
  99. {
  100. R[L[x]]=R[x];
  101. L[R[x]]=L[x];
  102. for(int i=D[x]; i!=x; i=D[i])
  103. {
  104. for(int j=R[i]; j!=i; j=R[j])
  105. {
  106. U[D[j]]=U[j];
  107. D[U[j]]=D[j];
  108. S[Col[j]]--;
  109. }
  110. }
  111. }
  112. void rec(int x)
  113. {
  114. for(int i=U[x]; i!=x; i=U[i])
  115. {
  116. for(int j=L[i]; j!=i; j=L[j])
  117. {
  118. U[D[j]]=j;
  119. D[U[j]]=j;
  120. S[Col[j]]++;
  121. }
  122. }
  123. R[L[x]]=x;
  124. L[R[x]]=x;
  125. }
  126. void dance(int x)
  127. {
  128. if(R[0]==0)
  129. {
  130. ff++;
  131. if(ff>=2) return ;
  132. cnt=x;
  133. for(int i=0; i<cnt; i++)
  134. {
  135. int tep=ans[i]-1;
  136. int a=tep/81,b=(tep%81)/9;
  137. mp[a+1][b+1]=tep%9+1;
  138. }
  139. return ;
  140. }
  141. int now=R[0];
  142. for(int i=R[0]; i!=0; i=R[i])
  143. {
  144. if(S[i]<S[now]) now=i;
  145. }
  146. del(now);
  147. for(int i=D[now]; i!=now; i=D[i])
  148. {
  149. ans[x]=Row[i];
  150. for(int j=R[i]; j!=i; j=R[j]) del(Col[j]);
  151. dance(x+1);
  152. if(ff>=2) return ;
  153. for(int j=L[i]; j!=i; j=L[j]) rec(Col[j]);
  154. }
  155. rec(now);
  156. return ;
  157. }
  158. } dlx;
  159. void getplace(int i,int j,int k,int &x,int &a,int &b,int &c)
  160. {
  161. x=(i-1)*81+(j-1)*9+k;
  162. a=81+(i-1)*9+k;
  163. b=81*2+(j-1)*9+k;
  164. c=81*3+(used[i][j]-1)*9+k;
  165. }
  166. int main()
  167. {
  168. int t,cas=1;
  169. cin>>t;
  170. while(t--)
  171. {
  172. memset(wall,0,sizeof(wall));
  173. for(int i=1; i<=9; i++)
  174. {
  175. for(int j=1; j<=9; j++)
  176. {
  177. int x;
  178. getd(x);
  179. if(x-128>=0)
  180. {
  181. x-=128;
  182. wall[i][j][i][j-1]=1;
  183. }
  184. if(x-64>=0)
  185. {
  186. x-=64;
  187. wall[i][j][i+1][j]=1;
  188. }
  189. if(x-32>=0)
  190. {
  191. x-=32;
  192. wall[i][j][i][j+1]=1;
  193. }
  194. if(x-16>=0)
  195. {
  196. x-=16;
  197. wall[i][j][i-1][j]=1;
  198. }
  199. mp[i][j]=x;
  200. }
  201. }
  202. kx=1;
  203. memset(used,0,sizeof(used));
  204. for(int i=1; i<=9; i++) for(int j=1; j<=9; j++) if(used[i][j]==0) bfs(i,j);
  205.  
  206. dlx.init(9*9*9,4*9*9);
  207. for(int i=1; i<=9; i++)
  208. {
  209. for(int j=1; j<=9; j++)
  210. {
  211. int tep=(i-1)*9+j;
  212. int x,a,b,c;
  213. if(mp[i][j]==0)
  214. {
  215. for(int k=1; k<=9; k++)
  216. {
  217. getplace(i,j,k,x,a,b,c);
  218. dlx.link(x,tep);
  219. dlx.link(x,a);
  220. dlx.link(x,b);
  221. dlx.link(x,c);
  222. }
  223. }
  224. else
  225. {
  226. getplace(i,j,mp[i][j],x,a,b,c);
  227. dlx.link(x,tep);
  228. dlx.link(x,a);
  229. dlx.link(x,b);
  230. dlx.link(x,c);
  231. }
  232. }
  233. }
  234. ff=0;
  235. dlx.dance(0);
  236. printf("Case %d:\n",cas++);
  237. if(ff==0) puts("No solution");
  238. else if(ff==1)
  239. {
  240. for(int i=1; i<=9; i++) for(int j=1; j<=9; j++) printf(j==9?"%d\n":"%d",mp[i][j]);
  241. }
  242. else puts("Multiple Solutions");
  243. }
  244. return 0;
  245. }

[DLX+bfs] hdu 4069 Squiggly Sudoku的更多相关文章

  1. 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 ...

  2. (中等) HDU 4069 Squiggly Sudoku , DLX+精确覆盖。

    Description Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that ...

  3. [DLX]HDOJ4069 Squiggly Sudoku

    题意:有9*9的格子 每个格子 由五部分组成:上(16).右(32).下(64).左(128).和该格的数值(0~9) 若上下左右有分割格子的线 就加上相应的数, 该格的数值若为0,则是未知  1~9 ...

  4. hdu 4069 福州赛区网络赛I DLC ***

    再遇到一个DLC就刷个专题 #include <stdio.h> #include <string.h> #include <iostream> #include ...

  5. 搜索(DLX): POJ 3074 3076 Sudoku

    POJ 3074 : Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller ...

  6. hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...

  7. ZOJ-1649 Rescue BFS (HDU 1242)

    看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...

  8. (hdu)5547 Sudoku (4*4方格的 数独 深搜)

    Problem Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game ...

  9. hdu 4069 垃圾数独

    首先dfs给每个格子分一个大的区块 其次套板子就a 我一开始直接在选取行的时候填数独,发现超时 我这一行也就4个元素,找到 x <= 81 的列计算元素位置,81 < x <= 16 ...

随机推荐

  1. webpack的详细介绍和使用

    // 一个常见的`webpack`配置文件 const webpack = require('webpack'); const HtmlWebpackPlugin = require('html-we ...

  2. vue-cli 中使用less

    (1)安装Less模块: npm install less (2)安装less和less-loader,命令如下 npm install less less-loader --sava-dev (3) ...

  3. 08C#事件

    C#事件 1.2      事件 事件是C#语言内置的语法,可以定义和处理事件,为使用组件编程提供了良好的基础. 1.16.1       事件驱动 Windows操作系统把用户的动作都看作消息,C# ...

  4. JavaSE-13 内部类

    学习要点 内部类的定义 内部类的应用 内部类 定义 Java的一个类中包含着另一类. A类和B类是C类的外部类.B类是C类的外部类.A类也称为顶层类. 如何使用内部类 public class MyF ...

  5. python circle nested

    #!/usr/bin/python # -*- coding:utf- -*- # @filename: tmp2 # @author:vickey # @date: // : def circle_ ...

  6. MySQL-----备份(转储)

    备份: **备份数据表结构+数据** mysqldump -u root 要备份的数据库表名 > 要备份的数据的备份名(这里也可以指定路径) -p **备份数据表结构** mysqldump - ...

  7. BNUOJ 7697 Information Disturbing

    Information Disturbing Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on HDU. ...

  8. Leetcode 211.添加与搜索单词

    添加与搜索单词 设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字 ...

  9. Spark 动态(统一)内存管理模型

    作者编辑:王玮,胡玉林 一.回顾 在前面的一篇文章中我们介绍了spark静态内存管理模式以及相关知识https://blog.csdn.net/anitinaj/article/details/809 ...

  10. 总结:常用的Linux系统监控命令(2)

    判断I/O瓶颈 mpstat命令 命令:mpstat -P ALL 1 1000 结果显示: 注意一下这里面的%iowait列,CPU等待I/O操作所花费的时间.这个值持续很高通常可能是I/O瓶颈所导 ...