This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define taskname "1"
#include <bits/stdc++.h>
#define iii tuple<int,int,int>
using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, maxn = 4010;
int a[maxn][maxn], d[maxn][maxn], w, h;
deque<iii> dq;
bool val(int i, int j) {return min(i, j) >= 1 && i <= w && j <= h;}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin>>w>>h;
char c;
for (int i=1; i<=w; i++) for (int j=1; j<=h; j++) cin>>c, a[i][j] = c != '.' ? c : -1, d[i][j] = maxn * maxn;
d[1][1] = 1;
dq.emplace_back(1, 1, 1);
while (!dq.empty())
{
auto [i, j, dij] = dq.front(); dq.pop_front();
if (dij != d[i][j]) continue;
for (int k=0; k<4; k++)
{
int ci = i + dx[k], cj = j + dy[k];
if (val(ci, cj))
{
int wt = a[i][j] != a[ci][cj] && min(a[i][j], a[ci][cj]) != -1;
if (dij + wt < d[ci][cj])
{
d[ci][cj] = dij + wt;
if (wt) dq.emplace_back(ci, cj, dij+wt);
else dq.emplace_front(ci, cj, dij+wt);
}
}
}
}
int ans = -1;
for (int i=1; i<=w; i++) for (int j=1; j<=h; j++) ans = max(ans, d[i][j]);
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |