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 <bits/stdc++.h>
using namespace std;
#define N 5005
char a[N][N]; int n, m, z, d[N][N], dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
deque<pair<int, int>> q;
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
memset(d, 63, sizeof d);
q.emplace_back(1, 1); d[1][1] = 1;
while (q.size())
{
auto [y, x] = q.front(); q.pop_front();
z = max(z, d[y][x]);
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i], ny = y + dy[i];
if (!a[ny][nx] || a[ny][nx] == '.') continue;
if (a[ny][nx] == a[y][x] && d[ny][nx] > d[y][x])
{
d[ny][nx] = d[y][x];
q.emplace_front(ny, nx);
}
if (a[ny][nx] != a[y][x] && d[ny][nx] > d[y][x] + 1)
{
d[ny][nx] = d[y][x] + 1;
q.emplace_back(ny, nx);
}
}
}
printf("%d", z);
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'int main()':
tracks.cpp:10:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
tracks.cpp:11:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
| ~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |