-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.cpp
160 lines (126 loc) · 3.32 KB
/
solver.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
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
150
151
152
153
154
155
156
157
158
159
160
// Basic and Easy CPP Sudoku Solver
// Code By : @puneetpotter
// Puneet Rai
/* Ready to use Code
can be compiled using online or offline compilers */
#include <iostream>
#include<stdio.h>
using namespace std;
#define N 9 //defining grid dimension, that is 9 by 9
int sudokuboard[N][N];
/* If you don't want to give the input of 81 numbers,
you may always use the commented out pre-defined sudoku arrangement in a 2-D array
or you can edit the code and always pre-define by yourself for your use.
Thank You! */
// Code By : @puneetpotter
/*int sudokuboard[N][N] =
{
{0, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 1, 0, 0, 0},
{0, 7, 8, 0, 0, 0, 0, 0, 1},
{0, 0, 4, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 2, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0}
};*/
/* The checkColumn() function takes the column number
and the input number or value as its arguments
and check if the number already exists in the specified column or not*/
// Code By : @puneetpotter
bool checkColumn(int c, int value)
{
for (int r = 0; r < N; r++)
if (sudokuboard[r][c] == value)
return true;
return false;
}
/* The checkRow() function matches the input number with every number in the specified row
and returns true or false after verifying its presence */
// Code By : @puneetpotter
bool checkRow(int r, int value)
{
for (int c = 0; c < N; c++)
if (sudokuboard[r][c] == value)
return true;
return false;
}
/* */
bool checkBox(int initial_row, int initial_column, int value)
{
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
if (sudokuboard[r+initial_row][c+initial_column] == value)
return true;
return false;
}
/* showGame() function to print the sudoku board on the console screen */
// Code By : @puneetpotter
void showGame()
{
for (int r = 0; r < N; r++){
for (int c = 0; c < N; c++)
{
if(c == 3 || c == 6)
cout << " | ";
cout << sudokuboard[r][c]<<" ";
}
if(r == 2 || r == 5)
{
cout << endl;
for(int k = 0; k<N; k++)
cout << "---";
}
cout << endl;
}
}
/* The findBox() function looks for an empty box on the entire game board to see if the user has completed the game or not */
// Code By : @puneetpotter
bool findBox(int &r, int &c)
{
for (r = 0; r < N; r++)
for (c = 0; c < N; c++)
if (sudokuboard[r][c] == 0)
return true;
return false;
}
/* The function canEnter() takes the input and calls these above functions inside its body.
It checks the output they return*/
// Code By : @puneetpotter
bool canEnter(int r, int c, int value)
{
return !checkRow(r, value) && !checkColumn(c, value) && !checkBox(r-r%3,c-c%3,value);
}
/* */
bool finalSolution(){
int r, c;
if (!findBox(r, c))
return true;
for (int val = 1; val <= 9; val++)
{
if (canEnter(r, c, val))
{
sudokuboard[r][c] = val;
if (finalSolution())
return true;
sudokuboard[r][c] = 0;
}
}
return false;
}
/* The main() function, the user only calls the finalSolution(), which is the primary function that solves the sudoku game */
// Code By : @puneetpotter
int main(){
cout<<"Enter your 81 numbers (0 <= Input_Numbers <= 9) :"<<endl;
int sudokuboard[N][N];
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
cin>>sudokuboard[i][j];
}
}
if (finalSolution() == true)
showGame();
else
cout << "This game can not be solved";
}