forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslidingblocks.cpp
185 lines (153 loc) · 4.32 KB
/
slidingblocks.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Their coords are given in the form {down x, right y}
vector<int> dx = {0,0,1,-1};
vector<int> dy = {1,-1,0,0};
vector<int> d = {'<','>','^','v'};
int main() {
int n, m, b;
cin >> n >> m >> b;
map<pair<int,int>, int> conv;
vector<pair<int,int>> coords(b);
vector<char> dir(b,'?');
for(int i = 0; i < b; i++) {
pair<int,int> p;
cin >> p.first >> p.second;
conv[p] = i;
coords[i] = p;
}
// {a,b} means 'a' must be placed before 'b'
set<pair<int,int>> edges;
vector<int> par(b,-1);
// BFS out from here to figure out what dir each block has
// additionally, make an edge for each 'move'
queue<int> q;
q.push(0);
dir[0] = '.';
while(!q.empty()) {
int curr = q.front();
q.pop();
if(par[curr] != -1) {
edges.insert({par[curr],curr});
}
int x = coords[curr].first;
int y = coords[curr].second;
for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(conv.count({nx,ny})) {
int next = conv[{nx,ny}];
if(dir[next] != '?') continue;
par[next] = curr;
dir[next] = d[i];
q.push(next);
}
}
}
// Debugging - print out all tiles' directions
/*
for(int i = 0; i < b; i++) {
cout << coords[i].first << " " << coords[i].second << " " << dir[i] << endl;
}
*/
// For each row and col, make sure we have all
// necessary edges added
map<int,set<int>> rows;
map<int,set<int>> cols;
for(auto i : coords) {
rows[i.first].insert(i.second);
cols[i.second].insert(i.first);
}
// For each block, depending on his direction, find the
// next largest or smallest value, add an edge
for(int i = 0; i < b; i++) {
int x = coords[i].first;
int y = coords[i].second;
bool works = false;
// Look for larger 'Y'
if(dir[i] == '<') {
auto it = rows[x].find(y);
it++;
if(it != rows[x].end()) {
works = true;
y = *it;
}
}
// Look for smaller 'Y'
if(dir[i] == '>') {
auto it = rows[x].find(y);
if(it != rows[x].begin()) {
works = true;
it--;
y = *it;
}
}
// Look for larger 'X'
if(dir[i] == '^') {
auto it = cols[y].find(x);
it++;
if(it != cols[y].end()) {
works = true;
x = *it;
}
}
// Look for smaller 'X'
if(dir[i] == 'v') {
auto it = cols[y].find(x);
if(it != cols[y].begin()) {
works = true;
it--;
x = *it;
}
}
// Add the edge from x,y to coods[i].first,second
if(works) {
if(!conv.count({x,y})) cout << "FUCK" << endl;
edges.insert({i, conv[{x,y}]});
}
}
// From all the edges, build a toposort graph
// If cycle, impossible, else just store and print
// the moves in order
vector<vector<int>> adj(b);
vector<int> deg(b,0);
for(auto i : edges) {
adj[i.first].push_back(i.second);
deg[i.second]++;
}
queue<int> zeroin;
for(int i = 0; i < b; i++) {
if(deg[i] == 0) {
zeroin.push(i);
}
}
int processed = 0;
vector<pair<char,int>> moves;
while(!zeroin.empty()) {
int curr = zeroin.front();
zeroin.pop();
processed++;
if(dir[curr] == '<' || dir[curr] == '>') {
moves.push_back({dir[curr],coords[curr].first});
}
if(dir[curr] == '^' || dir[curr] == 'v') {
moves.push_back({dir[curr],coords[curr].second});
}
for(auto next : adj[curr]) {
deg[next]--;
if(deg[next] == 0) {
zeroin.push(next);
}
}
}
if(processed == b) {
cout << "possible" << endl;
for(auto i : moves) {
cout << i.first << " " << i.second << '\n';
}
}
else {
cout << "impossible" << endl;
}
}