제출 #338291

#제출 시각아이디문제언어결과실행 시간메모리
338291nishuzTracks in the Snow (BOI13_tracks)C++14
100 / 100
894 ms129256 KiB
#include <bits/stdc++.h>

using namespace std;

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n, m, ans = 1;
    cin >> n >> m;
    vector <vector <char>> color(n+1, vector <char> (m+1));
    vector <vector <int>> depth(n+1, vector <int> (m+1));
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            cin >> color[i][j];
    auto valid = [&](int x, int y, int n, int m, vector <vector <char>>& color) {return x >= 1 && y >= 1 && x <= n && y <= m && color[x][y] != '.';};
    int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
    deque <pair <int, int>> dq;
    dq.push_front(make_pair(1, 1));
    depth[1][1] = 1;
    while (!dq.empty())
    {
        pair <int, int> cur = dq.front();
        dq.pop_front();
        ans = max(ans, depth[cur.first][cur.second]);
        for (int i = 0; i < 4; ++i)
        {
            int x = cur.first + dx[i], y = cur.second + dy[i];
            if (valid(x, y, n, m, color) && !depth[x][y])
            {
                if (color[cur.first][cur.second] == color[x][y])
                {
                    depth[x][y] = depth[cur.first][cur.second];
                    dq.push_front(make_pair(x, y));
                } else {
                    depth[x][y] = depth[cur.first][cur.second] + 1;
                    dq.push_back(make_pair(x, y));
                }
            }
        }
    }
    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...