-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashi.cp.cc
220 lines (202 loc) · 5.46 KB
/
hashi.cp.cc
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <set>
#include <cstdio>
#include <limits>
#include <queue>
#include "constraint/constraint.h"
using namespace std;
struct Node {
int x, y;
int size;
int id;
set<int> links;
Node(int x_, int y_, int size_, int id_)
: x(x_), y(y_), size(size_), id(id_) {}
};
struct Link {
int a, b;
bool horizontal;
int id;
set<int> forbidden;
Link(int a_, int b_, bool horizontal_, int id_)
: a(a_), b(b_), horizontal(horizontal_), id(id_) {}
};
class SingleGroupConstraint : public ExternalConstraint {
const vector<Node>& nodes;
const vector<Link>& links;
public:
SingleGroupConstraint(const vector<Node>& nodes_, const vector<Link>& links_)
: nodes(nodes_), links(links_) {}
virtual bool operator()(const State* state) const {
vector<bool> visited(nodes.size(), false);
queue<int> next;
next.push(0);
visited[0] = true;
while (!next.empty()) {
int cur = next.front();
next.pop();
for (int ilink : nodes[cur].links) {
const auto& link = links[ilink];
int other = link.a == cur ? link.b : link.a;
if (!visited[other] && state->read_lmax(ilink) > 0) {
visited[other] = true;
next.push(other);
}
}
}
for (bool v : visited) {
if (!v) {
return false;
}
}
return true;
}
};
class NoCrossConstraint : public ExternalConstraint {
const vector<Link>& links;
public:
NoCrossConstraint(const vector<Link>& links_)
: links(links_) {}
virtual bool operator()(const State* state) const {
for (const auto& link : links) {
for (int other : link.forbidden) {
if (state->read_lmin(link.id) > 0 &&
state->read_lmin(other) > 0) {
return false;
}
}
}
return true;
}
};
class HashiSolver {
private:
int width, height;
const vector<string>& grid;
vector<Node> nodes;
vector<Link> links;
ConstraintSolver solver;
public:
HashiSolver(int width_, int height_, const vector<string>& grid_)
: width(width_), height(height_), grid(grid_) {
}
template<typename T>
void for_all_digits(T callback) {
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
if (isdigit(grid[j][i])) {
callback(j, i);
}
}
}
}
void degeometrize() {
vector<vector<int>> id(height, vector<int>(width, -1));
for_all_digits([&](int j, int i) {
id[j][i] = nodes.size();
nodes.push_back(Node(i, j, grid[j][i] - '0', nodes.size()));
});
for_all_digits([&](int j, int i) {
for (int ii = i + 1; ii < width; ii++) {
if (isdigit(grid[j][ii])) {
links.push_back(Link(id[j][i], id[j][ii], true, links.size()));
break;
}
}
for (int jj = j + 1; jj < height; jj++) {
if (isdigit(grid[jj][i])) {
links.push_back(Link(id[j][i], id[jj][i], false, links.size()));
break;
}
}
});
for (auto& l1 : links) {
if (l1.horizontal) {
int y = nodes[l1.a].y;
for (const auto& l2 : links) {
if (!l2.horizontal) {
int x = nodes[l2.a].x;
if (y > nodes[l2.a].y && y < nodes[l2.b].y &&
x > nodes[l1.a].x && x < nodes[l1.b].x) {
l1.forbidden.insert(l2.id);
}
}
}
}
}
for (const auto& link : links) {
nodes[link.a].links.insert(link.id);
nodes[link.b].links.insert(link.id);
}
}
void solve() {
for (auto& link : links) {
link.id = solver.create_variable(0, 2);
}
vector<LinearConstraint*> linear;
for (const auto& n : nodes) {
auto cons = new LinearConstraint(n.size, n.size);
for (auto link : n.links) {
cons->add_variable(links[link].id);
}
linear.push_back(cons);
solver.add_constraint(cons);
}
if (nodes.size() > 2) {
for (const auto& link : links) {
if (nodes[link.a].size == nodes[link.b].size &&
nodes[link.a].size <= 2) {
int size = nodes[link.a].size;
auto cons = new LinearConstraint(0, size - 1);
cons->add_variable(link.id);
solver.add_constraint(cons);
}
}
}
NoCrossConstraint no_cross(links);
solver.add_external_constraint(&no_cross);
SingleGroupConstraint single_group(nodes, links);
solver.add_external_constraint(&single_group);
solver.solve();
for (const auto& link : links) {
auto var = solver.value(link.id);
cout << "solution from node " << nodes[link.a].size
<< " to " << nodes[link.b].size << " is "
<< "(" << var << ")\n";
}
}
void print() {
FILE *f = fopen("hashi.dot", "wt");
fprintf(f, "graph {\n");
for (const auto& n : nodes) {
fprintf(f, "n%d_%d [label=%d\npos=\"%d,%d!\"]\n",
n.id, n.size, n.size, n.x, height - n.y - 1);
}
for (const auto& link : links) {
for (int i = 1; i <= solver.value(link.id); i++) {
fprintf(f, "n%d_%d -- n%d_%d;\n",
link.a, nodes[link.a].size,
link.b, nodes[link.b].size);
}
}
fprintf(f, "}\n");
fclose(f);
}
};
int main() {
int width, height;
cin >> width;
cin >> height;
vector<string> grid(height);
for (int i = 0; i < height; i++) {
cin >> grid[i];
}
HashiSolver s(width, height, grid);
s.degeometrize();
s.solve();
s.print();
return 0;
}