답안 #386393

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
386393 2021-04-06T13:53:46 Z bytemask Tracks in the Snow (BOI13_tracks) C++17
100 / 100
1066 ms 243876 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://x...content-available-to-author-only...i.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
 
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

#define IOS         ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb          push_back
#define eb          emplace_back
#define vi          vector<int>
#define vvi         vector<vector<int>>
#define vii         vector<pii>
#define pii         pair<int,int>
#define all(a)      (a).begin(),(a).end()
#define ff          first
#define ss          second
#define sp(n)       setprecision(n)<<fixed
#define endl        '\n' 
#define umap        unordered_map<long long, long long, custom_hash>

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;        

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
template<typename T, typename U> static inline T amin(T &x, U y) {
    if (y < x)
        x = y;
    return x;
}
template<typename T, typename U> static inline T amax(T &x, U y) {
    if (x < y)
    x = y;
    return x;
}
inline void YN(int f){
    cout<< "NO\0YES" + 3*f << endl;
}
inline void yn(int f){
    cout<< "No\0Yes" + 3*f << endl;
}
inline void setIO(string name) {
    if(name.empty()) return ;
    freopen((name+".in").c_str(),"r",stdin);
    freopen((name+".out").c_str(),"w",stdout);
}

// -------------------Standard Traversal Moves---------------------
vi fx = {1 ,-1 ,0, 0}, fy = {0, 0, -1, 1};
// vi fx = {2, -2, 2, -2,  1, -1, 1, -1}, fy = {1, 1, -1, -1, 2, 2, -2, -2};
// vi fx = {1, 1, 1, -1, -1 , -1, 0, 0}, fy = {1, -1, 0, 1, -1, 0, 1, -1};
// ----------------------------------------------------------------

// #define int         long long
const long long INF = LLONG_MAX/2;
const int inf = INT_MAX/2;
const int N = 4000 + 5, K = 67, MOD = 1000000007;
string mat[N];
int d[N][N], vis[N][N];
void solve(){
    int n,m;
    cin>>n>>m;
    for(int i=0; i<n; i++){
        cin>>mat[i];
    }
    for(int i=0; i<N; i++) for(int j=0; j<N; j++) d[i][j] = inf;
    d[0][0] = 1;
    deque<pii> dq;
    dq.push_back(make_pair(0,0));
    int ans = 0;
    while(!dq.empty()){
        pii cur = dq.front();
        dq.pop_front();
        int cx = cur.ff, cy = cur.ss;
        if(vis[cx][cy]) continue;
        vis[cx][cy] = 1;
        amax(ans, d[cx][cy]);
        for(int i=0; i<4; i++){
            int nx = cx + fx[i], ny = cy + fy[i];
            if(nx<0 || nx>=n || ny<0 || ny>=m) continue;
            if(vis[nx][ny] || mat[nx][ny]=='.') continue;
            int dis = (mat[nx][ny] != mat[cx][cy]);
            d[nx][ny] = min(d[cx][cy] + dis, d[nx][ny]);
            if(dis) dq.push_back(make_pair(nx,ny));
            else dq.push_front(make_pair(nx,ny));
        }
    }
    cout<<ans<<endl;
}
int32_t main()
{
    IOS;
    string filename = "";
    setIO(filename);
    int TESTS=1;
    // cin>>TESTS;
    while(TESTS--)
    {
        solve();
    }
    // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.";
    return 0;
}

Compilation message

tracks.cpp: In function 'void setIO(std::string)':
tracks.cpp:66:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   66 |     freopen((name+".in").c_str(),"r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:67:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   67 |     freopen((name+".out").c_str(),"w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 66796 KB Output is correct
2 Correct 39 ms 63340 KB Output is correct
3 Correct 39 ms 63468 KB Output is correct
4 Correct 49 ms 66412 KB Output is correct
5 Correct 44 ms 64876 KB Output is correct
6 Correct 48 ms 63340 KB Output is correct
7 Correct 46 ms 63468 KB Output is correct
8 Correct 40 ms 63596 KB Output is correct
9 Correct 40 ms 63664 KB Output is correct
10 Correct 42 ms 64492 KB Output is correct
11 Correct 42 ms 64364 KB Output is correct
12 Correct 46 ms 65004 KB Output is correct
13 Correct 44 ms 64876 KB Output is correct
14 Correct 43 ms 64916 KB Output is correct
15 Correct 56 ms 66564 KB Output is correct
16 Correct 60 ms 66796 KB Output is correct
17 Correct 53 ms 66612 KB Output is correct
18 Correct 57 ms 66412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 78828 KB Output is correct
2 Correct 99 ms 74860 KB Output is correct
3 Correct 306 ms 138368 KB Output is correct
4 Correct 156 ms 92268 KB Output is correct
5 Correct 276 ms 114284 KB Output is correct
6 Correct 1066 ms 183644 KB Output is correct
7 Correct 53 ms 79468 KB Output is correct
8 Correct 51 ms 78828 KB Output is correct
9 Correct 41 ms 63596 KB Output is correct
10 Correct 39 ms 63340 KB Output is correct
11 Correct 54 ms 79212 KB Output is correct
12 Correct 42 ms 63980 KB Output is correct
13 Correct 96 ms 74860 KB Output is correct
14 Correct 93 ms 70764 KB Output is correct
15 Correct 64 ms 73164 KB Output is correct
16 Correct 66 ms 67948 KB Output is correct
17 Correct 194 ms 88252 KB Output is correct
18 Correct 163 ms 95084 KB Output is correct
19 Correct 129 ms 92268 KB Output is correct
20 Correct 104 ms 85356 KB Output is correct
21 Correct 200 ms 113516 KB Output is correct
22 Correct 256 ms 114520 KB Output is correct
23 Correct 353 ms 105580 KB Output is correct
24 Correct 196 ms 110324 KB Output is correct
25 Correct 643 ms 159340 KB Output is correct
26 Correct 660 ms 243876 KB Output is correct
27 Correct 823 ms 203620 KB Output is correct
28 Correct 1058 ms 183836 KB Output is correct
29 Correct 1039 ms 182072 KB Output is correct
30 Correct 948 ms 190420 KB Output is correct
31 Correct 1008 ms 135384 KB Output is correct
32 Correct 823 ms 183396 KB Output is correct