Advertisement
penguin88428

sudoku

Nov 30th, 2022 (edited)
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. void readFile(char filename[],int Q[][9])
  6. {
  7.     int r,c;
  8.     ifstream f1;
  9.     f1.open(filename);
  10.     for(r=0;r<9;r++)
  11.     {
  12.         for(c=0;c<9;c++)
  13.         {
  14.             f1 >> Q[r][c];
  15.         }
  16.     }
  17.     f1.close();
  18. }
  19.  
  20. void print(int Q[][9])
  21. {
  22.     int r,c;
  23.     for(r=0;r<9;r++)
  24.     {
  25.         for(c=0;c<9;c++)
  26.         {
  27.             if(Q[r][c]==0)
  28.             {
  29.                 cout << "  ";
  30.             }
  31.             else
  32.             {
  33.                 cout << Q[r][c] << " ";
  34.             }
  35.         }
  36.         cout << endl;
  37.     }
  38. }
  39.  
  40. void update(int A[][9][10],int r,int c,int a)
  41. {
  42.     int m,n;
  43.     A[r][c][0] = a;
  44.     for(m=1;m<=9;m++)
  45.         A[r][c][m] = 0;
  46.     for(m=0;m<9;m++)
  47.         A[r][m][a] = 0;
  48.     for(m=0;m<9;m++)   
  49.         A[m][c][a] = 0;
  50.     for(m=r/3*3;m<=r/3*3+2;m++)
  51.     {
  52.         for(n=c/3*3;n<=c/3*3+2;n++)
  53.         {
  54.             A[m][n][a] = 0;
  55.         }
  56.     }
  57.     A[r][c][a] = 1;
  58. }
  59.  
  60. int onlyyou(int A[][9][10],int r,int c)
  61. {
  62.     int count = 0, a, i;
  63.     for(i=1;i<=9;i++)
  64.     {
  65.         if(A[r][c][i]>0)
  66.         {
  67.             count++;
  68.             a = i;
  69.         }
  70.     }
  71.     if(count==1)
  72.         return a;
  73.     else
  74.         return 0;
  75. }
  76.  
  77. void init(int Q[][9],int A[][9][10])
  78. {
  79.     int r,c,i;
  80.     for(r=0;r<9;r++)
  81.     {
  82.         for(c=0;c<9;c++)
  83.         {
  84.             A[r][c][0] = 0;
  85.             for(i=1;i<=9;i++)
  86.                 A[r][c][i] = 1;
  87.         }
  88.     }
  89.     for(r=0;r<9;r++)
  90.     {
  91.         for(c=0;c<9;c++)
  92.         {
  93.             if(Q[r][c]>0)
  94.             {
  95.                 update(A,r,c,Q[r][c]);
  96.             }
  97.         }
  98.     }
  99. }
  100.  
  101. int main()
  102. {
  103.     int Q[9][9];
  104.     int A[9][9][10];
  105.     int ANS[9][9];
  106.     readFile("Q3.txt",Q);
  107.     print(Q);
  108.     init(Q,A);
  109.     int r,c,a,flag = 0;
  110.     do{
  111.         flag = 0;
  112.         for(r=0;r<9;r++)
  113.         {
  114.             for(c=0;c<9;c++)
  115.             {
  116.                 if(A[r][c][0]==0)
  117.                 {
  118.                     a = onlyyou(A,r,c);
  119.                     if(a>0)
  120.                     {
  121.                         update(A,r,c,a);
  122.                         flag = 1;
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.     }while(flag==1);
  128.     for(r=0;r<9;r++)
  129.     {
  130.         for(c=0;c<9;c++)
  131.         {
  132.             ANS[r][c] = A[r][c][0];
  133.         }
  134.     }
  135.     print(ANS);
  136.     return 0;
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement