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. CodeForces 681D Gifts by the List

    $dfs$,后续遍历. 如果某个节点$a[i]=i$,那么$i$的后继的$a[i]$都要指向$i$,直到出现新的后继$j$,$a[j]=j$.利用这个可以判断是否有解. 如果有解的话,那么只要输出后序 ...

  2. OSI七层模型详解

    OSI 七层模型通过七个层次化的结构模型使不同的系统不同的网络之间实现可靠的通讯,因此其最主要的功能就是帮助不同类型的主机实现数据传输 . 完成中继功能的节点通常称为中继系统.在OSI七层模型中,处于 ...

  3. SpringMVC一路总结(一)(转)

    itRed You are never too old to set another goal or to dream a new dream. SpringMVC一路总结(一) SpringMVC听 ...

  4. PHP22期基础班总结

    11月7号,我们结束了为期17天的PHP基础班课程,最后一天的晚自习之后,马总问了我们的一个问题,基础班1000块钱的课程,我们认为是否值得这个价格?这其实是一个很好的问题. 2016年1000块钱能 ...

  5. Nopi Excel导入

    http://download.csdn.net/detail/diaodiaop/7611721 using System.Collections.Generic; using System.Dat ...

  6. maven 3.3.9-bin 和 maven 3.3.9-src 的区别 以及 maven安装包的 .tar.gz后缀与.zip 后缀的区别

    (maven 3.3.9-bin)一个是class的文件包,由java文件编译成的,(maven 3.3.9-src )一个是java文件包即是源码(.tar.gz后缀)是linux的压缩包,(.zi ...

  7. 微信超时5s,调用客服接口异步回复消息(PHP)

    当用户触发事件,如果不能保证在5s内响应,可以先返回success,然后异步调用返回的信息.代码如下: // 立即返回(异步执行) ignore_user_abort(true);//start=== ...

  8. 深度分析如何在Hadoop中控制Map的数量

    深度分析如何在Hadoop中控制Map的数量 guibin.beijing@gmail.com 很多文档中描述,Mapper的数量在默认情况下不可直接控制干预,因为Mapper的数量由输入的大小和个数 ...

  9. 从头开始学Java【1】

    1:常见的DOS命令 盘符的切换 d:回车 目录的进入 cd javase cd javase\day01\code 目录的回退 cd.. cd\ 清屏 cls 退出 exit 创建目录 md 删除目 ...

  10. Chapter 2 Open Book——28

    I kept my voice indifferent. "May I?" 我尽量让我的声音显得不那么突兀,我能试试吗? 我尽量让自己的声音显得漠不关心.“可以让我看一下吗?” H ...