Submission #1370345

#TimeUsernameProblemLanguageResultExecution timeMemory
1370345atif2077Tracks in the Snow (BOI13_tracks)C++20
100 / 100
270 ms111976 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};

int n, m;

bool inside(int i, int j)
{
    return (i >= 0 && i < n && j >= 0 && j < m);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;

    vector<string> a(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    vector<vector<int>> dist(n, vector<int> (m, 0));
    deque<pair<int, int>> q;

    dist[0][0] = 1;
    int ans = 1;
    q.push_back({0, 0});

    while (!q.empty())
    {
        auto node = q.front();
        q.pop_front();

        int i = node.first;
        int j = node.second;

        int weight = dist[i][j];
        ans = max(ans, weight);

        for (int k = 0; k < 4; k++)
        {
            int nr = i + dr[k];
            int nc = j + dc[k];

            if (inside(nr, nc) && dist[nr][nc] == 0 && a[nr][nc] != '.')
            {
                int change = (a[i][j] != a[nr][nc]);
                dist[nr][nc] = weight + change;
                if (change)
                {
                    q.push_back({nr, nc});
                }
                else
                {
                    q.push_front({nr, nc});
                }
            }
        }
    }

    cout << ans;

    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...