Submission #1321715

#TimeUsernameProblemLanguageResultExecution timeMemory
1321715maniikkTracks in the Snow (BOI13_tracks)C++17
100 / 100
398 ms118784 KiB
#include <bits/stdc++.h>
#pragma GCC Optimize("Ofast")
using namespace std;
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
int depth[4005][4005];
string s[4000];
int main()
{
    fast_cin();
    int h, w;
    cin >> h >> w;
    for (int i = 0; i < h; i++)
        cin >> s[i];
    deque<pair<int, int>> q;
    depth[0][0] = 1;
    q.push_back({0, 0});
    vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    auto inside = [&](int x, int y)
    {
        return x >= 0 && x < h && y >= 0 && y < w && s[x][y] != '.';
    };

    int ans = 1;
    while (!q.empty())
    {
        auto [x, y] = q.front();
        q.pop_front();
        ans = max(ans, depth[x][y]);
        for (auto [dx, dy] : dir)
        {
            int nx = x + dx, ny = y + dy;
            if (inside(nx, ny) && depth[nx][ny] == 0)
            {
                
                if (s[x][y] == s[nx][ny])
                {
                    depth[nx][ny] = depth[x][y];
                    q.push_front({nx, ny});
                }   
                else 
                {
                    depth[nx][ny] = depth[x][y] + 1;
                    q.push_back({nx, ny});
                }
            }
        }
    }

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