제출 #1119433

#제출 시각아이디문제언어결과실행 시간메모리
1119433CowTheCowTracks in the Snow (BOI13_tracks)C++14
2.19 / 100
2016 ms702544 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<set<int>> 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]].insert(rep[i+1][j]);
                adj[rep[i+1][j]].insert(rep[i][j]);
            }
            
            if(j!=(w-1)&&rep[i][j]!=0&&rep[i][j+1]!=0) {
                adj[rep[i][j]].insert(rep[i][j+1]);
                adj[rep[i][j+1]].insert(rep[i][j]);
            }
        }
    }

    vi dist (crep, int_max);
    
    queue<int> Q;
    Q.push(1);
    dist[1]=0;

    while(!Q.empty()) {
        int c=Q.front();
        Q.pop();

        for(int i:adj[c]) {
            if(dist[i]>dist[c]+1) {
                dist[i]=dist[c]+1;
                Q.push(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--;
    // }
}

컴파일 시 표준 에러 (stderr) 메시지

tracks.cpp: In function 'void solve()':
tracks.cpp:32:26: warning: overflow in conversion from 'long int' to 'std::vector<int>::value_type' {aka 'int'} changes value from '4611686018427387903' to '-1' [-Woverflow]
   32 | #define int_max INT64_MAX/2
      |                          ^
tracks.cpp:141:20: note: in expansion of macro 'int_max'
  141 |     vi dist (crep, int_max);
      |                    ^~~~~~~
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...