Sudoku
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 12374 | Accepted: 6172 | 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.
![](https://i-blog.csdnimg.cn/blog_migrate/56e7e6182093bbca488d6c73ba833af6.jpeg)
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
Southeastern Europe 2005
大致题意:给定不完整的数独,填充数独。
题目分析:填充数独首先要找到需要填充的位置,然后去判断(1-9)是否能放下,然后继续找到下一个可放位置,继续测试,如果(1-9)都无法实现,那么返回上一个位置,
此时下一个可填充数,知道没有可填充位置。
解题思路:由上面分析可知可以采用深度优先搜索,按照行查找,确定每一行的可填充位置的数,关键在于如何判重,选用三个二维数组[9][9],一个判断当前行这个数字是否
出现,一个判断当前列这个数字是否出现,另一个判断九宫格中这个数字是否出现,行和列比较容易确定,关键是第几个九宫格如何确定,公式
大致题意:给定不完整的数独,填充数独。
题目分析:填充数独首先要找到需要填充的位置,然后去判断(1-9)是否能放下,然后继续找到下一个可放位置,继续测试,如果(1-9)都无法实现,那么返回上一个位置,
此时下一个可填充数,知道没有可填充位置。
解题思路:由上面分析可知可以采用深度优先搜索,按照行查找,确定每一行的可填充位置的数,关键在于如何判重,选用三个二维数组[9][9],一个判断当前行这个数字是否
出现,一个判断当前列这个数字是否出现,另一个判断九宫格中这个数字是否出现,行和列比较容易确定,关键是第几个九宫格如何确定,公式
k = ((rows-1)/3)*3+(cols-1)/3+1, 具体实现参考代码:
#include <stdio.h>
#include <memory.h>
#include <iostream>
using namespace std;
char col[10][10];
char row[10][10];
char grid[10][10];
char map[10][10];
bool dfs(int rows, int cols)
{
if (rows == 10)
{
return true;
}
bool flag = false;
if (map[rows][cols])
{
if (cols == 9)
{
return dfs(rows+1, 1);
}
else
return dfs(rows, cols+1);
}
else
{
int k = ((rows-1)/3)*3+(cols-1)/3+1;
for (int i=1; i<=9; ++i)
{
if (grid[k][i] == 1 || row[rows][i] == 1 || col[cols][i] == 1)
continue;
grid[k][i] = 1;
col[cols][i] = 1;
row[rows][i] = 1;
map[rows][cols] = i;
if (cols == 9)
flag = dfs(rows+1, 1);
else
flag = dfs(rows, cols+1);
if (!flag)
{
map[rows][cols] = 0;
grid[k][i] = 0;
col[cols][i] = 0;
row[rows][i] = 0;
}
else
return true;
}
}
return false;
}
int main()
{
int n;
char ch;
cin>>n;
while (n-- > 0)
{
memset(col, 0, sizeof(col));
memset(row, 0, sizeof(row));
memset(grid, 0, sizeof(grid));
for (int i=1; i<=9; ++i)
{
for (int j=1; j<=9; ++j)
{
cin>>ch;
ch -= 48;
map[i][j] = ch;
if (ch > 0)
{
col[j][ch] = 1;
row[i][ch] = 1;
grid[((i-1)/3)*3+(j-1)/3+1][ch] = 1; //标记grid
}
}
}
dfs(1, 1);
for (int i=1; i<=9; ++i)
{
for (int j=1; j<=9; ++j)
{
putchar(map[i][j]+48);
}
putchar('\n');
}
}
return 0;
}