-
Notifications
You must be signed in to change notification settings - Fork 0
/
backtrack_common.cpp
115 lines (101 loc) · 1.92 KB
/
backtrack_common.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
#include <iostream>
#include <cassert>
#include "coords.h"
#include "backtrack_common.h"
Grid::Grid() {
data = new int[81];
}
Grid::~Grid() {
delete [] data;
}
Grid::Grid(const Grid& grid) {
std::cout << "debugA" << std::endl;
data = new int[81];
for(int i=0; i < 81; i++) {
data[i] = grid.data[i];
}
}
Grid& Grid::operator=(const Grid& grid) {
std::cout << "debugB" << std::endl;
if(this != &grid) {
delete [] data;
data = new int[81];
for(int i=0; i < 81; i++) {
data[i] = grid.data[i];
}
}
return *this;
}
void Grid::init() {
for(int i=0; i < 81; i++) {
data[i] = 0;
}
}
int Grid::get(Spot p) {
return data[idx_of_spot(p)];
}
int Grid::getmanual(int idx) {
return data[idx];
}
void Grid::set(Spot p, int val) {
data[idx_of_spot(p)] = val;
}
void Grid::setmanual(int val, int idx) {
data[idx] = val;
}
bool Grid::covered(Spot p, int val) {
int boxid = boxid_of_spot(p);
for(int i=0; i < 9; i++) {
if(i != p.col) {
if(getmanual(idx_of_row(p.row, i)) == val) {
return true;
}
}
if(i != p.row) {
if(getmanual(idx_of_col(p.col, i)) == val) {
return true;
}
}
Spot pk = spot_of_boxid(boxid, i);
if(pk.row != p.row || pk.col != p.col) {
if(get(pk) == val) {
return true;
}
}
}
return false;
}
void Grid::write_to_psout(Psout& ps) {
for(int i=0; i < 81; i++) {
ps.cellvalues[i] = data[i];
}
}
std::ostream& operator << (std::ostream& os, Grid& grid) {
for(int i=0; i < 9; i++) {
for(int j=0; j < 9; j++) {
int spot = grid.get(Spot(i,j));
if(spot == 0) {
os << ".";
} else {
os << spot;
}
}
//os << std::endl;
}
os << std::endl;
return os;
}
std::istream& operator >> (std::istream& is, Grid& grid) {
char buf[90];
is.getline(buf, 90);
for(int i=0; i < 81; i++) {
if(buf[i] == '.') {
grid.setmanual(0, i);
} else if (buf[i] >= 49 && buf[i] <= 57){
grid.setmanual(buf[i] - 48, i);
} else {
assert(false);
}
}
return is;
}