forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapsules.cpp
108 lines (87 loc) · 2.32 KB
/
capsules.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
#include <bits/stdc++.h>
using namespace std;
struct group {
vector<bool> used;
vector<pair<int,int>> coords;
int num;
};
bool cmp(const group& g1, const group& g2) {
return g1.coords.size() < g2.coords.size();
}
int n, m;
vector<vector<char>> v;
vector<group> groups;
bool works(int x, int y) {
for(int i = -1; i <= 1; i++) {
for(int j = -1; j <= 1; j++) {
if(i == 0 && j == 0) continue;
if(v[x+i][y+j] == v[x][y]) return false;
}
}
return true;
}
void solve(int currGroup, int currSpot) {
// Base case
if(currGroup == groups.size()) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cout << v[i][j] << " ";
}
cout << endl;
}
exit(0);
}
// If we've stepped too far in current group, move to next
if(groups[currGroup].coords.size() == currSpot) {
solve(currGroup+1,0);
return;
}
// For each number
for(int j = 1; j <= groups[currGroup].num; j++) {
// If we can place it, place it
if(groups[currGroup].used[j]) continue;
pair<int,int> coord = groups[currGroup].coords[currSpot];
v[coord.first][coord.second] = j + '0';
groups[currGroup].used[j] = true;
if(works(coord.first,coord.second)) {
solve(currGroup, currSpot+1);
}
groups[currGroup].used[j] = false;
v[coord.first][coord.second] = '-';
}
}
int main() {
cin >> n >> m;
v.resize(n+2,vector<char>(m+2,'-'));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> v[i][j];
}
}
int segments;
cin >> segments;
groups.resize(segments);
for(int i = 0; i < segments; i++) {
groups[i].used.resize(8,false);
int t;
cin >> t;
groups[i].num = t;
for(int j = 0; j < t; j++) {
int x, y;
cin.ignore();
cin.ignore();
cin >> x;
cin.ignore();
cin >> y;
cin.ignore();
if(v[x][y] == '-') {
groups[i].coords.push_back({x,y});
}
else {
groups[i].used[v[x][y]-'0'] = true;
}
}
}
sort(groups.begin(),groups.end(),cmp);
solve(0,0);
}