Created
April 22, 2019 21:10
-
-
Save ske2004/8aa56e82d9823cd8fd65c7dbb587fa6f to your computer and use it in GitHub Desktop.
Sudoku solver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int field[9][9]; | |
bool checkInRow(int number, int row) | |
{ | |
for (int i = 0; i < 9; i++) | |
{ | |
if (field[row][i] == number) | |
return true; | |
} | |
return false; | |
} | |
bool checkInCol(int number, int col) | |
{ | |
for (int i = 0; i < 9; i++) | |
{ | |
if (field[i][col] == number) | |
return true; | |
} | |
return false; | |
} | |
bool checkInBlock(int number, int blocknx, int blockny) | |
{ | |
for (int i = 0; i < 3; i++) | |
{ | |
for (int j = 0; j < 3; j++) | |
{ | |
if (field[i+blocknx][j+blockny] == number) | |
return true; | |
} | |
} | |
return false; | |
} | |
bool isAvalible(int x, int y, int number) | |
{ | |
return !checkInRow(number, x) && !checkInCol(number, y) && !checkInBlock(number, x - x%3 , y - y%3); | |
} | |
bool isZero(int x, int y) | |
{ | |
if (field[x][y] == 0) | |
return true; | |
} | |
void generateField() | |
{ | |
for (int i = 0; i < 9; i++) | |
{ | |
for (int j = 0; j < 9; j++) | |
{ | |
field[i][j] = 0; | |
} | |
} | |
int x, y, num; | |
for (int i = 0; i < 25; i++) | |
{ | |
x = rand()%9; | |
y = rand()%9; | |
num = (rand()%9)+1; | |
if (isAvalible(x, y, num) && isZero(x, y)) | |
{ | |
field[x][y] = num; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment