首页 > 代码库 > poj 2676

poj 2676

Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14105 Accepted: 6964 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

1103000509002109400000704000300502006060000050700803004000401000009205800804000107

Sample Output

143628579572139468986754231391542786468917352725863914237481695619275843854396127

Source

Southeastern Europe 2005
#include<iostream>#include<algorithm>#include<cstdlib>#include<cstring>#include<string>#include<cmath>#include<cstdio> using namespace std;const int n = 9;int board[9][9];char ch[10];bool ok(int x, int y){               for (int i = x / 3 * 3; i < x / 3 * 3 + 3; ++i)        {                for (int j = y / 3 * 3; j < y / 3 * 3 + 3; ++j)                {                        if (x == i && y == j)                                continue;                        if (board[x][y] == board[i][j])        // the number has been used                                return false;                }        }        int temp = board[x][y];           for (int j = 0; j < n; ++j)        {                if (j == y)                        continue;                if (board[x][j] == temp)                        return false;        }                for (int i = 0; i < n; ++i)        {                if (i == x)                        continue;                if (board[i][y] == temp)                        return false;        }        return true;        }int dfs(int location){        if (location == -1)                return 1;                if (board[location / n][location % n] != 0)                        return dfs(location - 1);        else        {                for (int i = 1; i <= n; ++i)                        {                        board[location / n][location % n] = i;                        if (ok(location / n, location % n))                        {                                if (dfs(location - 1))                                        return 1;                        }                        board[location / n][location % n] = 0;                }        }        return 0;}int main(){        int nCases;        scanf("%d", &nCases);        while (nCases--)        {                for (int i = 0; i < n; ++i)                {                        scanf("%s", ch);                        for (int j = 0; j < n; ++j)                        {                                board[i][j] = ch[j] - ‘0‘;                        }                }                dfs(80);                                for (int i = 0; i < n; ++i)                {                        for (int j = 0; j < n; ++j)                        {                                printf("%d", board[i][j]);                        }                        printf("\n");                }        }                return 0;}

  

poj 2676