Sudoku
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 15809 | Accepted: 7724 | Special Judge |
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
题意:要求在一个9*9的矩阵中,每一行,每一列都不能出现相同的数字,并且在这个9*9的矩阵中分成的9个3*3的小矩阵中数字也不可以重复。输出要求的矩阵,其中矩阵的一些数字已经给出。
思路:用DFS+回溯去做,注意当满足条件是就不应该在继续回溯了。否则矩阵又恢复成原来的状态了。需要用一个标记变量去判断一下是否在当前状态下满足了题目的要求。
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std;
int map[10][10];
int v[10][10];
int pv[10][10];
int pw[10][10];
int flag;
int judge(int x,int y,int z)
{
if(pv[x][z] == 1 || v[z][y] == 1 || pw[(x/3)*3 + y/3][z] == 1)
{
return 0;
}
return 1;
}
void DFS(int x,int y)
{
if(x>=9 || flag)
{
flag = 1;
return ;
}
if(y+1<9)
{
if(map[x][y] == 0)
{
for(int i=1; i<=9; i++)
{
if(judge(x,y,i))
{
pv[x][i] = 1;
v[i][y] = 1;
pw[(x/3)*3+y/3][i] = 1;
map[x][y] = i;
DFS(x,y+1);
if(!flag)
{
map[x][y] = 0;
pv[x][i] = 0;
v[i][y] = 0;
pw[(x/3)*3+y/3][i] = 0;
}
else
{
return ;
}
}
}
}
else
{
DFS(x,y+1);
}
}
else
{
if(map[x][y] == 0)
{
for(int i=1; i<=9; i++)
{
if(judge(x,y,i))
{
pv[x][i] = 1;
v[i][y] = 1;
pw[(x/3)*3+y/3][i] = 1;
map[x][y] = i;
DFS(x+1,0);
if(!flag)
{
map[x][y] = 0;
pv[x][i] = 0;
v[i][y] = 0;
pw[(x/3)*3+y/3][i] = 0;
}
else
{
return ;
}
}
}
}
else
{
DFS(x+1,0);
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
flag = 0;
memset(v,0,sizeof(v));
memset(pv,0,sizeof(pv));
memset(pw,0,sizeof(pw));
char p[10][10];
for(int i=0; i<9; i++)
{
scanf("%s",p[i]);
for(int j=0; j<9; j++)
{
map[i][j] = p[i][j] - '0';
}
}
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
if(map[i][j]!=0)
{
pv[i][map[i][j]] = 1;
v[map[i][j]][j] = 1;
}
}
}
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
if(map[i][j]!=0)
{
int x = i/3 * 3;
int y = j/3;
x = x + y;
pw[x][map[i][j]] = 1;
}
}
}
DFS(0,0);
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
printf("%d",map[i][j]);
}
printf("\n");
}
}
return 0;
}