原题链接:http://poj.org/problem?id=2676

Sudoku

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
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

题解

我们开三个数组,记录在所有行,所有列,所有3×33×33\times 3矩阵里数字1→91→91\to 9是否出现过,这样,我们就可以很方便的爆搜了。

记录行和列很好做,但是3×33×33\times 3的矩阵不是很好做,我们可以科学计算,当然还可以打表。。。

赠送表一个:

int pos[10][10]={0,0,0,0,0,0,0,0,0,0,0,1,1,1,2,2,2,3,3,3,0,1,1,1,2,2,2,3,3,3,0,1,1,1,2,2,2,3,3,3,0,4,4,4,5,5,5,6,6,6,0,4,4,4,5,5,5,6,6,6,0,4,4,4,5,5,5,6,6,6,0,7,7,7,8,8,8,9,9,9,0,7,7,7,8,8,8,9,9,9,0,7,7,7,8,8,8,9,9,9,};
代码
#include<cstdio>
#include<cstring>
using namespace std;
bool heng[10][10],zong[10][10],squa[10][10];
int pos[10][10]={0,0,0,0,0,0,0,0,0,0,0,1,1,1,2,2,2,3,3,3,0,1,1,1,2,2,2,3,3,3,0,1,1,1,2,2,2,3,3,3,0,4,4,4,5,5,5,6,6,6,0,4,4,4,5,5,5,6,6,6,0,4,4,4,5,5,5,6,6,6,0,7,7,7,8,8,8,9,9,9,0,7,7,7,8,8,8,9,9,9,0,7,7,7,8,8,8,9,9,9,};
int sq[10][10];
void in()
{memset(heng,0,sizeof(heng));memset(zong,0,sizeof(zong));memset(squa,0,sizeof(squa));char ch[10];int x;for(int i=1;i<=9;++i){scanf("%s",ch);for(int j=0;j<9;++j){x=ch[j]-'0';sq[i][j+1]=x;heng[i][x]=1;zong[j+1][x]=1;squa[pos[i][j+1]][x]=1;}}
}
bool dfs(int x,int y)
{if(x==10)return 1;bool flag;if(sq[x][y]){if(y==9)flag=dfs(x+1,1);else flag=dfs(x,y+1);if(flag)return 1;else return 0;}else{for(int i=1;i<=9;++i){if(!heng[x][i]&&!zong[y][i]&&!squa[pos[x][y]][i]){sq[x][y]=i;heng[x][i]=1;zong[y][i]=1;squa[pos[x][y]][i]=1;if(y==9)flag=dfs(x+1,1);else flag=dfs(x,y+1);if(!flag){sq[x][y]=0;heng[x][i]=0;zong[y][i]=0;squa[pos[x][y]][i]=0;}else return 1;}}}return 0;
}
void ac()
{dfs(1,1);for(int i=1;i<=9;++i){for(int j=1;j<=9;++j)printf("%d",sq[i][j]);putchar(10);}
}
int main()
{int T;scanf("%d",&T);for(int i=1;i<=T;++i)in(),ac();return 0;
}

POJ2676 Sudoku相关推荐

  1. POJ-2676 Sudoku(简单数独-dfs深搜)

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

  2. poj2676 Sudoku 数独

    记录所有空位置,三个数组(row[M][M], col[M][M], mat[3][3][M])hash判断当前空位置是否可以填某个数,然后直接DFS,注意从后往前搜索,时间比正向搜快很多. #inc ...

  3. Poj 2676 Sudoku[dfs]

    题目大意: 九宫格问题,也有人叫数独问题 把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子网格内都只能使用一次1~9中的一个数字,即每行.每列.每个子网格内都不允许出现相同的数 ...

  4. ICPC程序设计题解书籍系列之九:罗勇军《算法竞赛入门到进阶》

    罗书<算法竞赛入门到进阶>题目一览 第1章 算法竞赛概述 HDU1000 HDU1089-HDU1096 A+B for Input-Output Practice (I)-(VIII)( ...

  5. ICPC程序设计题解书籍系列之三:秋田拓哉:《挑战程序设计竞赛》(第2版)

    白书<挑战程序设计竞赛>(第2版)题目一览 白书:秋田拓哉:<挑战程序设计竞赛>(第2版) 第1章 蓄势待发--准备篇(例题) POJ1852 UVa10714 ZOJ2376 ...

  6. 备战NOIP每周写题记录(一)···不间断更新

    ※Recorded By ksq2013 //其实这段时间写的题远远大于这篇博文中的内容,只不过那些数以百记的基础题目实在没必要写在blog上; ※week one 2016.7.18 Monday ...

  7. 整理:poj 基本搜索

    参考:http://exp-blog.com/ https://blog.csdn.net/consciousman/article/details/54613292 POJ2488 – A Knig ...

  8. Dancing Links题集

    POJ3740     Easy Finding [精确覆盖基础题] HUST1017    Exact cover [精确覆盖基础] HDOJ3663 Power Stations [精确覆盖] Z ...

  9. 舞蹈链算法(DLX 算法)略解

    Another Blog DLX=Dancing Line X 之前学 DLX 时在网上看了好几篇博客,结合着几篇讲代码的.有图片的.说原理的,总算弄懂了.这里 DLX 是用来解决精确覆盖问题的算法, ...

  10. 【POJ2676】Sudoku(优化搜索顺序)

    problem 补全9*9的数独 满足每行,每列,每个3*3方格内1~9均只出现一次 solution 1.答案 状态:我们关心数独每个位置填了什么数.我们需要在每个状态中找出没有填的位置,检查有哪些 ...

最新文章

  1. jstl动态取变量_C语言的变量名
  2. Jetson Nano and VIM3硬件参数对比及目标检测性能对比
  3. Stanford UFLDL教程 卷积特征提取
  4. 21丨容器化守护进程的意义:DaemonSet
  5. jquery基本操作笔记
  6. Java命令行界面(第3部分):jbock
  7. Eclipse export导出war包报错(Module name is invalid.)
  8. 为什么创建线程池一定要用ThreadPoolExecutor?
  9. java私塾 java篇_Java私塾跟我学系列——JAVA篇 五、
  10. css设置字体颜色、文本对齐方式、首行缩进、文本装饰、列表样式、鼠标样式、禁止文本域拖拽、轮廓线、块级元素对齐方式、文字溢出设置
  11. Deep Learning of Binary Hash Codes for Fast Image Retrieval(2015)
  12. 用PHP调用WEBSERVICE
  13. random_state的值如何选_算法萌新如何学好动态规划(3)
  14. Html中代码换行造成空格间距的问题
  15. python面向对象三大特性_python面向对象的三大特性
  16. Java递归下降分析器_递归下降语法分析器
  17. 线性代数学习笔记——第二十四讲——向量及其线性运算
  18. EasyGUI-5:文本显示
  19. 寻求任意颜色转CMY的方法,帮个忙
  20. html表格制作旅游网页,简单实用的网页表格特效_html

热门文章

  1. 对Map集合排序,先对value降序,value相同的情况下,key升序
  2. springboot中得注解_SpringBoot 中的基本注解
  3. uwsgi怎么启动停止
  4. 2017-2018 20155309 南皓芯 信息安全基础设计第八周博客
  5. 23种经典设计模式UML类图汇总
  6. 关于php print_r
  7. javascript 中==和===的区别
  8. ecshop中$user对象
  9. java t输出_java --输入输出
  10. mysql同步三张表如何用事务_MySql-第三部分(外键, 多表连接, 事务,视图 )