-
Notifications
You must be signed in to change notification settings - Fork 180
/
Sudoku.cpp
99 lines (98 loc) · 2.99 KB
/
Sudoku.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
#define empty 0
#define N 9
bool isGridSafe(int grid[N][N], int row, int col, int num);
bool isEmptyLocation(int grid[N][N], int &row, int &col);
/* assign values to all the zero (not assigned) values for Sudoku solution
*/
bool SolveSudoku(int grid[N][N])
{
int row, col;
if (!isEmptyLocation(grid, row, col))
return true;
for (int num = 1; num <= 9; num++)
{
if (isGridSafe(grid, row, col, num))
{
grid[row][col] = num;
if (SolveSudoku(grid))
return true;
grid[row][col] = empty;
}
}
return false;
}
/* Check for entries that don't have a value. */
bool isEmptyLocation(int grid[N][N], int &row, int &col)
{
for (row = 0; row < N; row++)
for (col = 0; col < N; col++)
if (grid[row][col] == empty)
return true;
return false;
}
/* Returns whether the assigned entry n in the particular row matches
the given number num. */
bool UsedInRow(int grid[N][N], int prow, int number)
{
for (int col = 0; col < N; col++)
if (grid[prow][col] == number)
return true;
return false;
}
/* Returns true if the number num matches any number in the column */
bool UsedInCol(int grid[N][N], int pcol, int number)
{
for (int row = 0; row < N; row++)
if (grid[row][pcol] == number)
return true;
else
return false;
//Check if the entry used already in the grid box
bool UsedInBox(int grid[N][N], int boxBeginRow, int boxBeginCol, int number)
{
bool tf = false;
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
if (grid[row+boxBeginRow][col+boxBeginCol] == number)
tf = true;
return tf;
}
/* Checks if num can be assigned to a given prow,pcol location. */
bool isGridSafe(int grid[N][N], int prow, int pcol, int number)
{
return !UsedInRow(grid, prow, number) && !UsedInCol(grid, pcol, number) &&
!UsedInBox(grid, prow - prow % 3 , pcol - pcol % 3, number);
}
/* print result */
void printResult(int finalgrid[N][N])
{
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
cout<< finalgrid[row][col]<<" ";
cout<<endl;
}
}
/* Main */
int main()
{
int grid[N][N] = {{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 3, 0, 8, 5},
{0, 0, 1, 0, 2, 0, 0, 0, 0},
{0, 0, 0, 5, 0, 7, 0, 0, 0},
{0, 0, 4, 0, 0, 0, 1, 0, 0},
{0, 9, 0, 0, 0, 0, 0, 0, 0},
{5, 0, 0, 0, 0, 0, 0, 7, 3},
{0, 0, 2, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 4, 0, 0, 0, 9}};
if (SolveSudoku(grid) == true)
printResult(grid);
else
cout<<"No solution found"<<endl;
return 0;
}