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. iOS UIScrollView 你可能不知道的奇技淫巧

    iOS 的 UIScrollView 可以说是十分强大,巧妙地运用它可以得到一些意想不到的效果.本文将举几个 ScrollView 不常见运用的例子. 自带信息应用 这个界面既可以上下卷动,也可以左右 ...

  2. Eclipse / Android : “Errors running builder 'Android Pre Compiler' on project…”

    Errors occurred during the build. Errors running builder 'Android Resource Manager' on project 'hell ...

  3. 实用脚本 - - addLoadEvent 页面加载完毕执行函数

    function addLoadEvent(func){ var oldonload = window.onload; if(typeof window.onload != "functio ...

  4. Asp.net主题(theme)和皮肤(skin)的使用

    asp.net 的服务器端控件提供了多种样式的设计,如果对每个控件都单独设置,是比较繁琐的事情,所以微软也提供了针对这些服务器端控件的样式管理,其实也可以通过 css来控制部分服务器端控件的样式,比如 ...

  5. Html5 部分特性

    HTML5 是 W3C 与 WHATWG 合作的结果. 编者注:W3C 指 World Wide Web Consortium,万维网联盟. 编者注:WHATWG 指 Web Hypertext Ap ...

  6. css-3列布局

    三列布局的步骤是,先定义左右两侧,然后定义中间,并设置'中间'部分的'margin'属性.并且'中间'部分不用设置'width'.例如: <!DOCTYPE html PUBLIC " ...

  7. Get file name without extension.

    Ref:How to get file name without the extension? Normally,there are two ways to implements this:use t ...

  8. 使用cocoapods管理第三方类库

    前言 在iOS项目中使用第三方类库可以说是非常常见的事,但是要正确地配置他们有时候是非常繁琐的事情,幸运的是CocoaPods是一个很好的解决方案. 什么是CocoaPods CocoaPods是OS ...

  9. [转]单例模式与静态变量在PHP中

    在PHP中,没有普遍意义上的静态变量.与Java.C++不同,PHP中的静态变量的存活周期仅仅是每次PHP的会话周期,所以注定了不会有Java或者C++那种静态变量. 所以,在PHP中,静态变量的存在 ...

  10. 关于cocoapods和swift中使用oc第三方

    mac 系统自带ruby,使用cocoapods,直接安装cocoapods就行 终端:$ sudo gem install cocoapods {安装较慢是因为有墙,查看ruby镜像列表:$ gem ...