This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.cpp
143 lines (121 loc) · 3.43 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
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
/*
Run the program like this : ./a.exe <file.txt>
where <file.txt> contains a sudoku table that should be looking like this :
0 0 0 0 9 0 0 0 6
7 8 0 0 0 0 0 0 0
0 9 2 0 0 8 4 0 5
0 0 0 4 0 0 0 2 0
0 3 0 1 0 2 0 5 0
0 1 0 0 0 9 0 0 0
4 0 5 8 0 0 1 6 0
0 0 0 0 0 0 0 8 7
9 0 0 0 0 1 0 0 0
note : 0s are empty squares
*/
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int table[9][9];
int open=0; // # of uncompleted squares
long int backtracks=0;
vector< pair<int,int> > squares;
void readfile(const char* filename){
pair<int,int> tmp;
ifstream fin("table.in");
if(!fin.is_open()){
fprintf(stderr, "Error opening file. Exiting... \n");
exit(1);
}
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
fin>>table[i][j];
if(table[i][j]==0) {
open++;
tmp.first=i;
tmp.second=j;
squares.push_back(tmp);
}
}
}
}
void print_table(){
printf("\n########################################\n");
for(int i=0;i<9;i++){
if(i!=0){
if(i%3==0) {
printf("\b#\n########################################\n");
}
else printf("\b#\n# # # #\n");
}
for(int j=0;j<9;j++){
if(j%3==0) printf("\b# ");
printf("%d ",table[i][j]);
}
}
printf("\b#\n########################################\n\n");
}
bool check(pair<int,int> cur){
/// Check column
for(int i=0;i<cur.first;i++) {
if(table[i][cur.second]==table[cur.first][cur.second]) return false;
}
for(int i=cur.first+1;i<9;i++) {
if(table[i][cur.second]==table[cur.first][cur.second]) return false;
}
/// Check row
for(int j=0;j<cur.second;j++) {
if(table[cur.first][j]==table[cur.first][cur.second]) return false;
}
for(int j=cur.second+1;j<9;j++) {
if(table[cur.first][j]==table[cur.first][cur.second]) return false;
}
// Check square
int start_i,start_j;
if(cur.first<=2) start_i=0;
else if(cur.first>=6) start_i=6;
else start_i=3;
if(cur.second<=2) start_j=0;
else if(cur.second>=6) start_j=6;
else start_j=3;
for(int i=start_i;i<start_i+3;i++){
for(int j=start_j;j<start_j+3;j++){
if(table[i][j]==table[cur.first][cur.second] && (i!=cur.first && j!=cur.second)) return false;
}
}
return true;
}
void run(){
int idx=0;
pair<int,int> current;
bool skipped=false;
while(idx<open){
current=squares[idx];
if(table[current.first][current.second]!=0 && check(current) && !skipped) {
idx++;
}
else {
if( table[current.first][current.second] == 9 ){
table[current.first][current.second] = 0;
skipped=true;
backtracks++;
idx--;
}
else {
table[current.first][current.second]++;
skipped=false;
}
}
}
}
int main(int argc, char** argv){
if(argc == 1){
fprintf(stderr, "File name not given. Exiting...\n");
exit(1);
}
readfile(argv[1]);
run();
print_table();
printf("backtracks : %d\n\n",backtracks);
return 0;
}