POJ 2676 Sudoku(深搜)
Sudoku
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 12
Special Judge

九宫格问题,也有人叫数独问题
把一个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(深搜)的更多相关文章
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- 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 ...
- 搜索 --- 数独求解 POJ 2676 Sudoku
Sudoku Problem's Link: http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...
- POJ 2676 Sudoku (数独 DFS)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14368 Accepted: 7102 Special Judg ...
- POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]
题目链接:http://poj.org/problem?id=2676 Time Limit: 2000MS Memory Limit: 65536K Description Sudoku is a ...
- poj 2676 Sudoku ( dfs )
dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...
- POJ 2676 Sudoku
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12005 Accepted: 5984 Special ...
- POJ 2676 Sudoku (DFS)
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11694 Accepted: 5812 Special ...
随机推荐
- PHP的ip2long和long2ip函数的实现原理
最近要做个十进制数字的可逆转换做邀请码,一直没搞清楚怎么弄的,实在太复杂了,今天弄IP时想到这个可以进行转换,于是研究了下原理: 主要是自己整理了下: $ip = '12.34.56.78'; $ip ...
- CodeForces 711C Coloring Trees
简单$dp$. $dp[i][j][k]$表示:前$i$个位置染完色,第$i$个位置染的是$j$这种颜色,前$i$个位置分成了$k$组的最小花费.总复杂度$O({n^4})$. #pragma com ...
- November 12th 2016 Week 46th Saturday
Never love anyone who treats you like you are ordinary. 请爱那些爱你的人. Don't waste your limited energy on ...
- Chapter 2 Open Book——36
"That was awful," he groaned. "They all looked exactly the same. You're lucky you had ...
- Chapter 2 Open Book——33
My chin raised a fraction. 我的下巴抬起来了一点. 我略微抬起下颚. "No, she did not send me here. I sent myself.&q ...
- C#与C++中struct和class的小结
在C#中,struct其实也是可以像class一样封装方法和数据的.请参考如下代码. using System; namespace testDiffInStructClass { public st ...
- some idea for my personal page
firstly, dump the old personal page source from Github to Dropbox.then the idea is: 1: make a fake s ...
- 从补丁到POC CVE-2015-0003(2015.3)
从补丁到POC CVE-2015-0003 1. 简介 该漏洞是由于Windows的win32k.sys模块存在对用户层参数验证不完全,导致存在空指针解引用(Null Pointer Derefere ...
- qemu毒液漏洞分析(2015.9)
0x00背景 安全娱乐圈媒体Freebuf对该漏洞的有关报道: 提供的POC没有触发崩溃,在MJ0011的博客给出了修改后可以使qemu崩溃的poc.详见: http://blogs.360.cn/b ...
- python修炼4
---恢复内容开始--- 集合 建立 set() ={},集合没有顺序,由不可改变的数字 ,字符串,元组构成 #交集print(a&b) #a.intersection(b) #并集prin ...