Submission #1098103

#TimeUsernameProblemLanguageResultExecution timeMemory
1098103The_EurekaTracks in the Snow (BOI13_tracks)C++17
100 / 100
531 ms138940 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mk make_pair
#define pb push_back
#define alls(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define rep(i, n) for (int i = 1; i <= int(n); i++)
#define inc(i, l, r, d) for (int i = l; i <= r; i += d)
#define dec(i, r, l, d) for (int i = r; i >= l; i -= d) 
#define dbg(x) cerr << #x << " = " << x << endl;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
const ld eps = 1e-12;
const ll inf = 1e16;
const ll mod = 998244353;
const ll mod1 = 1e9 + 87;
const ll mod2 = 127397154761;
ll qpow(ll a, ll b) {if (b == 0) return 1; ll ans = qpow(a, b >> 1); ans = ans * ans % mod; if (b & 1) ans = ans * a % mod; return ans;}
using namespace std;

void IOS(string name = "") {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    if ((int)name.size()) {
        freopen((name + ".in").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}


const int maxn = 4005;
int m, n;
char s[maxn][maxn];
int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int dis[maxn][maxn];
bool vis[maxn][maxn];

bool check(int x1, int y1) {
    return x1 > 0 && y1 > 0 && x1 <= m && y1 <= n;
}


int main() {
    IOS();
    cin >> m >> n;
    rep(i, m) {
        rep(j, n) {
            cin >> s[i][j];
        }
    }
    if (s[1][1] == '.') {
        cout << 0 << endl;
        return 0;
    }
    deque<pii> dq;
    rep(i, m) {
        rep(j, n) {
            dis[i][j] = INT_MAX - 10;
        }
    }
    dis[1][1] = true;
    dq.pb(mk(1, 1));
    vis[1][1] = true;

    while (!dq.empty()) {
        pii u = dq.front(); dq.pop_front();
        forn(k, 4) {
            int x1 = u.first + d[k][0], y1 = u.second + d[k][1];
            if (!check(x1, y1)) continue;
            if (s[x1][y1] == '.') continue;
            pii v = mk(x1, y1);
            int w = (s[x1][y1] != s[u.first][u.second]);
            if (vis[x1][y1]) continue;
            if (dis[x1][y1] > dis[u.first][u.second] + w) {
                dis[x1][y1] = dis[u.first][u.second] + w;
                vis[x1][y1] = true;
                if (w == 1) {
                    dq.pb(v);
                }
                else {
                    dq.push_front(v);
                }
            }
        }
    }

    int ans = 0;
    rep(i, m) {
        rep(j, n) {
            if (s[i][j] != '.') {
                ans = max(ans, dis[i][j]);
            }
        }
    }
    cout << ans << endl;

    return 0;
}

Compilation message (stderr)

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