标签:des style blog http color os io strong
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 13723 | Accepted: 6791 | Special Judge |
Description
Input
Output
Sample Input
1 103000509 002109400 000704000 300502006 060000050 700803004 000401000 009205800 804000107
Sample Output
143628579 572139468 986754231 391542786 468917352 725863914 237481695 619275843 854396127
思路:这个题正着搜超时,倒着搜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 20 15 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到9 46 if(!row[y][i] && !col[x][i]){ //当前行当前列是否有i 47 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 }
poj 2676 -- Sudoku,布布扣,bubuko.com
标签:des style blog http color os io strong
原文地址:http://www.cnblogs.com/ubuntu-kevin/p/3895603.html