Sudoku

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 12
Special Judge
Problem 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
 
Source
PKU
 
 
题意:

九宫格问题,也有人叫数独问题

把一个9行9列的网格,再细分为9个3*3的子网格

如图

要求每行、每列、每个子网格内都只能使用一次1~9中的一个数字,即每行、每列、每个子网格内都不允许出现相同的数字。

0是待填位置,其他均为已填入的数字。

要求填完九宫格并输出(如果有多种结果,则只需输出其中一种)

如果给定的九宫格无法按要求填出来,则输出原来所输入的未填的九宫格

思路:

判断某一个不为零的格子,看看它的本行本列以及相联系的一个宫已有那些数字,然后再把

有数字依次往里面填,填好这个方格后继续填写下一个方格,如果走到后面走不通了,就应该返回来,把

刚刚它的那个数字还原为零,这样好下一次再继续填写另外的数,如果所有的方格都填完了,整个回溯也就结束了。

AC代码:

 #include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath> using namespace std; int s[][]={};//存储矩阵
bool row[][];//row[i][j]表示第i行有没有j这给数字
bool col[][];//col[i][j]表示第i列有没有j这给数字
bool grid[][];//grid[k][j]表示第k个九宫格有没有j这给数字 bool DFS(int x,int y)
{
if(x==)
return true;
bool sgin=false;
if(s[x][y]!=){
if(y==)
sgin=DFS(x+,);
else
sgin=DFS(x,y+);
if(sgin)
return true;
else
return false;
}
else{
int k=*((x-)/)+(y-)/+;
for(int i=;i<=;i++)
if(!row[x][i]&&!col[y][i]&&!grid[k][i]){//确定放入第x行,第y列,第k个九宫格都可以数
s[x][y]=i;
row[x][i]=true;
col[y][i]=true;
grid[k][i]=true;
if(y==)
sgin=DFS(x+,);
else
sgin=DFS(x,y+);
if(sgin)
return true;
else {//这个放入不可以,则返回上一层;
row[x][i]=false;
col[y][i]=false;
grid[k][i]=false;
s[x][y]=;
}
}
}
return false;
} int main()
{
// freopen("1.txt","r",stdin);
int test;
cin>>test;
char a;
while(test){
memset(s,,sizeof(s));
memset(row,false,sizeof(row));
memset(col,false,sizeof(col));
memset(grid,false,sizeof(grid));
for(int i=;i<=;i++){
for(int j=;j<=;j++){
cin>>a;
s[i][j]=a-'';
if(s[i][j]){//本来就不是0,进行排除
int k=((i-)/)*+(j-)/+;
row[i][s[i][j]]=true;
col[j][s[i][j]]=true;
grid[k][s[i][j]]=true;
}
}
}
DFS(,);
for(int i=;i<=;i++){
for(int j=;j<=;j++){
cout<<s[i][j];
}
cout<<endl;
}
test--;
}
return ;
}

POJ 2676 Sudoku(深搜)的更多相关文章

  1. 深搜+回溯 POJ 2676 Sudoku

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

  2. ACM : POJ 2676 SudoKu DFS - 数独

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

  3. 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 ...

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

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

  5. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

  6. POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]

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

  7. poj 2676 Sudoku ( dfs )

    dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...

  8. POJ 2676 Sudoku

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12005   Accepted: 5984   Special ...

  9. POJ 2676 Sudoku (DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11694   Accepted: 5812   Special ...

随机推荐

  1. C#异常性能影响

    何谓异常 很多人在讨论异常的时候很模糊,仿佛所谓异常就是try{}catch{},异常就是Exception,非常的片面,所以导致异常影响性能,XXXX……等很多奇怪的言论,所以在此我意在对异常正名. ...

  2. Visual Studio 2010 使用 ankhsvn

    之前用的 Windows XP + Visual Studio 2010 + ankhSvn,其中ankhSvn安装完后直接可用, 后来系统换成Windows10后安装ankhSvn,Extentio ...

  3. sharepoint 2013基于AD的Form表单登录(三)——选择用户时,屏蔽掉AD。

    //来源:http://www.cnblogs.com/lrforever/p/3695820.html 隐藏AD人员选择,$ad.IsVisible设置为true,则显示出AD里用户 $cpm = ...

  4. wpf为ListBox添加渐变

    <Style.Triggers> <Trigger Property="ListBox.AlternationIndex" Value="1" ...

  5. Excel 复制Sql查询结果错位

    SELECT TOP 30000 REPLACE(REPLACE(T1.ReceiverName,CHAR(10),' '),CHAR(13),' ') AS ReceiverName, REPLAC ...

  6. maven package:Max maven Unsupported major.minor version 51.0

    编译maven项目时报错:Max maven Unsupported major.minor version 51.0 major.minor version 51.0 对应的是JDK1.7 majo ...

  7. List集合对象中的排序,随机显示

    List<User> students = new ArrayList<User>(); User user1 = new User(); user1.setAge(112); ...

  8. 怎样取json对应的值

    { "轮胎1":[{"数量": "1","型号": "195 65R15","售价&quo ...

  9. PHP编程----猴子选大王

    <?php/** * 猴子选大王 * 17个猴子围成一圈,从某个开始报数1-2-3-1-2-3---报"3"的猴子就被淘汰, * 游戏一直进行到圈内只剩一只猴子它就是猴大王了 ...

  10. 中小型公司数据仓库搭建——以mysql为例

    为了方便公司的数据分析平台的独立运行和数据挖掘的探索,今年上半年在公司搭建了支持数据平台和数据挖掘的数据仓库:现就数据仓库的创建工作总结如下,供大家参考: 首先介绍下数据仓库搭建的缘由: 公司创建两年 ...