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. cocoaPods下载使用记录

    cocoaPods下载使用记录 参考地址: 如何在Mac OS 上安装运行Ruby运行环境 http://www.cnblogs.com/daguo/p/4097263.html cocoaPods安 ...

  2. Gitolite轻松部署/管理git server

    对于今天越来越受欢迎的Git,相信做开发的朋友都基本有所耳闻.它最大的便利就是分布式的开发库,让使用git作为源码管理库的开发者可以在本地提交代码的修改而不用提交到远程的库,同时需要和团队协作.同步代 ...

  3. FreeCodeCamp 的 Basic Algorithm Scripting 题解(1)

    这是本人的原创文章,转载请注明原文链接http://www.cnblogs.com/wusuowiaaa1blog/p/5932121.html. 1.Reverse a String 翻转字符串 先 ...

  4. Jquery方法load之后导致js失效解决方法

    Jquery方法load之后导致js失效解决方法 >>>>>>>>>>>>>>>>>>> ...

  5. require.js优化器

    项目发布前,require.js优化器可以合并require.js各个模块. 官网: http://requirejs.org/docs/optimization.html 安装 npm instal ...

  6. 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。

    错误: 解决方案: 1."设置应用程序池默认属性"/"常规"/"启用32位应用程序",设置为 true. 如下图所示:(已测试,好使) 方法 ...

  7. CouchBase 遇到问题笔记(一)

    刚开始看CouchBase,按照官网给出的示例,边敲边理解,遇到了一个很奇怪的问题,如下代码: IView<IViewRow> view = client.GetView("be ...

  8. JavaScript学习笔记--ES6学习(五) 数值的扩展

    ES6 对于数值类型 (Number) 进行了一下扩展: 1.对于二进制和八进制提供了新的写法 ES6对于二进制和八进制的数值提供了新的写法,分别用0b (或者0B) 和0o (或者0o) 表示.例如 ...

  9. php定时执行任务的几个方法

    PHP的实现决定了它没有Java和.Net这种AppServer的概念, 而http协议是一个无状态的协议, php只能被用户触发, 被调用, 调用后会自动退出内存, 没有常驻内存, 就没有办法准确的 ...

  10. javascript--自己用的插件

    /** * Created by Administrator on 2015/4/2. * 时间:2012-6-6 作用:一对form标签下有多个(包括一个)表单需要提交时,提交当前作用域中的表单项做 ...