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. 【Android】策略模式封装百度地图路线规划模块

    百度地图的Demo里有个路线规划的功能,但是,这个功能和Activity耦合性太高,所以需要单独抽离出路径规划功能,进行"解耦". 注:由于项目原因,本文只针对驾车路线规划进行封装 ...

  2. dplyr 数据操作 常用函数(3)

    接下了我们继续了解dplyr中有用的函数 1.if_else() if_else主要用于在数据做判断用 x<-data.frame(id=1:6, name=c("wang" ...

  3. Java NIO Channel之FileChannel [ 转载 ]

    Java NIO Channel之FileChannel [ 转载 ] @author zachary.guo 对于文件 I/O,最强大之处在于异步 I/O(asynchronous I/O),它允许 ...

  4. MySQL千万级数据JDBC插入

    案例语句: String sql = "LOAD DATA LOCAL INFILE '" + dataFilepath + "' into table " + ...

  5. Chorme 快捷键

    掌握谷歌浏览器的快捷键,能提升一定的使用效率. Windows 和 Linux 标签页和窗口快捷键 操作 快捷键 打开新窗口 Ctrl + n 在隐身模式下打开新窗口 Ctrl + Shift + n ...

  6. 我对Map端spill的理解

    一.先看简单理解 对于hadoop的map端配置项"mapreduce.task.io.sort.mb"和"mapreduce.map.sort.spill.percen ...

  7. 微信支付服务商模式(受理机构模式)开发注意事项,jsapi支付

    1.首先下载的demo,一般都是有些bug的,先要改一下. 2.微信貌似没有为服务商模式单独开发demo,下载的也都是普通商户的支付demo,其实这里没有必要单独写,因为他们区别就是几个参数的区别. ...

  8. CentOS 下安装apt-get

    CentOS 下安装apt-get 最近在学习Linux系统时,网上好多帖子都是用apt-get里下载rpm包,于是就在Google上找了个CentOS下安装apt-get的方法 1.下载地址:htt ...

  9. mybatis配置sql超时时间

    mybatis如果不配置,默认超时时间是不做限制的.当系统慢sql很多时,势必会增加数据库压力,系统性能及稳定性降低.所以有必要要设置sql超时设置,下面配置超时时间是5分钟. 第一步:全局配置如下 ...

  10. [转]URL的解析,C语言实现

    http://blog.csdn.net/cuishumao/article/details/10284463 一 说明(1)应用情况:比如基于socket来实现http协议等,这时候就需要解析URL ...