Submission #917155

#TimeUsernameProblemLanguageResultExecution timeMemory
9171550x34cTracks in the Snow (BOI13_tracks)C++17
100 / 100
781 ms124496 KiB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define endl '\n'

using namespace std;

int N, M;
vector<vector<bool>> vis;
vector<vector<char>> grid;
vector<vector<int>> depth;
int mvt[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

bool is_ok(int r, int c)
{
    return r >= 0 && c >= 0 && r < N && c < M && grid[r][c] != '.' && !vis[r][c];
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> N >> M;
    vis.resize(N, vector<bool>(M, false));
    grid.resize(N, vector<char>(M));
    depth.resize(N, vector<int>(M));

    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            cin >> grid[i][j];

    deque<pii> q;
    q.push_back({0, 0});
    depth[0][0] = 1;
    vis[0][0] = true;

    int res = 0;
    while (!q.empty())
    {
        int r, c;
        tie(r, c) = q.front();
        q.pop_front();

        res = max(res, depth[r][c]);
        for (int i = 0; i < 4; i++)
        {
            int rx = r + mvt[i][0], cx = c + mvt[i][1];
            if (!is_ok(rx, cx))
                continue;

            vis[rx][cx] = true;
            if (grid[rx][cx] == grid[r][c])
            {
                depth[rx][cx] = depth[r][c];
                q.push_front({rx, cx});
            }
            else
            {
                depth[rx][cx] = depth[r][c] + 1;
                q.push_back({rx, cx});
            }
        }
    }

    cout << res << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...