首页 > 代码库 > poj 2676 -- Sudoku

poj 2676 -- Sudoku

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

思路:这个题正着搜超时,倒着搜16ms

 1 /*====================================================================== 2  *           Author :   kevin 3  *         Filename :   suduku.cpp 4  *       Creat time :   2014-08-06 15:51 5  *      Description : 6  ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio>10 #include <cstring>11 #include <queue>12 #include <cmath>13 #define clr(a,b) memset(a,b,sizeof(a))14 #define M 2015 using namespace std;16 int s[M][M],row[M][M],col[M][M];17 /*-------判断是否有3×3格子里是否有b-------*/18 bool judge(int x,int y,int b)19 {20     int i,j;21     for(i = (x-1)/3*3+1; i <= (x-1)/3*3+3; i++){22         for(j = (y-1)/3*3+1; j <= (y-1)/3*3+3; j++){23             if(s[i][j] == b) return false;24         }25     }26     return true;27 }28 /*----------------end----------------*/29 bool DFS(int x,int y) // x代表列,y代表行30 {31     if(x == 9 && y == 0){32         return true;33     }34     if(s[y][x]){35         if(x > 1){36             if(DFS(x-1,y))37                 return true;38         }39         if(x == 1){40             if(DFS(9,y-1))41                 return true;42         }43     }44     else{45         for(int i = 1; i <= 9; i++){      //枚举1到946             if(!row[y][i] && !col[x][i]){ //当前行当前列是否有i47                 if(judge(y,x,i)){         //判断能否放在3×3方格里48                     row[y][i] = 1; col[x][i] = 1;49                     s[y][x] = i;50                     if(x > 1){51                         if(DFS(x-1,y)){52                             return true;53                         }54                     }55                     if(x == 1){56                         if(DFS(9,y-1)){57                             return true;58                         }59                     }60                     row[y][i] = 0; col[x][i] = 0;61                     s[y][x] = 0;62                 }63             }64         }65     }66     return false;67 }68 int main(int argc,char *argv[])69 {70     int n;71     scanf("%d",&n);72     getchar();73     while(n--){74         clr(s,0);75         clr(row,0);76         clr(col,0);77         char c;78         for(int i  = 1; i <= 9; i++){79             for(int j = 1; j <= 9; j++){80                 scanf("%c",&c);81                 s[i][j] = c-0;82                 row[i][s[i][j]] = 1;   //标记行里出现的数83                 col[j][s[i][j]] = 1;   //标记列里出现的数84             }85             getchar();86         }87         DFS(9,9);          //倒着搜88         for(int i = 1; i <= 9; i++){89             for(int j = 1; j <= 9; j++){90                 printf("%d",s[i][j]);91             }92             printf("\n");93         }94     }95     return 0;96 }
View Code