Advertisement
Alhiris

Untitled

Jul 19th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. #include <fstream>
  5. using namespace std;
  6. ifstream fin("joc13.in");
  7.  
  8. bool squareCheck(char a[][10], int x, int y)
  9. {
  10.   int i, new_line, new_col;
  11.   bool f[10];
  12.   memset(f,0,10);
  13.   int dlin[] = {0, 0, 1, 2, 1, 1, 2, 2};
  14.   int dcol[] = {1, 2, 1, 2, 2, 0, 0, 1};
  15.   for(i = 0; i < 8; ++i)
  16.       {
  17.         new_line = x + dlin[i];
  18.         new_col = y + dcol[i];
  19.         if(a[new_line][new_col] != '.' && f[a[new_line][new_col] - '0'])
  20.           {
  21.         //    cout << new_line << " "<<new_col<<'\n';
  22.             return 0;
  23.           }
  24.         else
  25.           f[a[new_line][new_col] - '0'] = 1;
  26.       }
  27.   return 1;
  28. }
  29.  
  30. bool lineCheck(char a[][10], int x)
  31. {
  32.   int i;
  33.   bool f[10];
  34.   memset(f,0,10);
  35.   for(i = 0; i < 9; ++i)
  36.    if(a[x][i] != '.')
  37.    {
  38.     if(f[a[x][i] - '0'])
  39.       {
  40.         return 0;
  41.       }
  42.     else
  43.       f[a[x][i] - '0'] = 1;
  44.     }
  45.   return 1;
  46. }
  47.  
  48. bool columnCheck(char a[][10], int x)
  49. {
  50.   int i;
  51.   bool f[10];
  52.   memset(f,0,10);
  53.   for(i = 1; i <= 9; ++i)
  54.    if(a[i][x] != '.')
  55.    {
  56.     if(f[a[i][x] - '0'])
  57.       { //cout<<a[i][x]<<" "<<i<<" "<<x<<'\n';
  58.         return 0;
  59.       }
  60.     else
  61.       f[a[i][x] - '0'] = 1;
  62.     }
  63.   return 1;
  64. }
  65.  
  66. bool sudokuSolve(const vector<vector<char> >& board )
  67. {
  68.   int i = 0,j;
  69.   char a[10][10];
  70.   for(auto it1 = board.begin(); it1 != board.end(); ++it1)
  71.     {
  72.         i++;
  73.         j = -1;
  74.         for(auto it2 = it1->begin(); it2 != it1->end(); ++it2)
  75.             a[i][++j] = *it2;
  76.     }
  77.  
  78.   for(i = 1; i <= 9; ++i)
  79.     {
  80.         for(j = 0; j <= 8; ++j)
  81.             cout << a[i][j]<<'\n';
  82.     }
  83.      for(i = 1; i <= 9; ++i)
  84.     if(!lineCheck(a,i))
  85.       {
  86.         return 0;
  87.       }
  88.   for(i = 0; i < 9; ++i)
  89.     if(!columnCheck(a,i))
  90.         return 0;
  91.   for(i = 1; i <= 9; i += 3)
  92.   {
  93.     for(j = 0; j <= 8; j += 3)
  94.       if(!squareCheck(a,i,j) && a[i][j] != '.')
  95.         {
  96.           return 0;
  97.         }
  98.   }
  99.   return 1;
  100. }
  101.  
  102. int main() {
  103.   vector<vector<char> >board(9);
  104.   int i;
  105.   for (int i = 0; i < 9; ++i)
  106.     {
  107.         board[i].resize(9);
  108.         for (int j = 0; j < 9; ++j)
  109.         {
  110.             fin >> board[i][j];
  111.         }
  112.     }
  113.   cout << sudokuSolve(board);
  114.   return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement