이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 && a[i][j] != -1;}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    //freopen("1.inp", "r", stdin);
    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];
                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... |