-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1624.cpp
More file actions
257 lines (208 loc) · 5.67 KB
/
1624.cpp
File metadata and controls
257 lines (208 loc) · 5.67 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// https://acm.timus.ru/problem.aspx?space=1&num=1624
// constraint propagation + hypothesis tracking + bitsets + grid graph + consistency/uniqueness checks
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)((x).size())
#define rep(i,a,b) for (int i=(a); i<(b); ++i)
#define res reset
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
const int MAX_CELLS = 150;
const int MAX_EDGES = 300;
typedef bitset<MAX_CELLS> msk;
int w, h, k;
int n, eh;
struct sc_state
{
int pos[MAX_CELLS];
msk act;
msk wall[MAX_EDGES];
msk open[MAX_EDGES];
void init(int _n)
{
act.res();
rep(i,0,_n)
{
pos[i] = i;
act.set(i);
}
rep(e,0,MAX_EDGES)
{
wall[e].res();
open[e].res();
}
}
};
static sc_state s1, s2;
static msk mate[MAX_CELLS];
static inline int ge(int r, int c, char dir)
{
if (dir == 'N') return (r - 1) * w + c;
if (dir == 'S') return r * w + c;
if (dir == 'W') return eh + r * (w - 1) + (c - 1);
if (dir == 'E') return eh + r * (w - 1) + c;
return -1;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
if (!(cin >> w >> h >> k)) return 0;
n = w * h;
eh = (h - 1) * w;
s1.init(n);
s2.init(n);
rep(i,0,n) mate[i].set();
if (n < MAX_CELLS)
{
msk keep;
rep(j,0,n) keep.set(j);
rep(i,0,n) mate[i] &= keep;
}
int sol = -1;
vi touch;
touch.reserve(n);
msk nw[MAX_EDGES];
msk no[MAX_EDGES];
rep(step,1,k + 1)
{
char dir, res;
cin >> dir >> res;
bool s1_turn = (step & 1);
sc_state *cur = s1_turn ? &s1 : &s2;
sc_state *opp = s1_turn ? &s2 : &s1;
msk died;
died.res();
if (!s1_turn) touch.clear();
rep(i,0,n)
{
if (!cur->act[i]) continue;
int p = cur->pos[i];
int r = p / w;
int c = p % w;
int nr = r, nc = c;
if (dir == 'N') nr--;
else if (dir == 'S') nr++;
else if (dir == 'E') nc++;
else if (dir == 'W') nc--;
bool oob = (nr < 0 || nr >= h || nc < 0 || nc >= w);
if (oob)
{
if (res == '+')
{ cur->act[i] = 0; died.set(i); } continue;
}
int e = ge(r, c, dir);
int tgt = nr * w + nc;
if (res == '+')
{
if (cur->wall[e][i])
{
cur->act[i] = 0;
died.set(i);
}
else
{
cur->open[e].set(i);
cur->pos[i] = tgt;
if (s1_turn) mate[i] &= ~opp->wall[e];
else
{
if (!no[e].any() && !nw[e].any()) touch.pb(e);
no[e].set(i);
}
}
}
else
{
if (cur->open[e][i])
{
cur->act[i] = 0;
died.set(i);
}
else
{
cur->wall[e].set(i);
if (s1_turn) mate[i] &= ~opp->open[e];
else
{
if (!no[e].any() && !nw[e].any()) touch.pb(e);
nw[e].set(i);
}
}
}
}
if (!s1_turn)
{
if (died.any())
{
msk alive = ~died;
rep(i,0,n) if (s1.act[i]) mate[i] &= alive;
}
for (int e : touch)
{
if (no[e].any())
{
msk bad1 = s1.wall[e];
msk bad2 = no[e];
rep(i,0,n) if (s1.act[i] && bad1[i]) mate[i] &= ~bad2;
no[e].res();
}
if (nw[e].any())
{
msk bad1 = s1.open[e];
msk bad2 = nw[e];
rep(i,0,n) if (s1.act[i] && bad1[i]) mate[i] &= ~bad2;
nw[e].res();
}
}
}
bool psb = 0;
bool uniq = 1;
if (sol == -1)
{
int p1 = -1;
int p2 = -1;
msk bad;
rep(i,0,n)
{
if (!s1.act[i]) continue;
if (!mate[i].any()) continue;
int x = s1.pos[i];
if (!psb)
{
psb = 1;
p1 = x;
rep(j,0,n)
{
if (mate[i][j])
{
p2 = s2.pos[j];
break;
}
}
bad.res();
rep(j,0,n) if (s2.pos[j] != p2) bad.set(j);
}
if (x != p1) { uniq = 0; break; }
if ((mate[i] & bad).any()) { uniq = 0; break; }
}
}
else
rep(i,0,n)
if (s1.act[i] && mate[i].any())
{ psb = 1; break; }
if (!psb)
{
cout << "A mistake has been made at step number " << step << '\n';
return 0;
}
if (uniq && sol == -1) sol = step;
}
if (sol != -1) cout << "The scouts are safe at step number " << sol << '\n';
else cout << "There is not enough data" << '\n';
return 0;
}