Submission #1137316

#TimeUsernameProblemLanguageResultExecution timeMemory
1137316henrllyTracks in the Snow (BOI13_tracks)C++20
2.19 / 100
1613 ms830172 KiB
// time-limit: 3000
#include <bits/stdc++.h>
#include <vector>

using namespace std;

#define ll long long

using vll = vector<ll>;
using vvll = vector<vector<ll> >;
using mpll = map<pair<ll, ll>, ll>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vector<int> >;
using pi = pair<int, int>;

void setIO(string name = "") {
    if (name.size()) {
        freopen((name + ".in").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}

void solve() {
    return;
}

int visited[4001][4001];
char m[4001][4001];
int xx, yy;
int h, w;
char cur;

bool findn(int x, int y) {
    if (x < 0 || y < 0 || x >= w || y >= h) return false;
    if (m[y][x] == '.' || m[y][x] == (cur == 'F' ? 'R' : 'F') || visited[y][x]) return false;
    visited[y][x] = 1;
    if (m[y][x] == cur) {
        xx = x;
        yy = y;
        return true;
    }
    return
        findn(x-1, y) ||
        findn(x+1, y) ||
        findn(x, y-1) ||
        findn(x, y+1);
}

void cover(int x, int y) {
    if (x < 0 || y < 0 || x >= w || y >= h) return;
    if (m[y][x] == '.' || visited[y][x]) return;
    visited[y][x] = 1;
    if (m[y][x] == cur) m[y][x] = 'C';
    cover(x-1, y);
    cover(x+1, y);
    cover(x, y-1);
    cover(x, y+1);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    // setIO("test");

    // for optimal solution, each color crosses meadow alternatingly
    // if same color cross >1 in a row, can just go back from end point for all the subsequent paths
    // construct tree that connects each continguous area of one color to the other color
    //

    cin >> h >> w;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> m[i][j];
        }
    }

    ll res = 0;
    cur = m[0][0];
    // "expand" from 0,0 until F or R reached
    while (true) {
        memset(visited, 0, sizeof(visited));
        xx = -1;
        yy = -1;
        if (!findn(0,0)) break;
        memset(visited, 0, sizeof(visited));
        cover(xx,yy);
        res++;
        cur = (cur == 'F' ? 'R' : 'F');
    }

    cout << res << endl;

    return 0;
}

Compilation message (stderr)

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