This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1000
#define M 1000
#define N_ (N * M)
#define INF 0x3f3f3f3f
int di[] = { 0, 0, -1, 1 };
int dj[] = { -1, 1, 0, 0 };
int *oj[N_], *ow[N_], oo[N_];
void link(int i, int j, int w) {
int o = oo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
oj[i] = (int *) realloc(oj[i], o * 2 * sizeof *oj[i]);
ow[i] = (int *) realloc(ow[i], o * 2 * sizeof *ow[i]);
}
oj[i][o] = j, ow[i][o] = w;
}
int iq[1 + N_], pq[N_], dd[N_], 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 dijkstra(int n, int s, int t) {
memset(dd, 0x3f, n * sizeof *dd);
dd[s] = 0, pq_add_last(s);
while (cnt) {
int i = pq_remove_first(), o;
if (i == t)
return dd[t];
for (o = 0; o < oo[i]; o++) {
int j = oj[i][o], d = dd[i] + ow[i][o];
if (dd[j] > d) {
if (dd[j] == INF)
pq_add_last(j);
dd[j] = d, pq_up(j);
}
}
}
return INF;
}
int main() {
static char cc[N][M + 1];
static int dd[N][M], qu[N * M];
int n, m, i, j, is, js, ic, jc, head, cnt;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%s", cc[i]);
head = cnt = 0;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (i == 0 || i == n - 1 || j == 0 || j == m - 1 || cc[i][j - 1] == '#' || cc[i][j + 1] == '#' || cc[i - 1][j] == '#' || cc[i + 1][j] == '#')
dd[i][j] = 0, qu[head + cnt++] = i * m + j;
else
dd[i][j] = n * m;
while (cnt) {
int ij, d, h;
ij = qu[cnt--, head++], i = ij / m, j = ij % m, d = dd[i][j] + 1;
for (h = 0; h < 4; h++) {
int i_ = i + di[h], j_ = j + dj[h];
if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < m && dd[i_][j_] > d)
dd[i_][j_] = d, qu[head + cnt++] = i_ * m + j_;
}
}
for (i = 0; i < n * m; i++) {
oj[i] = (int *) malloc(2 * sizeof *oj[i]);
ow[i] = (int *) malloc(2 * sizeof *ow[i]);
}
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (cc[i][j] != '#') {
if (i > 0 && cc[i - 1][j] != '#') {
link(i * m + j, (i - 1) * m + j, 1);
link((i - 1) * m + j, i * m + j, 1);
}
if (j > 0 && cc[i][j - 1] != '#') {
link(i * m + j, i * m + j - 1, 1);
link(i * m + j - 1, i * m + j, 1);
}
}
for (i = 0; i < n; i++) {
int j_;
for (j = 0, j_ = 0; j < m; j++)
if (cc[i][j] == '#')
j_ = j + 1;
else
link(i * m + j, i * m + j_, dd[i][j] + 1);
for (j = m - 1, j_ = m - 1; j >= 0; j--)
if (cc[i][j] == '#')
j_ = j - 1;
else
link(i * m + j, i * m + j_, dd[i][j] + 1);
}
for (j = 0; j < m; j++) {
int i_;
for (i = 0, i_ = 0; i < n; i++)
if (cc[i][j] == '#')
i_ = i + 1;
else
link(i * m + j, i_ * m + j, dd[i][j] + 1);
for (i = n - 1, i_ = n - 1; i >= 0; i--)
if (cc[i][j] == '#')
i_ = i - 1;
else
link(i * m + j, i_ * m + j, dd[i][j] + 1);
}
is = js = -1, ic = jc = -1;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (cc[i][j] == 'S')
is = i, js = j;
else if (cc[i][j] == 'C')
ic = i, jc = j;
printf("%d\n", dijkstra(n * m, is * m + js, ic * m + jc));
return 0;
}
Compilation message (stderr)
portals.c: In function 'link':
portals.c:18:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
18 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
portals.c: In function 'main':
portals.c:88:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
portals.c:90:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | scanf("%s", cc[i]);
| ^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |