POJ 2676 Sudoku

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17627   Accepted: 8538   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
    我的思路错误之处:我本来只设置了两个二维bool数组表示每行9个数字和每列9个数字,是否用过,再深搜填写9个方格,可是发现,深搜填写每个方格是很难写的。
    正解:开是三个二维bool数组,分别表示每列每行每个大方格的数字的使用情况。
    读入时注意数字是连起来的。
  1. /*----------------正确代码-------------------------*/
  2. #include<iostream>
  3. using namespace std;
  4. #include<cstdio>
  5. #include<cstring>
  6. #define N 15
  7. char duru[N];
  8. int jz[N][N];
  9. bool flag=false,flagfg[N][N],flaghang[N][N],flaglie[N][N];
  10. inline void input()
  11. {
  12. for(int i=;i<=;++i)
  13. {
  14. scanf("%s",duru+);/*注意读入的数字之间没有空格*/
  15. for(int j=;j<=;++j)
  16. {
  17. jz[i][j]=duru[j]-'';
  18. if(jz[i][j]==) continue;
  19. int k=*((i-)/)+(j-)/+;
  20. flagfg[k][jz[i][j]]=true;
  21. flaghang[i][jz[i][j]]=true;
  22. flaglie[j][jz[i][j]]=true;
  23. }
  24. }
  25. }
  26. void output()
  27. {
  28. for(int i=;i<=;++i)
  29. {
  30. for(int j=;j<=;++j)
  31. printf("%d",jz[i][j]);
  32. printf("\n");
  33. }
  34. }
  35. void dfs(int x,int y)
  36. {
  37. if(x==)
  38. {
  39. flag=true;
  40. return;
  41. }
  42. if(jz[x][y])
  43. {
  44. if(y==)
  45. dfs(x+,);
  46. else dfs(x,y+);
  47. if(flag) return;
  48. }
  49. else {
  50. int k=*((x-)/)+(y-)/+;
  51. for(int i=;i<=;++i)
  52. {
  53. if(!flaghang[x][i]&&!flaglie[y][i]&&!flagfg[k][i])
  54. {/*枚举符合三个条件的i*/
  55. jz[x][y]=i;
  56. flaghang[x][i]=true;
  57. flaglie[y][i]=true;
  58. flagfg[k][i]=true;
  59. if(y==)
  60. dfs(x+,);
  61. else dfs(x,y+);
  62. if(flag) return;
  63. jz[x][y]=;/*回溯*/
  64. flaghang[x][i]=false;
  65. flaglie[y][i]=false;
  66. flagfg[k][i]=false;
  67.  
  68. }
  69. }
  70. }
  71. }
  72. int main()
  73. {
  74. int T;
  75. scanf("%d",&T);
  76. while(T--)
  77. {
  78. input();
  79. dfs(,);
  80. output();
  81. memset(jz,,sizeof(jz));
  82. memset(flagfg,false,sizeof(flagfg));
  83. memset(flaghang,false,sizeof(flaghang));
  84. memset(flaglie,false,sizeof(flaglie));
  85. flag=false;
  86. }
  87. return ;
  88. }

深搜+回溯 POJ 2676 Sudoku的更多相关文章

  1. HDU5723 Abandoned country (最小生成树+深搜回溯法)

    Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since aban ...

  2. ****Curling 2.0(深搜+回溯)

    Curling 2.0 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  3. ACM : POJ 2676 SudoKu DFS - 数独

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

  4. The 2016 ACM-ICPC Asia China-Final L World Cup(深搜+回溯 暴力求解)

    题目分析: 对于A,B,C,D四支队伍,两两之间进行一场比赛,获胜得3分,平局得1分,失败不得分,现在对给出的四个队伍的得分,判断能否满足得到这种分数,且方案唯一输出yes,不唯一输出no,不可能则输 ...

  5. UVA 165 Stamps (DFS深搜回溯)

     Stamps  The government of Nova Mareterrania requires that various legal documents have stamps attac ...

  6. POJ 2676 Sudoku(深搜)

    Sudoku Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submi ...

  7. POJ - 2676 Sudoku 数独游戏 dfs神奇的反搜

    Sudoku Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smalle ...

  8. POJ 2488 A Knight's Journey(深搜+回溯)

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  9. 搜索 --- 数独求解 POJ 2676 Sudoku

    Sudoku Problem's Link:   http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...

随机推荐

  1. web ppt

    先记录,以后再试试 https://github.com/gnab/remark/wiki http://segmentfault.com/blog/sweetdum/1190000000484121 ...

  2. 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)

    matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...

  3. mongodb学习5--mongo的type类型

    db.active.group({key:{id:1},cond:{cd:20160913,cid:"fgsdljsdv",aid:"54465"},reduc ...

  4. Hibernate的缓存技术详解

    转载注明出处:http://www.cnblogs.com/xiaoming0601/p/5882980.html 一.什么是缓存: 并不是指计算机的内存或者CPU的一二级缓存:缓存是指为了降低应用程 ...

  5. jQuery中的事件冒泡

    1.什么是冒泡 eg: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he ...

  6. 类库LinqToExcel的介绍

            LinqToExcel是一个.net framework平台下开源项目,它主要实现了LINQ的语法查询Excel电子表格.类型之前的LINQToXXX如果你是LINQ语法糖爱好者那最适 ...

  7. NLog在.NET Core Console Apps中的简单应用

    什么是NLog? NLog is a free logging platform for .NET with rich log routing and management capabilities. ...

  8. 硅谷新闻1--引导界面GuideActivity

    1.红点切换间距 RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv_red_point.getLayoutPa ...

  9. SharpGL学习笔记(十二) 光源例子:解决光源场景中的常见问题

    笔者学到光源这一节,遇到的问题就比较多了,收集了一些如下所述: (1) 导入的3ds模型,如果没有材质光照效果很奇怪.如下图 (2) 导入的3ds模型,有材质,灯光效果发暗,材质偏色,效果也很奇怪. ...

  10. 【GOF23设计模式】命令模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_命令模式.数据库事务机制底层架构实现.撤销和回复 package com.test.command; public cla ...