-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.cpp
116 lines (112 loc) · 3.01 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <fstream>
#include <iostream>
using namespace std;
ifstream input("sudoku.inp");
ofstream output("sudoku.out");
int num[9][9];
int abc[9][9];
bool garo[9][9];
bool sero[9][9];
bool alpha[9][9];
bool division[9][9];
bool a;
bool en;
void solve(int x, int y) {
for (int i = 0; i < 9; i++) {
if (num[x][y] == 0) {
if ((garo[x][i] == 0) && (sero[y][i] == 0) && (alpha[abc[x][y] - 'a'][i] == 0)) {
num[x][y] = i + 1;
garo[x][i] = 1;
sero[y][i] = 1;
alpha[abc[x][y] - 'a'][i] = 1;
a = true;
if (x == 8 && y == 8) {
en = true;
return;
} else {
if (y == 8) {
solve(x + 1, 0);
} else {
solve(x, y + 1);
}
}
if (a == false) {
num[x][y] = 0;
garo[x][i] = 0;
sero[y][i] = 0;
alpha[abc[x][y] - 'a'][i] = 0;
}
}
} else {
if (en == true)
return;
if (division[x][y] == 1) {
if (a == false) {
return;
} else {
if (y == 8) {
if (x == 8) {
en = true;
return;
}
solve(x + 1, 0);
} else {
solve(x, y + 1);
}
}
}
}
if (i == 8 && en == false) {
a = false;
return;
}
}
return;
}
int main() {
int count;
input >> count;
int re = 1;
while (count != 0) {
a = true;
en = false;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
input >> num[i][j];
garo[i][j] = 0;
sero[i][j] = 0;
alpha[i][j] = 0;
division[i][j] = 0;
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
char v;
input >> v;
abc[i][j] = v;
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (num[i][j] != 0) {
int temp = abc[i][j] - 'a';
division[i][j] = 1;
alpha[temp][num[i][j] - 1] = 1;
garo[i][num[i][j] - 1] = 1;
sero[j][num[i][j] - 1] = 1;
}
}
}
solve(0, 0);
output << "Test Case No: " << re << "\n";
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
output << num[i][j] << " ";
}
output << "\n";
}
output << "\n";
count--;
re++;
}
}