Submission #1326257

#TimeUsernameProblemLanguageResultExecution timeMemory
1326257dex111222333444555Tracks in the Snow (BOI13_tracks)C++20
100 / 100
641 ms127096 KiB
#include <bits/stdc++.h>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ii pair<int, int>
#define fi first
#define se second
#define all(v) begin(v), end(v)
#define siz(x) (int)(x).size()
using namespace std;
const int MAXN = 4005, MAXV = 5e6 + 6, MAXC = 10, LOG = 20, mod = 1e9 + 7, infINT = 1e9 + 23234;
const long long inf = 1e18 + 3;
const int dx[4] = {-1, 0, 0, +1};
const int dy[4] = {0, -1, +1, 0};
template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}
template<class X, class Y> bool maximize(X &x, const Y &y){return x < y ? x = y, 1: 0;}

void add(int &a, const int &b){
    a += b;
    if (a >= mod) a -= mod;
}

void sub(int &a, const int &b){
    a -= b;
    if (a < 0) a += mod;
}

int mul(const int &a, const int &b){
    return 1LL * a * 1LL * b % mod;
}

int bin_pow(int a, int k){
    int res = 1;
    while(k){
        if (k & 1) res = mul(res, a);
        k >>= 1; a = mul(a, a);
    }
    return res;
}

int numRow, numCol, dist[MAXN][MAXN];
char board[MAXN][MAXN];

void input(){
    cin >> numRow >> numCol;
    for(int row = 1; row <= numRow; row++)
    for(int col = 1; col <= numCol; col++) cin >> board[row][col];
}

bool isValid(int row, int col){
    return 1 <= row && row <= numRow && 1 <= col && col <= numCol && board[row][col] != '.';
}

void bfs(){
    memset(dist, 0x3f, sizeof dist);
    deque<ii> q;
    q.emplace_back(1, 1); dist[1][1] = 1;
    while(siz(q)){
        int row = q.front().fi, col = q.front().se;
        q.pop_front();
        for(int k = 0; k < 4; k++){
            int nrow = row + dx[k], ncol = col + dy[k];
            if (isValid(nrow, ncol)){
                int w = (board[row][col] != board[nrow][ncol]);
                if (minimize(dist[nrow][ncol], dist[row][col] + w)){
                    if (w == 0) q.emplace_front(nrow, ncol);
                    else q.emplace_back(nrow, ncol);
                }
            }
        }
    }
}

void solve(){
    bfs();
    int res = 0;
    for(int i = 1; i <= numRow; i++)
    for(int j = 1; j <= numCol; j++) if (board[i][j] != '.')
        res = max(res, dist[i][j]);
    cout << res << '\n';
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".in", "r")){
        freopen(task".in", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int t = 1;
//    cin >> t;
    while(t--){
        input();
        solve();
    }
    cerr << (1.0 * clock()) / CLOCKS_PER_SEC << ".s\n";
}

Compilation message (stderr)

tracks.cpp: In function 'int main()':
tracks.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |         freopen(task".in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...