-
Notifications
You must be signed in to change notification settings - Fork 0
/
def-con-ctf-qualifier-2013-omgacm-4-writeup.html
429 lines (371 loc) · 63.6 KB
/
def-con-ctf-qualifier-2013-omgacm-4-writeup.html
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=endge, chrome=IE8" />
<meta name="viewport" content="initial-scale=1.0, width=device-width, user-scalable=no" />
<title>DEF CON CTF Qualifier 2013 OMGACM 4 Writeup — Titleless Sights</title>
<!--[if lte IE 8]><script type="text/javascript" src="http://james0zan.github.io/theme/js/html5shiv.js"></script><![endif]-->
<link rel="stylesheet" type="text/css" href="http://james0zan.github.io/theme/css/skeleton.css" />
<link rel="stylesheet" type="text/css" href="http://james0zan.github.io/theme/css/theme.css" />
<link rel="shortcut icon" type="image/png" href="http://james0zan.github.io/favicon.png" />
<!--[if lte IE 8]><link rel="shortcut icon" type="image/x-icon" href="http://james0zan.github.io/favicon.ico" /><![endif]-->
<link rel="alternate" type="application/atom+xml"
title="Titleless Sights — Flux Atom"
href="http://james0zan.github.io/feeds/atom.xml" />
<style>
li {
margin: 10px 0;
}
</style>
<meta name="author" content="Mingxing Zhang" />
<meta name="keywords" content="DEFCON, algorithm" />
<link rel="stylesheet" media="not print" type="text/css" href="http://james0zan.github.io/theme/css/pygments.css" />
</head>
<body>
<div id="page">
<header id="page-head">
<h1>
<a href="http://james0zan.github.io/index.html">Titleless Sights</a>
</h1>
</header>
<div id="page-body">
<article class="post" id="page-main" role="main">
<header class="post-header">
<h1>
<a rel="bookmark"
href="http://james0zan.github.io/def-con-ctf-qualifier-2013-omgacm-4-writeup.html"
title="Lien permanent vers «DEF CON CTF Qualifier 2013 OMGACM 4 Writeup»">
DEF CON CTF Qualifier 2013 OMGACM 4 Writeup
</a>
</h1>
<div class="meta">
<!-- includes/article_meta.html -->
On <time datetime="2013-06-18T00:00:00">Mar 18 Giugno 2013</time>
in <em><a href="http://james0zan.github.io/category/blog.html">Blog</a></em>
by <a href="http://james0zan.github.io/author/mingxing-zhang.html">Mingxing Zhang</a> <br />Tags: <span class="tag"><a rel="tag" href="http://james0zan.github.io/tag/defcon.html">DEFCON</a></span>, <span class="tag"><a rel="tag" href="http://james0zan.github.io/tag/algorithm.html">algorithm</a></span> </div>
</header>
<div class="post-content">
<h1>Crossposting</h1>
<p>This artical is also cross-posted in <a href="http://www.blue-lotus.net/">blue-lotus</a>'s official blog at <a href="http://www.blue-lotus.net/def-con-ctf-qualifier-2013-omgacm-4-writeup/">here</a>.</p>
<h1>Problem Description</h1>
<p>Each problem gives you a circuit board, which will have a dimension, a set of nulls (that trace cannot go through), a feed point, and a set of antenna points.</p>
<p>Your goal is to draw a trace for each antenna point that starts at the feed point, ends at the antenna point and does not intersect other traces.</p>
<p>The distance of these traces must also be equal for all antenna points.</p>
<h1>Our Solution</h1>
<p>Since we intuitively classify this problem as NP-Complete, we resort to iterative deepening depth-first search for solving it.</p>
<p>In our algorithm, we choose to "grow" the traces from antenna points, which means that all the traces simultaneously start from their respective antenna point and go one step further one by one.</p>
<p>This strategy forces that all the traces will be equal length with each other, thus many unnecessary states are avoided.<br />
And if one trace encounters with another trace before reaching the feed point, we can simply merge them into one to eschew intersection.</p>
<p>More specifically, in order to record the current state, we use:</p>
<ol>
<li>Nine bool variables for each point: one for the point and 8 for adjacent edges; </li>
<li>One pair < int, int > (one for 1 and the other for sqrt 2) variable for each point to store the distance from this point to the corresponding antenna point if it has been covered by a trace; </li>
<li>Current "growing" point for each trace; </li>
<li>Which trace’s turn is it to grow in this iteration; </li>
<li>The final distance, if one of the traces has reached the feed point; </li>
</ol>
<p>And we also use a set of pruning tricks to accelerate the algorithm.</p>
<ol>
<li>Iterative deepening on length of each path; </li>
<li>Limit the depth of dfs to be at most 30. (This ought to have been another iterative deepening argument, but we hard code it with an empirical number for simplifying the code); </li>
<li>Preprocess the minimal distance from each point to the feed point and combine this information with iterative deepening threshold for further pruning; </li>
<li>If one of the trace has already reached the feed point, use the distance for pruning. </li>
</ol>
<p>Here is another tip:</p>
<p>The intersections will not only happen at points but also in small squares, you may need to double check this.</p>
<h1>Code</h1>
<div class="highlight"><pre><span class="cp">#include <stdio.h></span>
<span class="cp">#include <stdlib.h></span>
<span class="cp">#include <map></span>
<span class="cp">#include <set></span>
<span class="cp">#include <vector></span>
<span class="cp">#include <queue></span>
<span class="cp">#include <string.h></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
<span class="k">typedef</span> <span class="kt">unsigned</span> <span class="kt">long</span> <span class="kt">long</span> <span class="n">llu</span><span class="p">;</span>
<span class="cp">#define MAX 100</span>
<span class="cp">#define mp make_pair</span>
<span class="cp">#define pb push_back</span>
<span class="kt">bool</span> <span class="n">Null</span><span class="p">[</span><span class="n">MAX</span><span class="p">][</span><span class="n">MAX</span><span class="p">],</span> <span class="n">Null2</span><span class="p">[</span><span class="n">MAX</span><span class="p">][</span><span class="n">MAX</span><span class="p">];</span>
<span class="kt">int</span> <span class="n">XY</span><span class="p">[</span><span class="n">MAX</span><span class="p">][</span><span class="n">MAX</span><span class="p">];</span>
<span class="kt">int</span> <span class="n">N</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">eN</span><span class="p">,</span> <span class="n">fX</span><span class="p">,</span> <span class="n">fY</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">UP</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">dy</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">};</span>
<span class="kt">int</span> <span class="n">dx</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">};</span>
<span class="kt">int</span> <span class="n">verse</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">8</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span> <span class="p">,</span> <span class="mi">1</span><span class="p">};</span>
<span class="n">set</span><span class="o"><</span><span class="n">vector</span><span class="o"><</span><span class="kt">bool</span><span class="o">></span> <span class="o">></span> <span class="n">S</span><span class="p">;</span>
<span class="n">pair</span><span class="o"><</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="o">></span> <span class="n">dp</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span>
<span class="n">mp</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="n">mp</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
<span class="n">mp</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="n">mp</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
<span class="n">mp</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
<span class="n">mp</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="n">mp</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
<span class="n">mp</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">};</span>
<span class="k">struct</span> <span class="n">node</span> <span class="p">{</span>
<span class="n">vector</span><span class="o"><</span><span class="kt">bool</span><span class="o">></span> <span class="n">t</span><span class="p">;</span>
<span class="n">vector</span><span class="o"><</span><span class="n">pair</span><span class="o"><</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="o">></span> <span class="o">></span> <span class="n">len</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">cnt</span><span class="p">;</span>
<span class="n">pair</span><span class="o"><</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="o">></span> <span class="n">ans</span><span class="p">;</span>
<span class="n">vector</span><span class="o"><</span><span class="n">pair</span><span class="o"><</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="o">></span> <span class="o">></span> <span class="n">now</span><span class="p">;</span>
<span class="n">vector</span><span class="o"><</span><span class="kt">bool</span> <span class="o">></span> <span class="n">used</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">I</span><span class="p">;</span>
<span class="p">};</span>
<span class="n">node</span> <span class="n">Key</span><span class="p">;</span>
<span class="kt">int</span> <span class="nf">get</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">x</span><span class="o">*</span><span class="n">M</span> <span class="o">+</span> <span class="n">y</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">pair</span><span class="o"><</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="o">></span> <span class="n">getLen</span><span class="p">(</span><span class="kt">int</span> <span class="n">j</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">mp</span><span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">len</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">].</span><span class="n">first</span><span class="p">,</span> <span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">].</span><span class="n">second</span><span class="p">)].</span><span class="n">first</span><span class="o">+</span><span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">first</span><span class="p">,</span>
<span class="n">Key</span><span class="p">.</span><span class="n">len</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">].</span><span class="n">first</span><span class="p">,</span> <span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">].</span><span class="n">second</span><span class="p">)].</span><span class="n">second</span><span class="o">+</span><span class="n">dp</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">second</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="n">output</span><span class="p">(</span><span class="n">node</span> <span class="n">ans</span><span class="p">)</span> <span class="p">{</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"Solution %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">P</span><span class="p">);</span>
<span class="kt">int</span> <span class="n">kk</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">L</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o"><</span><span class="n">N</span><span class="o">*</span><span class="n">M</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="n">kk</span><span class="o">++</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">tx</span> <span class="o">=</span> <span class="n">i</span> <span class="o">/</span> <span class="n">M</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">ty</span> <span class="o">=</span> <span class="n">i</span> <span class="o">%</span> <span class="n">M</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">j</span><span class="o"><</span><span class="mi">4</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">,</span> <span class="n">kk</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">ans</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">kk</span><span class="p">])</span>
<span class="n">L</span><span class="o">++</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">kk</span><span class="o">+=</span><span class="mi">4</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"Line %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">L</span><span class="p">);</span>
<span class="kt">int</span> <span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o"><</span><span class="n">N</span><span class="o">*</span><span class="n">M</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="n">k</span><span class="o">++</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">tx</span> <span class="o">=</span> <span class="n">i</span> <span class="o">/</span> <span class="n">M</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">ty</span> <span class="o">=</span> <span class="n">i</span> <span class="o">%</span> <span class="n">M</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">j</span><span class="o"><</span><span class="mi">4</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">,</span> <span class="n">k</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">ans</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"Segment %d %d %d %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">tx</span><span class="p">,</span> <span class="n">ty</span><span class="p">,</span> <span class="n">tx</span><span class="o">+</span><span class="n">dx</span><span class="p">[</span><span class="n">j</span><span class="p">],</span> <span class="n">ty</span><span class="o">+</span><span class="n">dy</span><span class="p">[</span><span class="n">j</span><span class="p">]);</span>
<span class="p">}</span>
<span class="n">k</span><span class="o">+=</span><span class="mi">4</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kt">bool</span> <span class="n">none</span><span class="p">()</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o"><</span><span class="n">eN</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">used</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="k">return</span> <span class="nb">false</span><span class="p">;</span>
<span class="k">return</span> <span class="nb">true</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="n">myabs</span><span class="p">(</span><span class="kt">int</span> <span class="n">xx</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">xx</span><span class="o"><</span><span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="n">xx</span><span class="p">;</span>
<span class="k">return</span> <span class="n">xx</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="n">UPPER_BOUND</span> <span class="o">=</span> <span class="mi">28</span><span class="p">;</span>
<span class="kt">void</span> <span class="nf">dfs</span><span class="p">(</span><span class="kt">int</span> <span class="n">depth</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">depth</span> <span class="o">></span> <span class="n">UPPER_BOUND</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">].</span><span class="n">first</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">].</span><span class="n">second</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">rand</span><span class="p">()</span> <span class="o">%</span> <span class="mi">8</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">iii</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">iii</span><span class="o"><</span><span class="mi">8</span><span class="p">;</span> <span class="n">iii</span><span class="o">++</span><span class="p">,</span> <span class="n">i</span><span class="o">=</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="mi">8</span><span class="p">)</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">newX</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">dx</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="kt">int</span> <span class="n">newY</span> <span class="o">=</span> <span class="n">y</span> <span class="o">+</span> <span class="n">dy</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="k">if</span> <span class="p">(</span><span class="n">newX</span> <span class="o"><</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">newX</span> <span class="o">>=</span> <span class="n">N</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">newY</span> <span class="o"><</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">newY</span> <span class="o">>=</span> <span class="n">M</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Null</span><span class="p">[</span><span class="n">newX</span><span class="p">][</span><span class="n">newY</span><span class="p">])</span> <span class="k">continue</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">XY</span><span class="p">[</span><span class="n">newX</span><span class="p">][</span><span class="n">newY</span><span class="p">]</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">||</span> <span class="n">getLen</span><span class="p">(</span><span class="n">i</span><span class="p">).</span><span class="n">first</span> <span class="o">+</span> <span class="n">getLen</span><span class="p">(</span><span class="n">i</span><span class="p">).</span><span class="n">second</span> <span class="o">+</span> <span class="n">XY</span><span class="p">[</span><span class="n">newX</span><span class="p">][</span><span class="n">newY</span><span class="p">]</span> <span class="o">></span> <span class="n">UP</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">cnt</span> <span class="o">></span> <span class="mi">0</span> <span class="o">&&</span>
<span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">ans</span><span class="p">.</span><span class="n">first</span> <span class="o"><</span> <span class="n">getLen</span><span class="p">(</span><span class="n">i</span><span class="p">).</span><span class="n">first</span> <span class="o">||</span> <span class="n">Key</span><span class="p">.</span><span class="n">ans</span><span class="p">.</span><span class="n">second</span> <span class="o"><</span> <span class="n">getLen</span><span class="p">(</span><span class="n">i</span><span class="p">).</span><span class="n">second</span><span class="p">)</span> <span class="p">)</span> <span class="k">continue</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">cnt</span> <span class="o">></span> <span class="mi">0</span> <span class="o">&&</span>
<span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">ans</span><span class="p">.</span><span class="n">first</span> <span class="o">==</span> <span class="n">getLen</span><span class="p">(</span><span class="n">i</span><span class="p">).</span><span class="n">first</span> <span class="o">&&</span> <span class="n">Key</span><span class="p">.</span><span class="n">ans</span><span class="p">.</span><span class="n">second</span> <span class="o">==</span> <span class="n">getLen</span><span class="p">(</span><span class="n">i</span><span class="p">).</span><span class="n">second</span><span class="p">)</span> <span class="p">)</span> <span class="k">continue</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="o">*</span> <span class="mi">9</span> <span class="o">+</span> <span class="mi">6</span><span class="p">])</span> <span class="k">continue</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">==</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="o">*</span> <span class="mi">9</span> <span class="o">+</span> <span class="mi">8</span><span class="p">])</span> <span class="k">continue</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">==</span> <span class="mi">7</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mi">9</span> <span class="o">+</span> <span class="mi">6</span><span class="p">])</span> <span class="k">continue</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">==</span> <span class="mi">5</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mi">9</span> <span class="o">+</span> <span class="mi">8</span><span class="p">])</span> <span class="k">continue</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">newX</span> <span class="o">==</span> <span class="n">fX</span> <span class="o">&&</span> <span class="n">newY</span> <span class="o">==</span> <span class="n">fY</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">cnt</span> <span class="o">></span> <span class="mi">0</span> <span class="o">&&</span> <span class="n">Key</span><span class="p">.</span><span class="n">ans</span> <span class="o">!=</span> <span class="n">getLen</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="k">continue</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">cnt</span><span class="o">++</span><span class="p">;</span> <span class="n">Key</span><span class="p">.</span><span class="n">ans</span> <span class="o">=</span> <span class="n">getLen</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[(</span><span class="n">newX</span><span class="o">*</span><span class="n">M</span> <span class="o">+</span> <span class="n">newY</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span> <span class="o">+</span> <span class="n">verse</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span> <span class="o">+</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">used</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">]</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">]</span> <span class="o">=</span> <span class="n">mp</span><span class="p">(</span><span class="n">newX</span><span class="p">,</span> <span class="n">newY</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">cnt</span> <span class="o">></span> <span class="mi">0</span> <span class="o">&&</span> <span class="n">none</span><span class="p">())</span> <span class="p">{</span>
<span class="n">output</span><span class="p">(</span><span class="n">Key</span><span class="p">);</span>
<span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="n">II</span> <span class="o">=</span> <span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">,</span> <span class="n">ttt</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="o">=</span><span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="n">eN</span><span class="p">;</span> <span class="n">ttt</span><span class="o"><</span><span class="n">eN</span><span class="p">;</span> <span class="n">ttt</span><span class="o">++</span><span class="p">,</span> <span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="o">=</span><span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="n">eN</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">used</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">])</span>
<span class="k">break</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">ttt</span> <span class="o"><</span> <span class="n">eN</span><span class="p">)</span> <span class="p">{</span>
<span class="n">dfs</span><span class="p">(</span><span class="n">depth</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[(</span><span class="n">newX</span><span class="o">*</span><span class="n">M</span> <span class="o">+</span> <span class="n">newY</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span> <span class="o">+</span> <span class="n">verse</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span> <span class="o">+</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">I</span> <span class="o">=</span> <span class="n">II</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">used</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">]</span> <span class="o">=</span> <span class="n">mp</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span>
<span class="n">Key</span><span class="p">.</span><span class="n">cnt</span><span class="o">--</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">newX</span><span class="p">,</span> <span class="n">newY</span><span class="p">)</span> <span class="o">*</span> <span class="mi">9</span><span class="p">])</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">len</span><span class="p">[</span><span class="n">newX</span><span class="o">*</span><span class="n">M</span> <span class="o">+</span> <span class="n">newY</span><span class="p">]</span> <span class="o">!=</span> <span class="n">getLen</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="k">continue</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[(</span><span class="n">newX</span><span class="o">*</span><span class="n">M</span> <span class="o">+</span> <span class="n">newY</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span> <span class="o">+</span> <span class="n">verse</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span> <span class="o">+</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">used</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">]</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">]</span> <span class="o">=</span> <span class="n">mp</span><span class="p">(</span><span class="n">newX</span><span class="p">,</span> <span class="n">newY</span><span class="p">);</span>
<span class="kt">int</span> <span class="n">II</span> <span class="o">=</span> <span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">,</span> <span class="n">ttt</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="o">=</span><span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="n">eN</span><span class="p">;</span> <span class="n">ttt</span><span class="o"><</span><span class="n">eN</span><span class="p">;</span> <span class="n">ttt</span><span class="o">++</span><span class="p">,</span> <span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="o">=</span><span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="n">eN</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">used</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">])</span>
<span class="k">break</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">ttt</span> <span class="o"><</span> <span class="n">eN</span><span class="p">)</span> <span class="p">{</span>
<span class="n">dfs</span><span class="p">(</span><span class="n">depth</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[(</span><span class="n">newX</span><span class="o">*</span><span class="n">M</span> <span class="o">+</span> <span class="n">newY</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span> <span class="o">+</span> <span class="n">verse</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span> <span class="o">+</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">I</span> <span class="o">=</span> <span class="n">II</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">used</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">]</span> <span class="o">=</span> <span class="n">mp</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[(</span><span class="n">newX</span><span class="o">*</span><span class="n">M</span> <span class="o">+</span> <span class="n">newY</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span><span class="p">]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[(</span><span class="n">newX</span><span class="o">*</span><span class="n">M</span> <span class="o">+</span> <span class="n">newY</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span> <span class="o">+</span> <span class="n">verse</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span> <span class="o">+</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">len</span><span class="p">[</span><span class="n">newX</span><span class="o">*</span><span class="n">M</span> <span class="o">+</span> <span class="n">newY</span><span class="p">]</span> <span class="o">=</span> <span class="n">getLen</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
<span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">]</span> <span class="o">=</span> <span class="n">mp</span><span class="p">(</span><span class="n">newX</span><span class="p">,</span> <span class="n">newY</span><span class="p">);</span>
<span class="kt">int</span> <span class="n">II</span> <span class="o">=</span> <span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">,</span> <span class="n">ttt</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="o">=</span><span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="n">eN</span><span class="p">;</span> <span class="n">ttt</span><span class="o"><</span><span class="n">eN</span><span class="p">;</span> <span class="n">ttt</span><span class="o">++</span><span class="p">,</span> <span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="o">=</span><span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="n">eN</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Key</span><span class="p">.</span><span class="n">used</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">])</span>
<span class="k">break</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">ttt</span> <span class="o"><</span> <span class="n">eN</span><span class="p">)</span> <span class="p">{</span>
<span class="n">dfs</span><span class="p">(</span><span class="n">depth</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[(</span><span class="n">newX</span><span class="o">*</span><span class="n">M</span> <span class="o">+</span> <span class="n">newY</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span><span class="p">]</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[(</span><span class="n">newX</span><span class="o">*</span><span class="n">M</span> <span class="o">+</span> <span class="n">newY</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span> <span class="o">+</span> <span class="n">verse</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span><span class="o">*</span><span class="mi">9</span> <span class="o">+</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">I</span> <span class="o">=</span> <span class="n">II</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">[</span><span class="n">Key</span><span class="p">.</span><span class="n">I</span><span class="p">]</span> <span class="o">=</span> <span class="n">mp</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="nf">dfs2</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o"><</span><span class="mi">8</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">newX</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">dx</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="kt">int</span> <span class="n">newY</span> <span class="o">=</span> <span class="n">y</span> <span class="o">+</span> <span class="n">dy</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="k">if</span> <span class="p">(</span><span class="n">newX</span> <span class="o"><</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">newX</span> <span class="o">>=</span> <span class="n">N</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">newY</span> <span class="o"><</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">newY</span> <span class="o">>=</span> <span class="n">M</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">Null</span><span class="p">[</span><span class="n">newX</span><span class="p">][</span><span class="n">newY</span><span class="p">])</span> <span class="k">continue</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">XY</span><span class="p">[</span><span class="n">newX</span><span class="p">][</span><span class="n">newY</span><span class="p">]</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&&</span> <span class="n">XY</span><span class="p">[</span><span class="n">newX</span><span class="p">][</span><span class="n">newY</span><span class="p">]</span> <span class="o"><=</span> <span class="n">XY</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span>
<span class="n">XY</span><span class="p">[</span><span class="n">newX</span><span class="p">][</span><span class="n">newY</span><span class="p">]</span> <span class="o">=</span> <span class="n">XY</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>
<span class="n">dfs2</span><span class="p">(</span><span class="n">newX</span><span class="p">,</span> <span class="n">newY</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="nf">solve</span><span class="p">()</span> <span class="p">{</span>
<span class="n">scanf</span><span class="p">(</span><span class="s">"%*s%d"</span><span class="p">,</span> <span class="o">&</span><span class="n">P</span><span class="p">);</span>
<span class="n">scanf</span><span class="p">(</span><span class="s">"%*s%*d"</span><span class="p">);</span>
<span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&</span><span class="n">N</span><span class="p">,</span> <span class="o">&</span><span class="n">M</span><span class="p">);</span>
<span class="n">scanf</span><span class="p">(</span><span class="s">"%*s%*d"</span><span class="p">);</span>
<span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&</span><span class="n">fX</span><span class="p">,</span> <span class="o">&</span><span class="n">fY</span><span class="p">);</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">N</span> <span class="o">*</span> <span class="n">M</span> <span class="o">*</span> <span class="mi">9</span><span class="p">);</span>
<span class="n">Key</span><span class="p">.</span><span class="n">len</span><span class="p">.</span><span class="n">resize</span><span class="p">(</span><span class="n">N</span> <span class="o">*</span> <span class="n">M</span><span class="p">);</span>
<span class="n">scanf</span><span class="p">(</span><span class="s">"%*s%d"</span><span class="p">,</span> <span class="o">&</span><span class="n">eN</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o"><</span><span class="n">eN</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">tx</span><span class="p">,</span> <span class="n">ty</span><span class="p">;</span>
<span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&</span><span class="n">tx</span><span class="p">,</span> <span class="o">&</span><span class="n">ty</span><span class="p">);</span>
<span class="n">Key</span><span class="p">.</span><span class="n">now</span><span class="p">.</span><span class="n">pb</span><span class="p">(</span><span class="n">mp</span><span class="p">(</span><span class="n">tx</span><span class="p">,</span> <span class="n">ty</span><span class="p">));</span>
<span class="n">Key</span><span class="p">.</span><span class="n">t</span><span class="p">[</span><span class="n">get</span><span class="p">(</span><span class="n">tx</span><span class="p">,</span> <span class="n">ty</span><span class="p">)</span> <span class="o">*</span> <span class="mi">9</span><span class="p">]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="n">Key</span><span class="p">.</span><span class="n">used</span><span class="p">.</span><span class="n">pb</span><span class="p">(</span><span class="nb">true</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="n">nN</span><span class="p">;</span>
<span class="n">scanf</span><span class="p">(</span><span class="s">"%*s%d"</span><span class="p">,</span> <span class="o">&</span><span class="n">nN</span><span class="p">);</span>
<span class="n">memset</span><span class="p">(</span><span class="n">Null</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Null</span><span class="p">));</span>
<span class="n">memset</span><span class="p">(</span><span class="n">Null2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Null2</span><span class="p">));</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o"><</span><span class="n">nN</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">tx</span><span class="p">,</span> <span class="n">ty</span><span class="p">;</span>
<span class="n">scanf</span><span class="p">(</span><span class="s">"%d%d"</span><span class="p">,</span> <span class="o">&</span><span class="n">tx</span><span class="p">,</span> <span class="o">&</span><span class="n">ty</span><span class="p">);</span>
<span class="n">Null</span><span class="p">[</span><span class="n">tx</span><span class="p">][</span><span class="n">ty</span><span class="p">]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="n">Null2</span><span class="p">[</span><span class="n">tx</span><span class="p">][</span><span class="n">ty</span><span class="p">]</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">memset</span><span class="p">(</span><span class="n">XY</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">XY</span><span class="p">));</span>
<span class="n">XY</span><span class="p">[</span><span class="n">fX</span><span class="p">][</span><span class="n">fY</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">dfs2</span><span class="p">(</span><span class="n">fX</span><span class="p">,</span> <span class="n">fY</span><span class="p">);</span>
<span class="n">node</span> <span class="n">base</span> <span class="o">=</span> <span class="n">Key</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">UP</span><span class="o">=</span><span class="mi">6</span><span class="p">;</span> <span class="p">;</span> <span class="n">UP</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="n">dfs</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="n">Key</span> <span class="o">=</span> <span class="n">base</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
<span class="n">solve</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
<h1>Result</h1>
<p>You can check out <a href="http://ascii.io/a/3644">http://ascii.io/a/3644</a> for the result.</p>
<p><strong>CAVEAT:</strong> We use many heuristics in the program, so it will not guarantee success for every run.</p>
</div>
<footer class="post-footer">
<div class="meta">
<!-- includes/article_meta.html -->
On <time datetime="2013-06-18T00:00:00">Mar 18 Giugno 2013</time>
in <em><a href="http://james0zan.github.io/category/blog.html">Blog</a></em>
by <a href="http://james0zan.github.io/author/mingxing-zhang.html">Mingxing Zhang</a> <br />Tags: <span class="tag"><a rel="tag" href="http://james0zan.github.io/tag/defcon.html">DEFCON</a></span>, <span class="tag"><a rel="tag" href="http://james0zan.github.io/tag/algorithm.html">algorithm</a></span> </div>
</footer>
</article> <!-- /#page-main -->
<aside id="page-side">
<!-- begin includes/sidebar.html -->
<nav>
<h3>Pages</h3>
<ul>
<li><a href="http://james0zan.github.io/index.html">Home</a></li>
<li><a href="http://james0zan.github.io/category/blog.html">Blog</a></li>
<li><a href="http://james0zan.github.io/category/bookmark.html">Bookmark</a></li>
</ul>
</nav>
<nav>
<h3>Projects</h3>
<ul>
<li><a href="http://james0zan.github.io/AI.html">AI</a></li>
</ul>
</nav>
<nav>
<h3>Meta</h3>
<ul>
<li><a href="http://james0zan.github.io/archives.html">Archives</a></li>
<li><a href="http://james0zan.github.io/tags.html">Tags</a></li>
</ul>
</nav>
<nav>
<h3>Contacts</h3>
<ul>
<li><a href="mailto:[email protected]">Gmail</a></li>
<li><a href="https://github.com/james0zan">Github</a></li>
<li><a href="http://www.linkedin.com/profile/view?id=80873276">Linkedin</a></li>
</ul>
<h3>Feeds</h3>
<ul>
<li><a href="http://james0zan.github.io/feeds/atom.xml">Atom</a></li>
<li><a href="http://james0zan.github.io/feeds/blog.atom.xml">Blog Feeds</a></li>
<li><a href="http://james0zan.github.io/feeds/bookmark.atom.xml">Bookmark Feeds</a></li>
</ul>
</nav>
<!-- end includes/sidebar.html --></aside> <!-- /#page-side -->
</div> <!-- /#page-body -->
<footer id="page-foot">
<p>
Powered by <a href="http://pelican.readthedocs.org">Pelican</a>; Theme from <a href="http://blog.1flow.net/">1flow team</a>.
</p>
<p><a id="github-link" href="https://github.com/james0zan/james0zan.github.io">Fork me on Github</a></p> </footer>
</div> <!-- /#page -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-40495434-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>