Advertisement
SuitNdtie

Sudoku

May 7th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int board[9][9];
  4. int UpDown[9][10];
  5. int LeRigh[9][10];
  6. int block[3][3][10];
  7.  
  8. int ans[9][9];
  9. bool found = false;
  10. void cal(int I,int J){
  11.     if(found)return;
  12.     if(J == 9){
  13.         cal(I+1,0);
  14.         return;
  15.     }
  16.     if(board[I][J] != 0){
  17.         cal(I,J+1);
  18.         return;
  19.     }
  20.     if(I == 9){
  21.         for(int i = 0 ; i < 9 ; i ++){
  22.             for(int j = 0 ; j < 9 ; j ++){
  23.                 ans[i][j] = board[i][j];
  24.             }
  25.         }
  26.         found = true;
  27.         return;
  28.     }
  29.     for(int num = 1 ; num <= 9 ; num ++){
  30.         if(UpDown[I][num])continue;
  31.         if(LeRigh[J][num])continue;
  32.         if(block[I/3][J/3][num])continue;
  33.        
  34.         UpDown[I][num] = true;
  35.         LeRigh[J][num] = true;
  36.         block[I/3][J/3][num] = true;
  37.         board[I][J] = num;
  38.        
  39.         cal(I,J+1);
  40.        
  41.         UpDown[I][num] = false;
  42.         LeRigh[J][num] = false;
  43.         block[I/3][J/3][num] = false;
  44.         board[I][J] = 0;
  45.        
  46.     }
  47. }
  48.  
  49. int main()
  50. {
  51. //  freopen("input_sudoku.txt","r",stdin);
  52.     for(int t = 1 ; t <= 100 ; t ++){
  53.         char str[7];int b;
  54.         scanf(" %s %d",str,&b);
  55.         int cnt = 0 ;
  56.         for(int i = 0 ; i < 9 ; i ++){
  57.             for(int j = 0 ; j < 9 ; j ++){
  58.                 scanf("%d",&board[i][j]);
  59.                 UpDown[i][board[i][j]] = true;
  60.                 LeRigh[j][board[i][j]] = true;
  61.                 block[(i/3)][(j/3)][board[i][j]] = true;
  62.             }
  63.         }
  64.         found = false;
  65.         cal(0,0);
  66.     //  freopen("output_sudoku.txt","w",stdout);
  67.         printf("%s %d\n",str,b);
  68.         for(int i = 0 ; i < 9 ; i ++){
  69.             for(int j = 0 ; j < 9 ; j ++){
  70.                 printf("%d ",ans[i][j]);
  71.             }
  72.             printf("\n");
  73.         }
  74.         for(int i = 0 ; i < 9 ; i ++)
  75.         {
  76.             for(int j = 0 ; j < 10 ; j ++)
  77.             {
  78.                 UpDown[i][j] = false;
  79.                 LeRigh[i][j] = false;
  80.             }
  81.         }
  82.         for(int i = 0 ; i < 3 ; i ++)
  83.         {
  84.             for(int j = 0 ; j < 3 ; j ++)
  85.             {
  86.                 for(int k = 0 ; k < 10 ; k ++)
  87.                 {
  88.                     block[i][j][k] = false;
  89.                 }
  90.             }
  91.         }
  92.     }
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement