Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21612   Accepted: 10274   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. 1
  2. 103000509
  3. 002109400
  4. 000704000
  5. 300502006
  6. 060000050
  7. 700803004
  8. 000401000
  9. 009205800
  10. 804000107

Sample Output

  1. 143628579
  2. 572139468
  3. 986754231
  4. 391542786
  5. 468917352
  6. 725863914
  7. 237481695
  8. 619275843
  9. 854396127

Source

题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行、每列、每个子网格内都只能使用一次1~9中的一个数字,即每行、每列、每个子网格内都不允许出现相同的数字,填完数独。

分析:直接搜索,标记行、列、块,值得一提的是倒着搜比正着搜效率高出许多,这也算是一个技巧。

代码:

  1. ////#include "bits/stdc++.h"
  2. #include "cstdio"
  3. #include "map"
  4. #include "set"
  5. #include "cmath"
  6. #include "queue"
  7. #include "vector"
  8. #include "string"
  9. #include "cstring"
  10. #include "time.h"
  11. #include "iostream"
  12. #include "stdlib.h"
  13. #include "algorithm"
  14. #define db double
  15. #define ll long long
  16. #define vec vector<ll>
  17. #define Mt vector<vec>
  18. #define ci(x) scanf("%d",&x)
  19. #define cd(x) scanf("%lf",&x)
  20. #define cl(x) scanf("%lld",&x)
  21. #define pi(x) printf("%d\n",x)
  22. #define pd(x) printf("%f\n",x)
  23. #define pl(x) printf("%lld\n",x)
  24. #define rep(i, x, y) for(int i=x;i<=y;i++)
  25. const int N = 1e6 + ;
  26. const int mod = 1e9 + ;
  27. const int MOD = mod - ;
  28. const db eps = 1e-;
  29. const db PI = acos(-1.0);
  30. using namespace std;
  31. int t;
  32.  
  33. int R()
  34. {
  35. int x=,f=;char ch=getchar();
  36. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  37. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  38. return x*f;
  39. }
  40. char s[][];
  41. int a[][];
  42. bool ok(int ans,int x,int y)
  43. {
  44. for(int i=;i<;i++)
  45. if(a[i][y]==ans) return ;
  46. for(int i=;i<;i++)
  47. if(a[x][i]==ans) return ;
  48. int xx=x-x%,yy=y-y%;
  49. for(int i=xx;i<xx+;i++)
  50. for(int j=yy;j<yy+;j++)
  51. if(a[i][j]==ans) return ;
  52. return ;
  53. }
  54. bool okk=;
  55. void dfs(int x,int y,int cnt)
  56. {
  57. if(cnt==){
  58. okk=;//满足条件后立刻结束,并标记
  59. return;
  60. }
  61. while(a[x][y]){
  62. if(y==) x++,y=;
  63. else y++;
  64. if(x==) {okk=;return;}//满足条件后立刻结束,并标记
  65. }
  66. for(int i=;i<=;i++){
  67. if(ok(i,x,y)){
  68. a[x][y]=i;
  69. if(y==) dfs(x+,,cnt+);
  70. else dfs(x,y+,cnt+);
  71. if(okk) return;//满足条件后立刻结束
  72. a[x][y]=;
  73. }
  74. }
  75. return;
  76. }
  77. int main()
  78. {
  79. t=R();
  80. while(t--)
  81. {
  82. int cnt=;
  83. memset(a,, sizeof(a));
  84. memset(s,, sizeof(s));
  85. okk=;
  86. for(int i=;i<;i++)
  87. {
  88. scanf("%s",s[i]);
  89. for(int j=;j<;j++){
  90. a[i][j]=s[i][j]-'';
  91. if(!a[i][j]) cnt--;
  92. }
  93. }
  94. dfs(,,cnt);//输出即为满足条件的,结束后的情况。
  95. for(int i=;i<;i++){
  96. for(int j=;j<;j++){
  97. printf("%d",a[i][j]);
  98. }
  99. puts("");
  100. }
  101. }
  102. }
 

POJ 2676 数独(DFS)的更多相关文章

  1. POJ 2676 数独+dfs深搜

    数独 #include "cstdio" #include "cstring" #include "cstdlib" #include &q ...

  2. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  3. 随手练——POJ - 2676 数独 (回溯法)

    POJ - 2676 : http://poj.org/problem?id=2676: 解题思想 (大力出奇迹): 1. 依次在空格里面填上“1~9”,并检查这个数字是否合法(其所在的行.列,以及3 ...

  4. poj 2676 数独问题 dfs

    题意:完成数独程序,数独要求每行每列且每个3*3矩阵都必须是1~9的数字组成. 思路:dfs 用row[i][n] 记录第i行n存在  用col[j][n] 记录第j列n存在 grid[k][n] 记 ...

  5. poj 2676 Sudoku ( dfs )

    dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...

  6. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  7. POJ.3172 Scales (DFS)

    POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...

  8. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

  9. POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]

    题目链接:http://poj.org/problem?id=2676 Time Limit: 2000MS Memory Limit: 65536K Description Sudoku is a ...

随机推荐

  1. Excel 解析 (大文件读取)BingExcel

    最近在整理一个excel读取与写出的orm框架.使用的saxreader方式,支持百万级别的excel读取. 并且在通常使用中提供了监听的读取方式.如感兴趣的朋友可以稍微了解下 ,项目地址https: ...

  2. u-boot分析(八)----串口初始化

    u-boot分析(八) 上篇博文我们按照210的启动流程,分析到了内存初始化,今天我们继续按照u-boot的启动流程对串口的初始化进行分析. 今天我们会用到的文档: 1.        2440芯片手 ...

  3. 青石B2C商城

    平台: Windows 类型: 虚拟机镜像 软件包: azure commercial ecbluestone ecommerce ecommerce solution 服务优惠价: 按服务商许可协议 ...

  4. SSM事务

    问题描述:查询用户信息时想级联查出用户订单以及订单详情,在查询用户的时候JDBC是will be managed by Spring,但懒加载用户订单以及订单详情时就will not be manag ...

  5. Office加载项

    出自我的个人主页 Alvin Blog 前言 前一段时间公司做了有关Excel 加载项的开发,也遇到了很多坑,所以在此记录一下,有两个原因,1.留给以后在用到加载项的时候,复习所用,避免 跳进同一个坑 ...

  6. 开源框架 epics,开源画面编辑软件 edm

    epics Experimental Physics and Industrial Control System 一套开源软件框架,实验物理和工业控制系统 http://www.aps.anl.gov ...

  7. 从HTTP响应头判断是否命中CDN

    腾讯云: X-Cache-Lookup:Hit From MemCache 表示命中CDN节点的内存X-Cache-Lookup:Hit From Disktank 表示命中CDN节点的磁盘X-Cac ...

  8. 在你的andorid设备上运行netcore (Linux Deploy)

    最近注意到.net core 的新版本已经开始支持ARM 平台的CPU, 特意去Linux Deploy 中尝试了一下,真的可以运行 Welcome to Ubuntu 16.04 LTS (GNU/ ...

  9. 智能开关:orange pi one(arm linux)控制继电器

    大家都知道,继电器是用小电流去控制大电流运作的一种“自动开关”,在我们生活.工作中随处可见.现在的“智能家居”概念,有很多功能模块其实就是“智能开关”,远程开关.定时开关.条件触发开关等等. 下面介绍 ...

  10. 【转载】#335 - Accessing a Derived Class Using a Base Class Variable

    You can use a variable whose type is a base class to reference instances of a derived class. However ...