Submission #1119440

#TimeUsernameProblemLanguageResultExecution timeMemory
1119440CowTheCowTracks in the Snow (BOI13_tracks)C++14
100 / 100
1507 ms898708 KiB
#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

#include <iostream>
#include <stack>
#include <fstream>
#include <cstdio>
#include <numeric>
#include <time.h>
#include <sys/timeb.h>

#define double long double
#define vi vector<int>
#define pb push_back
#define ll long long
#define ld long double
#define fi first
#define se second
#define pii pair<int,int>
#define sz(a) (int)a.size()
#define revall(a) a.rbegin(), a.rend()
#define all(a) a.begin(),a.end()
#define int_max INT64_MAX/2
#define int_min INT64_MIN/2
#define multierase (a, x) a.erase(a.find(x))
#define pq priority_queue
#define pqr priority_queue<int, vector<int>, greater<int>>
#define contains (a, x) a.find(x)!=a.end()

#define MOD 1000000007
#define SMOD 998244353
#define ODDMOD 100000000

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

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

void read(vi& arr, int n) {
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        arr.pb(a);
    } 
}

void print(pii p) {
    cout<<"[ "<<p.first<<" "<<p.second<<" ]"<<endl;
}

void print(vi arr) {
    for(int i : arr) {
        if(i==int_max)
            cout<<"i ";
        else if(i==int_min)
            cout<<"-i ";
        else
            cout<<i<<" ";
    }
    cout<<endl;
}

int ceil_div(int a, int b) {
    if(a%b==0) {
        return a/b;
    }else {
        return (a/b)+1;
    }
}

int h,w;
vector<string> grid;
vector<vi> rep;

void ff(int x, int y, int crep, char gval) {
    if(x<0||y<0||x>=w||y>=h) return;
    if(grid[y][x]!=gval) return;
    if(rep[y][x]!=0) return;

    rep[y][x]=crep;
    ff(x-1, y, crep,gval);
    ff(x+1, y, crep,gval);
    ff(x,y+1,crep,gval);
    ff(x,y-1,crep,gval);
}   

void solve() {
    cin>>h>>w;

    grid.clear();
    for(int i=0;i<h;i++) {
        string s;
        cin>>s;
        grid.pb(s);
    }

    rep=vector<vi>(h, vi(w, 0));

    int crep=1;
    for(int i=0;i<h;i++) {
        for(int j=0;j<w;j++) {
            if(rep[i][j]==0&&grid[i][j]!='.') {
                ff(j,i,crep, grid[i][j]);
                crep++;
            }
        }
    }

    vector<vi> adj;
    adj.resize(crep);

    for(int i=0;i<h;i++) {
        for(int j=0;j<w;j++) {
            if(i!=(h-1)&&rep[i][j]!=0&&rep[i+1][j]!=0) {
                adj[rep[i][j]].pb(rep[i+1][j]);
                adj[rep[i+1][j]].pb(rep[i][j]);
            }
            
            if(j!=(w-1)&&rep[i][j]!=0&&rep[i][j+1]!=0) {
                adj[rep[i][j]].pb(rep[i][j+1]);
                adj[rep[i][j+1]].pb(rep[i][j]);
            }
        }
    }

    vi dist (crep, INT32_MAX);
    vi vis (crep, 0);

    priority_queue<pii> Q;
    Q.push({0, 1});
    dist[1]=0;

    while(!Q.empty()) {
        pii c=Q.top();
        Q.pop();

        if(vis[c.se]) continue;
        vis[c.se]=1;

        for(int i:adj[c.se]) {
            if(dist[i]>dist[c.se]+1) {
                dist[i]=dist[c.se]+1;
                Q.push({-dist[i], i});
            }
        }
    }
    
    int res=0;
    for(int i=1;i<crep;i++) {
        res=max(res, dist[i]);
    }
    cout<<res+1<<endl;
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    
    // int x;
    // cin>>x;

    // while(x>0) {
        solve();  
    //     x--;
    // }
}

Compilation message (stderr)

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