#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NM 1000000
#define INF 0x3f3f3f3f
int *ev[NM + 1], *ew[NM + 1], eo[NM + 1];
void append(int u, int v, int w) {
int o = eo[u]++;
if (o >= 2 && (o & o - 1) == 0) {
ev[u] = (int *) realloc(ev[u], o * 2 * sizeof *ev[u]);
ew[u] = (int *) realloc(ew[u], o * 2 * sizeof *ew[u]);
}
ev[u][o] = v, ew[u][o] = w;
}
int dd[NM + 1], iq[NM + 2], pq[NM + 1], cnt;
int lt(int i, int j) {
return dd[i] < dd[j];
}
int p2(int p) {
return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_add_last(int i) {
iq[pq[i] = ++cnt] = i;
}
int pq_remove_first() {
int i = iq[1], j = iq[cnt--];
if (j != i)
pq[j] = 1, pq_dn(j);
pq[i] = 0;
return i;
}
int main() {
static char cc[NM], cc_[NM + 1];
int n, m, i, i_, j, u, v, o;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%s", cc + i * m);
for (u = 0; u <= n * m; u++) {
ev[u] = (int *) malloc(2 * sizeof *ev[u]);
ew[u] = (int *) malloc(2 * sizeof *ew[u]);
}
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (cc[u = i * m + j] == '#') {
if (i > 0 && cc[v = (i - 1) * m + j] == '#')
append(u, v, 0), append(v, u, 0);
if (j > 0 && cc[v = i * m + (j - 1)] == '#')
append(u, v, 0), append(v, u, 0);
}
for (j = 0; j < m; j++)
for (i = n - 1, i_ = n, v = n * m; i >= 0; i--)
if (cc[u = i * m + j] == '#')
append(v, u, i_ - i - 1), i_ = i, v = u;
memset(dd, 0x3f, (n * m + 1) * sizeof *dd);
dd[n * m] = 0, pq_add_last(n * m);
while (cnt) {
u = pq_remove_first();
for (o = eo[u]; o--; ) {
int w, d;
v = ev[u][o], w = ew[u][o], d = dd[u] + w;
if (dd[v] > d) {
if (dd[v] == INF)
pq_add_last(v);
dd[v] = d, pq_up(v);
}
}
}
memset(cc_, '.', n * m * sizeof *cc_);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (cc[u = i * m + j] == '#')
cc_[(i + dd[u]) * m + j] = '#';
for (i = 0; i < n; i++) {
char c = cc_[(i + 1) * m];
cc_[(i + 1) * m] = 0;
printf("%s\n", cc_ + i * m);
cc_[(i + 1) * m] = c;
}
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
Main.c: In function 'main':
Main.c:63:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:65:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%s", cc + i * m);
| ^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
292 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
292 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
0 ms |
292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
95284 KB |
Output is correct |
2 |
Correct |
125 ms |
95192 KB |
Output is correct |
3 |
Correct |
138 ms |
95176 KB |
Output is correct |
4 |
Correct |
140 ms |
95220 KB |
Output is correct |
5 |
Correct |
113 ms |
95188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
292 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
292 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
0 ms |
292 KB |
Output is correct |
24 |
Correct |
139 ms |
95284 KB |
Output is correct |
25 |
Correct |
125 ms |
95192 KB |
Output is correct |
26 |
Correct |
138 ms |
95176 KB |
Output is correct |
27 |
Correct |
140 ms |
95220 KB |
Output is correct |
28 |
Correct |
113 ms |
95188 KB |
Output is correct |
29 |
Correct |
164 ms |
107084 KB |
Output is correct |
30 |
Correct |
166 ms |
107668 KB |
Output is correct |
31 |
Correct |
129 ms |
98380 KB |
Output is correct |
32 |
Correct |
77 ms |
59048 KB |
Output is correct |
33 |
Correct |
182 ms |
111212 KB |
Output is correct |
34 |
Correct |
220 ms |
118580 KB |
Output is correct |
35 |
Correct |
206 ms |
115076 KB |
Output is correct |
36 |
Correct |
158 ms |
106528 KB |
Output is correct |
37 |
Correct |
192 ms |
110752 KB |
Output is correct |
38 |
Correct |
158 ms |
107472 KB |
Output is correct |
39 |
Correct |
109 ms |
99792 KB |
Output is correct |
40 |
Correct |
204 ms |
110324 KB |
Output is correct |
41 |
Correct |
228 ms |
116508 KB |
Output is correct |
42 |
Correct |
250 ms |
119368 KB |
Output is correct |
43 |
Correct |
157 ms |
105308 KB |
Output is correct |
44 |
Correct |
193 ms |
109992 KB |
Output is correct |
45 |
Correct |
72 ms |
95472 KB |
Output is correct |
46 |
Correct |
195 ms |
112924 KB |
Output is correct |
47 |
Correct |
147 ms |
105428 KB |
Output is correct |
48 |
Correct |
181 ms |
112848 KB |
Output is correct |
49 |
Correct |
206 ms |
110432 KB |
Output is correct |
50 |
Correct |
272 ms |
119788 KB |
Output is correct |
51 |
Correct |
129 ms |
96240 KB |
Output is correct |
52 |
Correct |
226 ms |
101880 KB |
Output is correct |
53 |
Correct |
103 ms |
94452 KB |
Output is correct |
54 |
Correct |
115 ms |
94424 KB |
Output is correct |
55 |
Correct |
161 ms |
94348 KB |
Output is correct |
56 |
Correct |
132 ms |
94164 KB |
Output is correct |
57 |
Correct |
103 ms |
95064 KB |
Output is correct |
58 |
Correct |
155 ms |
97640 KB |
Output is correct |
59 |
Correct |
143 ms |
94192 KB |
Output is correct |
60 |
Correct |
160 ms |
98132 KB |
Output is correct |
61 |
Correct |
74 ms |
95060 KB |
Output is correct |
62 |
Correct |
95 ms |
94808 KB |
Output is correct |
63 |
Correct |
265 ms |
113888 KB |
Output is correct |
64 |
Correct |
151 ms |
94624 KB |
Output is correct |
65 |
Correct |
107 ms |
94228 KB |
Output is correct |
66 |
Correct |
160 ms |
94164 KB |
Output is correct |
67 |
Correct |
73 ms |
86748 KB |
Output is correct |
68 |
Correct |
59 ms |
86528 KB |
Output is correct |
69 |
Correct |
121 ms |
90736 KB |
Output is correct |
70 |
Correct |
58 ms |
86472 KB |
Output is correct |