-
Notifications
You must be signed in to change notification settings - Fork 0
/
so_doku_checker.cpp
165 lines (161 loc) · 4.54 KB
/
so_doku_checker.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
/*
Made by: Romeu I. L. Pires
for "Special topics in programming" course
in UFRJ (Universidade Federal do Rio de Janeiro),
on 2019.1 semester
- Problem PDF:
https://uva.onlinejudge.org/external/109/10957.pdf
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <math.h>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
enum {
INCORRECT ,
CORRECT_INCOMPLETE ,
CORRECT_COMPLETE ,
};
short checkRow( int row , const vector<short>& grid ){
short ret = CORRECT_COMPLETE;
set<short> passed;
short n;
for( size_t i = row*9 ; i < row*9 + 9 ; i += 1 ){
n = grid[i];
if( n == 0 ){
ret = CORRECT_INCOMPLETE;
}else{
if( passed.find(n) == passed.end() ){
passed.insert(n);
} else {
return INCORRECT;
}
}
}
return ret;
}
short checkColumn( int column , const vector<short>& grid ){
short ret = CORRECT_COMPLETE;
set<short> passed;
short n;
for( size_t i = column ; i < 81 ; i += 9 ){
n = grid[i];
if( n == 0 ){
ret = CORRECT_INCOMPLETE;
}else{
if( passed.find(n) == passed.end() ){
passed.insert(n);
} else {
return INCORRECT;
}
}
}
return ret;
}
short checkCell( int cell_x , int cell_y , const vector<short>& grid ){
short ret = CORRECT_COMPLETE;
set<short> passed;
short n;
for( size_t x = cell_x*3 ; x < cell_x*3 + 3 ; x ++ )
for( size_t y = cell_y*3 ; y < cell_y*3 + 3 ; y ++ )
{
n = grid[ 9*y + x ];
if( n == 0 ){
ret = CORRECT_INCOMPLETE;
}else{
if( passed.find(n) == passed.end() ){
passed.insert(n);
} else {
return INCORRECT;
}
}
}
return ret;
}
set<short> possibleValues( short row , short column , vector<short>& grid ){
set<short> ret , has;
for( size_t x = 0 ; x < 9 ; x++ )
has.insert( grid[ row*9 + x ] );
for( size_t y = 0 ; y < 9 ; y++ )
has.insert( grid[ y*9 + column ] );
for( size_t x = (column/3)*3 ; x < 3 + (column/3)*3 ; x++ )
for( size_t y = (row/3)*3 ; y < 3 + (row/3)*3 ; y++ )
has.insert( grid[ y*9 + x ] );
auto end = has.end();
for( short i = 1 ; i <= 9 ; i++ )
if( has.find(i) == end )
ret.insert(i);
return ret;
}
bool validSudoku( vector<short>& grid ){
for( size_t x = 0 ; x < 9 ; x++ )
if( checkColumn(x,grid) == INCORRECT )
return false;
for( size_t y = 0 ; y < 9 ; y++ )
if( checkRow(y,grid) == INCORRECT )
return false;
for( size_t x = 0 ; x < 3 ; x++ )
for( size_t y = 0 ; y < 3 ; y++ )
if( checkCell( x , y , grid ) == INCORRECT )
return false;
return true;
}
void backtracking( vector<short>& grid , vector< vector<short> >& solutions ){
if( solutions.size() > 1 ) return;
short next_pos = -1;
for( short i = 0 ; i < 81 ; i++ ){
if(grid[i]==0){
next_pos = i ;
break ;
}
}
if( next_pos == -1 ){
// check if ok?
solutions.push_back(grid);
}else{
set<short> available_numbers = possibleValues(next_pos/9,next_pos%9,grid);
for( auto n : available_numbers ){
vector<short> new_grid = vector<short>(grid);
new_grid[next_pos] = n;
backtracking( new_grid , solutions );
}
}
}
int main(){
#ifndef ONLINE_JUDGE
ifstream cin("entrada.txt");
ofstream cout("saida.txt");
#endif
// ==========
int N , case_number = 1;
vector<short> grid;
while( cin >> N ){
grid.clear();
grid.push_back(N);
for( int i = 0 ; i < 80 ; i++ ){
cin >> N;
grid.push_back(N);
}
vector< vector<short> > solutions;
if( !validSudoku(grid) ){
cout << "Case " << case_number++ << ": Illegal." << endl;
}else{
backtracking( grid , solutions );
switch( solutions.size() ){
case 0 :
cout << "Case " << case_number++ << ": Impossible." << endl;
break;
case 1:
cout << "Case " << case_number++ << ": Unique." << endl;
break;
default:
cout << "Case " << case_number++ << ": Ambiguous." << endl;
break;
}
}
}
}