NYOJ 722 数独(DFS+检查)
2018-03-29 23:26
211 查看
数独
时间限制:1000 ms | 内存限制:65535 KB 难度:4描述 数独是一种运用纸、笔进行演算的逻辑游戏。玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个3*3宫内的数字均含1-9,不重复。 每一道合格的数独谜题都有且仅有唯一答案,推理方法也以此为基础,任何无解或多解的题目都是不合格的。有一天hrdv碰到了一道号称是世界上最难的数独的题目,作为一名合格的程序员,哪能随随便便向困难低头,于是他决定编个程序来解决它。。
输入第一行有一个数n(0< n <100),表示有n组测试数据,每组测试数据是由一个9*9的九宫格构成,0表示对应的格子为空输出输出一个9*9的九宫格,为这个数独的答案样例输入
1 0 0 5 3 0 0 0 0 0 8 0 0 0 0 0 0 2 0 0 7 0 0 1 0 5 0 0 4 0 0 0 0 5 3 0 0 0 1 0 0 7 0 0 0 6 0 0 3 2 0 0 0 8 0 0 6 0 5 0 0 0 0 9 0 0 4 0 0 0 0 3 0 0 0 0 0 0 9 7 0 0样例输出
1 4 5 3 2 7 6 9 8 8 3 9 6 5 4 1 2 7 6 7 2 9 1 8 5 4 3 4 9 6 1 8 5 3 7 2 2 1 8 4 7 3 9 5 6 7 5 3 2 9 6 4 8 1 3 6 7 5 4 2 8 1 9 9 8 4 7 6 1 2 3 5 5 2 1 8 3 9 7 6 4//TEL代码#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
typedef long long ll;
using namespace std;
int mp[10][10];
int n;
int flag=0;
void print()
{
int i,j;
for(i=1; i<=9; i++)
{
for(j=1; j<9; j++)
printf("%d ",mp[i][j]);
printf("%d\n",mp[i][j]);
}
}
bool check(int x,int y,int k)
{
for(int i=1; i<=9; i++)
{
if(mp[x][i]==k)
return false;
}
for(int i=1; i<=9; i++)
{
if(mp[i][y]==k)
{
return false;
}
}
int i,j;
if(x>=1 && x<=3)
{
if(y>=1 && y<=3)
i=1,j=1;
else if(y>=4 && y<=6)
i=1,j=4;
else if(y>=7 && y<=9)
i=1,j=7;
}
else if(x>=4 && x<=6)
{
if(y>=1 && y<=3)
i=4,j=1;
else if(y>=4 && y<=6)
i=4,j=4;
else if(y>=7 && y<=9)
i=4,j=7;
}
else
{
if(y>=1 && y<=3)
i=7,j=1;
else if(y>=4 && y<=6)
i=7,j=4;
else if(y>=7 && y<=9)
i=7,j=7;
}
for(int p=i; p <3+i; p++)
{
for(int q=j; q <3+j; q++)
{
if(mp[p][q]==k)
return false;
}
}
/*//优化
int i, j;
i = (x-1)/ 3 * 3+1;
j = (y-1)/ 3 * 3+1;
for (int p = i; p <3 + i; p++)
{
for (int q = j; q < 3 + j; q++)
{
if (mp[p][q] == k)
return false;
}
}*/
return true;
}
void dfs(int x,int y)
{
if(flag)
return ;
if(x==10 && y==1)
{
print();
flag=1;
return ;
}
else if(mp[x][y]!=0)
{
if(y+1<=9)
dfs(x,y+1);
else
dfs(x+1,y);
}
else
{
for(int i=1; i<=9; i++)
{
if(check(x,y,i))
{
mp[x][y]=i;
if(y+1<=9)
dfs(x,y+1);
else
dfs(x+1,1);
mp[x][y]=0;
}
}
}
}
int main()
{
cin>>n;
while(n--)
{
for(int i=1; i<=9; i++)
{
for(int j=1; j<=9; j++)
{
scanf("%d",&mp[i][j]);
}
}
dfs(1,1);
}
return 0;
}//变相检查#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
typedef long long ll;
using namespace std;
int mp[10][10];
int row[10][10];
int col[10][10];
int block[4][4][10];
int n;
int flag;
void print()
{
int i,j;
for(i=1; i<=9; i++)
{
for(j=1; j<9; j++)
printf("%d ",mp[i][j]);
printf("%d\n",mp[i][j]);
}
}
void dfs(int x,int y)
{
if(flag)
return ;
if(x==10 && y==1)
{
print();
flag=1;
return ;
}
else if(mp[x][y]!=0)
{
if(y+1<=9)
dfs(x,y+1);
else
dfs(x+1,1);
}
else
{
for(int i=1; i<=9; i++)
{
if(!(row[x][i] || col[i][y] || block[(x-1)/3+1][(y-1)/3+1][i]))
{
mp[x][y]=i;
row[x][i]=col[i][y]=block[(x-1)/3+1][(y-1)/3+1][i]=1;
if(y+1<=9)
dfs(x,y+1);
else
dfs(x+1,1);
mp[x][y]=0;
row[x][i]=col[i][y]=block[(x-1)/3+1][(y-1)/3+1][i]=0;
}
}
}
}
int main()
{
cin>>n;
while(n--)
{
flag=0;
memset(row,0,sizeof(row));
memset(col,0,sizeof(col));
memset(block,0,sizeof(block));
for(int i=1; i<=9; i++)
for(int j=1; j<=9; j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]!=0)
row[i][mp[i][j]]=col[mp[i][j]][j]=block[(i-1)/3+1][(j-1)/3+1][mp[i][j]]=1;
}
dfs(1,1);
}
return 0;
}
相关文章推荐
- NYOJ 722--数独【DFS】
- NYOJ 722 数独 【DFS】+【预处理】
- NYOJ 722数独(DFS)
- nyoj 722 数独 【经典DFS】
- NYOJ 722 数独(dfs)
- NYOJ 722 数独(DFS)
- NYOJ 722 数独 【DFS】+【预处理】
- NYOJ 722 数独
- NYOJ 722 数独
- NYOJ 数独 DFS
- NYOJ 数独(DFS+优化)
- NYOJ 722 数独
- DLX 求解数独 模 板 NYOJ 722 , poj 2676
- 回溯法 —— NYOJ 722 数独
- 数独 nyoj722
- 数独(nyoj 722)
- nyoj722 数独
- NYOJ 722 数独
- NYOJ 722 数独
- HDU-1426-Sudoku Killer&&NYOJ-722-数独