POJ2676 Sudoku

POJ2676 Sudoku

  • 需要算出第i行第j个所对应得九宫格的编号
  • 据说可以直接算: 3*((i-1)/3)+(j-1)/3+1,蒟蒻不会,只好预处理
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <queue>
  4 #include <algorithm>
  5 #include <iostream>
  6 #include <cctype>
  7 using namespace std;
  8 
  9 #define res register int
 10 inline int read() {
 11     int x(0),f(1); char ch;
 12     while(!isdigit(ch=getchar())) if(ch=='-') f=-1;
 13     while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
 14     return f*x;
 15 }
 16 int id[10][10];//打表算出i,j所在的九宫格 
 17 inline void pre_work()
 18 {
 19     for(res i=1 ; i<=3 ; i++)
 20     {
 21         for(res j=1 ; j<=3 ; j++) id[i][j]=1;
 22         for(res j=4 ; j<=6 ; j++) id[i][j]=4;
 23         for(res j=7 ; j<=9 ; j++) id[i][j]=7;    
 24     } 
 25     for(res i=4 ; i<=6 ; i++)
 26     {
 27         for(res j=1 ; j<=3 ; j++) id[i][j]=2;
 28         for(res j=4 ; j<=6 ; j++) id[i][j]=5;
 29         for(res j=7 ; j<=9 ; j++) id[i][j]=8;    
 30     } 
 31     for(res i=7 ; i<=9 ; i++)
 32     {
 33         for(res j=1 ; j<=3 ; j++) id[i][j]=3;
 34         for(res j=4 ; j<=6 ; j++) id[i][j]=6;
 35         for(res j=7 ; j<=9 ; j++) id[i][j]=9;    
 36     }         
 37 }
 38 
 39 int map[10][10];
 40 bool row[10][10],col[10][10],blo[10][10];
 41 
 42 bool dfs(int x,int y) 
 43 {
 44     if(x==10) return true;
 45     bool flag=false;
 46     if(map[x][y])
 47     {
 48         if(y==9) flag=dfs(x+1,1);
 49         else flag=dfs(x,y+1);
 50         return flag;
 51     }
 52     else
 53     {
 54         int k=id[x][y];
 55         for(res i=1 ; i<=9 ; i++)
 56         {
 57             if(!row[x][i] && !col[y][i] && !blo[k][i])
 58             {
 59                 map[x][y]=i;
 60                 row[x][i]=col[y][i]=blo[k][i]=true;
 61                 if(y==9) flag=dfs(x+1,1); 
 62                 else     flag=dfs(x,y+1);
 63                 if(flag) return true;
 64                 map[x][y]=0;
 65                 row[x][i]=col[y][i]=blo[k][i]=false;            
 66             }    
 67         }    
 68     }
 69     return false;    
 70 }
 71 
 72 int main()
 73 {
 74     pre_work();
 75     int T=read();
 76     while(T--)
 77     {
 78         memset(row,false,sizeof(row)); 
 79         memset(col,false,sizeof(col));
 80         memset(blo,false,sizeof(blo));
 81         char ch;
 82         for(res i=1 ; i<=9 ; i++)
 83             for(res j=1 ; j<=9 ; j++)
 84             {
 85                 cin>>ch;
 86 //                scanf(" %c",ch);
 87                 map[i][j]=ch-'0';
 88                 if(map[i][j]>0)
 89                 {
 90                     int k=id[i][j];
 91                     row[i][map[i][j]]=true;
 92                     col[j][map[i][j]]=true;
 93                     blo[k][map[i][j]]=true;
 94                 }
 95             }
 96         dfs(1,1);
 97         for(res i=1 ; i<=9 ; i++)
 98         {
 99             for(res j=1 ; j<=9 ; j++)
100                 printf("%d",map[i][j]);
101             puts("");
102         }
103             
104     }
105     
106     
107     return 0;
108 }
View Code
原文地址:https://www.cnblogs.com/wmq12138/p/10368212.html