-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.cpp
165 lines (148 loc) · 3.89 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Author: Artin Zamani
Jan 2016
Description: A simple console Sudoku solver.
*/
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int TABLE_LEN = 9;
const int BLOCK_LEN = 3;
bool isLegit(int table[TABLE_LEN][TABLE_LEN], int x, int y, int value);
bool isSolvable(int table[TABLE_LEN][TABLE_LEN], string status[TABLE_LEN][TABLE_LEN]);
void printTable(int table[TABLE_LEN][TABLE_LEN]);
int main()
{
int table[TABLE_LEN][TABLE_LEN];
string status[TABLE_LEN][TABLE_LEN];
for (int y = 0; y < TABLE_LEN; y++)
{
for (int x = 0; x < TABLE_LEN; x++)
{
table[x][y] = 0;
status[x][y] = "undecided";
}
}
int givens;
char temp[10];
int tempX;
int tempY;
int val;
cin >> givens;
getchar();
for (int index = 0; index < givens; index++)
{
fgets(temp, 7, stdin);
tempY = temp[0] - '0' - 1;
tempX = temp[2] - '0' - 1;
val = temp[4] - '0';
table[tempX][tempY] = val;
status[tempX][tempY] = "fixed";
}
isSolvable(table, status);
printTable(table);
return 0;
}
bool isLegit(int table[TABLE_LEN][TABLE_LEN], int x, int y, int value)
{
for (int xPrime = 0; xPrime < TABLE_LEN; xPrime++)
{
if (value == table[xPrime][y])
return false;
}
for (int yPrime = 0; yPrime < TABLE_LEN; yPrime++)
{
if (value == table[x][yPrime])
return false;
}
for (int xPrime = 0; xPrime < BLOCK_LEN; xPrime++)
{
for (int yPrime = 0; yPrime < BLOCK_LEN; yPrime++)
{
if (x == x - x % BLOCK_LEN + xPrime && y == y - y % BLOCK_LEN + yPrime)
continue;
if (value == table[x - x % BLOCK_LEN + xPrime][y - y % BLOCK_LEN + yPrime])
return false;
}
}
return true;
}
bool isSolvable(int table[TABLE_LEN][TABLE_LEN], string status[TABLE_LEN][TABLE_LEN])
{
for (int y = 0; y < TABLE_LEN; y++)
{
for (int x = 0; x < TABLE_LEN; x++)
{
if (status[x][y] == "undecided")
{
bool isDeadEnd = true;
for (int val = 1; val <= TABLE_LEN; val++)
{
if (!isLegit(table, x, y, val))
continue;
else
{
table[x][y] = val;
status[x][y] = "decided";
if (!isSolvable(table, status))
{
status[x][y] = "undecided";
table[x][y] = 0;
continue;
}
else
isDeadEnd = false;
}
}
if (isDeadEnd)
return false;
}
}
}
return true;
}
void printTable(int table[TABLE_LEN][TABLE_LEN])
{
cout << " ";
for (int y = 0; y < TABLE_LEN; y++)
{
if (y % BLOCK_LEN == 0)
{
for (int x = 0; x < TABLE_LEN + BLOCK_LEN; x++)
{
cout << "--";
}
cout << "-" << endl << " ";
}
for (int x = 0; x < TABLE_LEN; x++)
{
if (x % BLOCK_LEN == 0)
{
cout << "| ";
}
if (table[x][y] == 0)
{
cout << " ";
}
else
{
cout << table[x][y] << " ";
}
if (x == TABLE_LEN - 1)
{
cout << "| ";
}
}
cout << endl << " ";
if (y == TABLE_LEN - 1)
{
for (int x = 0; x < TABLE_LEN + BLOCK_LEN; x++)
{
cout << "--";
}
cout << "-" << endl;
}
}
}