Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12075   Accepted: 6026   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
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
题意:数独问题,当有多种方案时任意输出一种; 思路:回溯法,用三个数组row[][],col[][],square[][]维护某个数是否能被填充,在未填充的空格里尝试着放一个数,继续递归,当发生冲突时就回溯,更改已做的标记;
 #include<stdio.h>
#include<string.h>
int map[][];
bool row[][];//row[i][x] 表示第i行x是否出现过;
bool col[][];//col[j][x] 表示第j行x是否出现过;
bool square[][];//square[k][x] 表示第k个方格x是否出现过; bool dfs(int x, int y)
{ if(x == )
return true;//递归边界;
bool flag = false; if(map[x][y])//如果map[x][y]已经填了数字,确定向下递归的方向;
{
if(y == )
flag = dfs(x+,);
else
flag = dfs(x,y+); if(flag)
return true;
else return false;
}
else
{
int k = *((x-)/) + (y-)/+;
for(int i = ; i <= ; i++)
{
if(!row[x][i] && !col[y][i] && !square[k][i])
{
map[x][y] = i;//找到合适的i填充; row[x][i] = true;
col[y][i] = true;
square[k][i] = true;
//继续递归
if(y == )
flag = dfs(x+,);
else flag = dfs(x,y+); if(flag)
return true;
else //回溯,修改已作的标记
{
map[x][y] = ; row[x][i] = false;
col[y][i] = false;
square[k][i] = false;
}
}
}
}
return false;
}
int main()
{
int t;
char s[];
scanf("%d",&t);
while(t--)
{
memset(row,false,sizeof(row));
memset(col,false,sizeof(col));
memset(square,false,sizeof(square));
for(int i = ; i <= ; i++)
{
scanf("%s",s);
for(int j = ; j < ; j++)
{
map[i][j+] = s[j]-'';
if(map[i][j+])
{
int k = *((i-)/) + j/+;
row[i][ map[i][j+] ] = true;
col[j+][ map[i][j+] ] = true;
square[k][ map[i][j+] ] = true;
}
}
}
dfs(,);
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
printf("%d",map[i][j]);
printf("\n");
}
}
return ;
}

Sudoku(回溯)的更多相关文章

  1. 深搜+回溯 POJ 2676 Sudoku

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

  2. 求解数独难题, Sudoku问题(回溯)

    Introduction : 标准的数独游戏是在一个 9 X 9 的棋盘上填写 1 – 9 这 9 个数字,规则是这样的: 棋盘分成上图所示的 9 个区域(不同颜色做背景标出,每个区域是 3 X 3 ...

  3. [LeetCode] Sudoku Solver 解数独,递归,回溯

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. 【POJ - 2676】Sudoku(数独 dfs+回溯)

    -->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...

  5. Leetcode之回溯法专题-37. 解数独(Sudoku Solver)

    Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...

  6. HDU 1426 Sudoku Killer (回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398818 题意: 给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开.其中1-9代表该位 ...

  7. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  8. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  9. [leetcode]算法题目 - Sudoku Solver

    最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...

随机推荐

  1. PL/SQL 记录集合IS TABLE OF的使用

    在PL/SQL代码块中使用select into 赋值的话,有可能返回的是一个结果集.此时,如果使用基本类型或自定义的记录类型,将会报错. 因此,需要定义一个变量,是某种类型的集合.下面以一个基于表的 ...

  2. WCF 用netTcpbinding,basicHttpBinding 传输大文件

    问题:WCF如何传输大文件 方案:主要有几种绑定方式netTcpbinding,basicHttpBinding,wsHttpbinding,设置相关的传输max消息选项,服务端和客户端都要设置,tr ...

  3. List指定字段赋特定值(非循环) asp.net

    List<Cart> cartd=cartd.Where(p => (p.Id= "123").Length > -1).ToList(); 把Id的值都赋 ...

  4. RegistryKey 类

    表示 Windows 注册表中的项级节点. 此类是注册表封装. 继承层次结构 System.Object   System.MarshalByRefObject    Microsoft.Win32. ...

  5. 最终版-perl工具解析数据库的报告文件0120

    ********************需要根据自己的实际环境修改哦**************************** ******************** 1. 收集awr报告样本   a ...

  6. angular的post提交

    用下来明显感觉jquery的post提交比ng的post提交好用很多 一开始,用angularjs的$http提交的数据,在php服务器端无法通过 因为jQuery会把作为JSON对象的data序列化 ...

  7. PHP中的&传值引用的问题,在foreach循环的结果能帮解释下输出的结果原理是什么?

    PHP中的&传值引用的问题,在foreach循环的结果能帮解释下输出的结果原理是什么? 代码如下: <?php $arr = array('one','two','three'); fo ...

  8. C# Windows服务安装出现System.Security.SecurityException异常解决办法

    我把注册windows服务所用的安装及启用服务命令写到了bat可执行文件(名称为install.bat)中,如下所示: %SystemRoot%\Microsoft.NET\Framework\v4. ...

  9. HTML5 Canvas画数字时钟

    先不说废话,没代码算个蛋. 一些地方注释都写得比较清楚,不过这只是部分,因为只有秒针,但是时针,分针的逻辑都是一致的. 代码中有些坐标不知道为什么较不准,看看就好

  10. mysql中显示方式的切换

    1. mysql中如果使用\G,则':'不用写.如果\G后面跟':'则会报"error:no query specified"错误.请知晓. 2. mysql在登陆时,mysql ...