SHARE
TWEET

Untitled

a guest Jul 19th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.   vector<vector<char>>::const_iterator it1;
  71.   vector<char>::const_iterator it2;
  72.   for(it1 = board.begin(); it1 != board.end(); ++it1)
  73.     {
  74.         i++;
  75.         j = -1;
  76.         for(it2 = it1->begin(); it2 != it1->end(); ++it2)
  77.             a[i][++j] = *it2;
  78.     }
  79.  
  80.   for(i = 1; i <= 9; ++i)
  81.     {
  82.         for(j = 0; j <= 8; ++j)
  83.             cout << a[i][j]<<'\n';
  84.     }
  85.      for(i = 1; i <= 9; ++i)
  86.     if(!lineCheck(a,i))
  87.       {
  88.         return 0;
  89.       }
  90.   for(i = 0; i < 9; ++i)
  91.     if(!columnCheck(a,i))
  92.         return 0;
  93.   for(i = 1; i <= 9; i += 3)
  94.   {
  95.     for(j = 0; j <= 8; j += 3)
  96.       if(!squareCheck(a,i,j) && a[i][j] != '.')
  97.         {
  98.           return 0;
  99.         }
  100.   }
  101.   return 1;
  102. }
  103.  
  104. int main() {
  105.   vector<vector<char> >board;
  106.   int i;
  107.   for (int i = 1; i <= 9; ++i)
  108.     {
  109.         board[i].resize(9);
  110.         for (int j = 0; j < 9; ++j)
  111.         {
  112.             fin >> board[i][j];
  113.         }
  114.     }
  115.   cout << sudokuSolve(board);
  116.   return 0;
  117. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top