Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstring>
- #include <fstream>
- using namespace std;
- ifstream fin("joc13.in");
- bool squareCheck(char a[][10], int x, int y)
- {
- int i, new_line, new_col;
- bool f[10];
- memset(f,0,10);
- int dlin[] = {0, 0, 1, 2, 1, 1, 2, 2};
- int dcol[] = {1, 2, 1, 2, 2, 0, 0, 1};
- for(i = 0; i < 8; ++i)
- {
- new_line = x + dlin[i];
- new_col = y + dcol[i];
- if(a[new_line][new_col] != '.' && f[a[new_line][new_col] - '0'])
- {
- // cout << new_line << " "<<new_col<<'\n';
- return 0;
- }
- else
- f[a[new_line][new_col] - '0'] = 1;
- }
- return 1;
- }
- bool lineCheck(char a[][10], int x)
- {
- int i;
- bool f[10];
- memset(f,0,10);
- for(i = 0; i < 9; ++i)
- if(a[x][i] != '.')
- {
- if(f[a[x][i] - '0'])
- {
- return 0;
- }
- else
- f[a[x][i] - '0'] = 1;
- }
- return 1;
- }
- bool columnCheck(char a[][10], int x)
- {
- int i;
- bool f[10];
- memset(f,0,10);
- for(i = 1; i <= 9; ++i)
- if(a[i][x] != '.')
- {
- if(f[a[i][x] - '0'])
- { //cout<<a[i][x]<<" "<<i<<" "<<x<<'\n';
- return 0;
- }
- else
- f[a[i][x] - '0'] = 1;
- }
- return 1;
- }
- bool sudokuSolve(const vector<vector<char> >& board )
- {
- int i = 0,j;
- char a[10][10];
- vector<vector<char>>::const_iterator it1;
- vector<char>::const_iterator it2;
- for(it1 = board.begin(); it1 != board.end(); ++it1)
- {
- i++;
- j = -1;
- for(it2 = it1->begin(); it2 != it1->end(); ++it2)
- a[i][++j] = *it2;
- }
- for(i = 1; i <= 9; ++i)
- {
- for(j = 0; j <= 8; ++j)
- cout << a[i][j]<<'\n';
- }
- for(i = 1; i <= 9; ++i)
- if(!lineCheck(a,i))
- {
- return 0;
- }
- for(i = 0; i < 9; ++i)
- if(!columnCheck(a,i))
- return 0;
- for(i = 1; i <= 9; i += 3)
- {
- for(j = 0; j <= 8; j += 3)
- if(!squareCheck(a,i,j) && a[i][j] != '.')
- {
- return 0;
- }
- }
- return 1;
- }
- int main() {
- vector<vector<char> >board;
- int i;
- for (int i = 1; i <= 9; ++i)
- {
- board[i].resize(9);
- for (int j = 0; j < 9; ++j)
- {
- fin >> board[i][j];
- }
- }
- cout << sudokuSolve(board);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement