이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <deque>
#define NMAX 4000
using namespace std;
int n, m, viz[NMAX+10][NMAX+10], cost[NMAX+10][NMAX+10], ans;
char v[NMAX+10][NMAX+10];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
deque <pair <int, int> > dq;
void bfs()
{   viz[1][1] = 1;
    while(!dq.empty())
        {   pair <int, int> a = dq.front();
            dq.pop_front();
            for(int t=0; t<4; t++)
                {   pair <int, int> vec = {dx[t] + a.first, dy[t] + a.second};
                    if(vec.first && vec.first <= n && vec.second && vec.second <= m
                       && !viz[vec.first][vec.second])
                        {   viz[vec.first][vec.second] = 1;
                            if(v[a.first][a.second] == v[vec.first][vec.second])
                                {   dq.push_front(vec);
                                    cost[vec.first][vec.second] = cost[a.first][a.second];
                                }
                            else
                                {   dq.push_back(vec);
                                    cost[vec.first][vec.second] = cost[a.first][a.second] + 1;
                                }
                        }
                }
        }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
        {   string s;
            cin >> s;
            for(int j=0; j<m; j++)
                {   v[i][j+1] = s[j];
                    if(s[j] == '.') viz[i][j+1] = 1;
                }
        }
    dq.push_front({1, 1});
    bfs();
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            ans = max(ans, cost[i][j]);
    cout << ans + 1<< '\n';
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |