Submission #418524

#TimeUsernameProblemLanguageResultExecution timeMemory
418524gsc2001Tracks in the Snow (BOI13_tracks)C++17
100 / 100
658 ms130868 KiB
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define f(i, l, r) for (int i = l; i <= r; i++)
#define debug(x) cout << #x << '=' << (x) << endl
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define ff first
#define ss second

using PLL = pair<ll, ll>;
using VL = vector<ll>;
using VLL = vector<pair<ll, ll>>;

// -------------------------------------------------------------------------------
constexpr int mod = 1e9 + 7;

void setIO(const string &name = "") {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (sz(name)) {
        freopen((name + ".in").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}

// -------------------------------------------------------------------------------
// ------------------------Template End ------------------------------------------
// -------------------------------------------------------------------------------
string grid[4004];
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int n, m;

bool valid(int x, int y) {
    return x >= 0 and x < n and y >= 0 and y < m and grid[x][y] != '.';
}

int dist[4004][4004];

int main() {
    setIO();
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> grid[i];
    }
    deque<pair<int, int>> q;
    q.push_back({0, 0});
    int ans = 1;
    dist[0][0] = 1;
    while (q.size()) {
        auto curr = q.front();
        q.pop_front();
        ans = max(ans, dist[curr.ff][curr.ss]);

        for (int i = 0; i < 4; i++) {
            int x = curr.ff + dx[i], y = curr.ss + dy[i];
            if (valid(x, y) and dist[x][y] == 0) {
                if (grid[curr.ff][curr.ss] == grid[x][y]) {
                    dist[x][y] = dist[curr.ff][curr.ss];
                    q.push_front({x, y});
                } else {
                    dist[x][y] = dist[curr.ff][curr.ss] + 1;
                    q.push_back({x, y});
                }
            }
        }
    }
    cout << ans;

    return 0;
}

Compilation message (stderr)

tracks.cpp: In function 'void setIO(const string&)':
tracks.cpp:25:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |         freopen((name + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:26:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...