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 <string.h>
#define N 1000
#define M 1000
#define INF 0x3f3f3f3f
int di[] = { -1, 1, 0, 0 };
int dj[] = { 0, 0, -1, 1 };
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int main() {
static int aa[N][M], dd[N][M], qu[N * M];
int n, m, cnt, g, h, i, i_, j, j_, ij, d;
scanf("%d%d", &m, &n);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &aa[i][j]);
for (i = 0; i < n; i++)
memset(dd[i], 0x3f, m * sizeof *dd[i]);
cnt = 0;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (aa[i][j] == 1)
dd[i][j] = 0, qu[cnt++] = i * m + j;
for (h = 0; h < cnt; h++) {
ij = qu[h], i = ij / m, j = ij % m, d = dd[i][j] + 1;
for (g = 0; g < 4; g++) {
i_ = i + di[g], j_ = j + dj[g];
if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < m && aa[i_][j_] != -1 && dd[i_][j_] > d)
dd[i_][j_] = d, qu[cnt++] = i_ * m + j_;
}
}
d = 0;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (aa[i][j] != -1)
d = max(d, dd[i][j]);
if (d == INF)
d = -1;
printf("%d\n", d);
return 0;
}
Compilation message (stderr)
tomato.c: In function 'main':
tomato.c:18:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf("%d%d", &m, &n);
| ^~~~~~~~~~~~~~~~~~~~~
tomato.c:21:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf("%d", &aa[i][j]);
| ^~~~~~~~~~~~~~~~~~~~~~
# | 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... |
# | 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... |