-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku.cpp
More file actions
149 lines (130 loc) · 3.79 KB
/
sudoku.cpp
File metadata and controls
149 lines (130 loc) · 3.79 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <array>
using std::cout;
using std::endl;
#define N 9
#define BLANK 0
#define SPACE " "
#define LINE "|"
#define NEW_ROW "-------------------------------------"
#define GRID_FULL std::make_pair(9, 9)
// Function to print the Sudoku grid
void print_grid(int grid[N][N])
{
for (int i = 0; i < N; i++)
{
cout << SPACE << SPACE << SPACE << SPACE << endl;
cout << NEW_ROW << endl;
for (int j = 0; j < N; j++)
{
cout << SPACE;
if (BLANK == grid[i][j])
{
cout << SPACE;
}
else
{
cout << grid[i][j];
}
cout << SPACE;
cout << LINE;
}
}
cout << endl << NEW_ROW << endl << endl;;
}
// Function to check if a number is used in the specified row
bool used_in_row(int grid[N][N], int row, int num)
{
for (int col = 0; col < N; col++)
if (grid[row][col] == num)
{
return true;
}
return false;
}
// Function to check if a number is used in the specified column
bool used_in_col(int grid[N][N], int col, int num)
{
for (int row = 0; row < N; row++)
if (grid[row][col] == num)
{
return true;
}
return false;
}
// Function to check if a number is used in the specified 3x3 box
bool used_in_box(int grid[N][N], int box_start_row, int box_start_col, int num)
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
if (grid[row + box_start_row][col + box_start_col] == num)
{
return true;
}
return false;
}
// Function to check if it is safe to assign a number to a location
bool is_safe(int grid[N][N], int row, int col, int num)
{
return !used_in_row(grid, row, num) &&
!used_in_col(grid, col, num) &&
!used_in_box(grid, row - row % 3, col - col % 3, num);
}
// Function to get an unassigned location in the grid
std::pair<int, int> get_unassigned_location(int grid[N][N])
{
for (int row = 0; row < N; row++)
for (int col = 0; col < N; col++)
if (grid[row][col] == BLANK)
{
return std::make_pair(row, col);
}
return GRID_FULL;
}
// Function to solve the Sudoku puzzle
bool solve_sudoku(int grid[N][N])
{
if (GRID_FULL == get_unassigned_location(grid))
{
return true;
}
std::pair<int, int> row_and_col = get_unassigned_location(grid);
int row = row_and_col.first;
int col = row_and_col.second;
for (int num = 1; num <= 9; num++)
{
if (is_safe(grid, row, col, num))
{
grid[row][col] = num;
if (solve_sudoku(grid))
{
return true;
}
grid[row][col] = BLANK;
}
}
return false;
}
int main()
{
cout << "********************************\n\n\tSudoku Solver\n\n********************************" << endl << endl;
int grid[N][N] = { { 0, 9, 0, 0, 0, 0, 8, 5, 3 },
{ 0, 0, 0, 8, 0, 0, 0, 0, 4 },
{ 0, 0, 8, 2, 0, 3, 0, 6, 9 },
{ 5, 7, 4, 0, 0, 2, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 0, 6, 3, 7 },
{ 9, 4, 0, 1, 0, 8, 5, 0, 0 },
{ 7, 0, 0, 0, 0, 6, 0, 0, 0 },
{ 6, 8, 2, 0, 0, 0, 0, 9, 0 } };
print_grid(grid);
if (true == solve_sudoku(grid))
{
print_grid(grid);
}
else
{
cout << "No solution exists for the given Sudoku" << endl << endl;
}
return 0;
}