제출 #1275509

#제출 시각아이디문제언어결과실행 시간메모리
1275509SoMotThanhXuan포탈들 (BOI14_portals)C++17
100 / 100
384 ms107644 KiB
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define REP(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define mp make_pair
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
#define Bit(x, i) ((x >> (i)) & 1)
#define Mask(i) (1 << (i))
#define Cnt(x) __builtin_popcount(x)
#define Cntll(x) __builtin_popcountll(x)
#define Ctz(x) __builtin_ctz(x)
#define Ctzll(x) __builtin_ctzll(x)
#define Clz(x) __builtin_clz(x)
#define Clzll(x) __builtin_clzll(x)
#define left __left
#define down __down
#define right __right
#define up __up
inline bool maximize(int &u, int v){ return v > u ? u = v, true : false; }
inline bool minimize(int &u, int v){ return v < u ? u = v, true : false; }
inline bool maximizell(long long &u, long long v){ return v > u ? u = v, true : false; }
inline bool minimizell(long long &u, long long v){ return v < u ? u = v, true : false; }
const int mod = 1e9 + 7;
inline int fastPow(int a, int n){
    if(n == 0) return 1;
    int t = fastPow(a, n >> 1);
    t = 1ll * t * t % mod;
    if(n & 1) t = 1ll * t * a % mod;
    return t;
}
inline void add(int &u, int v){ u += v; if(u >= mod) u -= mod; }
inline void sub(int &u, int v){ u -= v; if(u < 0) u += mod; }
const int maxN = 1e3 + 5;
const int inf = 1e9;
const long long infll = 1e18;
int up[maxN][maxN], down[maxN][maxN], left[maxN][maxN], right[maxN][maxN];
char c[maxN][maxN];
int dist[maxN][maxN];
int numRow, numCol;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
using T = tuple<int, int, int>;
priority_queue<T, vector<T>, greater<T>> q;
vector<tuple<int, int, int>> adj[maxN][maxN];
void addEdge(const int &u, const int &v, const int &x, const int &y, const int &w){
    adj[u][v].emplace_back(x, y, w);
}
void punch(int u, int v, int luv, int ruv, int tuv, int buv, int cost){
    // luv <-> ruv

}
void process(){
    cin >> numRow >> numCol;
    int si, sj, ei, ej;
    FOR(i, 1, numRow){
        FOR(j, 1, numCol){
            cin >> c[i][j];
            if(c[i][j] == 'S')si = i, sj = j;
            if(c[i][j] == 'C')ei = i, ej = j;
            dist[i][j] = inf;
        }
    }
    FOR(i, 1, numRow){
        int cur = 0;
        FOR(j, 1, numCol){
            if(c[i][j] == '#')cur = j;
            left[i][j] = cur + 1;
        }
        cur = numCol + 1;
        REP(j, numCol, 1){
            if(c[i][j] == '#')cur = j;
            right[i][j] = cur - 1;
        }
    }
    FOR(j, 1, numCol){
        int cur = 0;
        FOR(i, 1, numRow){
            if(c[i][j] == '#')cur = i;
            up[i][j] = cur + 1;
        }
        cur = numRow + 1;
        REP(i, numRow, 1){
            if(c[i][j] == '#')cur = i;
            down[i][j] = cur - 1;
        }
    }
    FOR(i, 1, numRow){
        FOR(j, 1, numCol){
            if(c[i][j] == '#') continue;
            int costl = j - left[i][j] + 1;
            int costr = right[i][j] - j + 1;
            int costu = i - up[i][j] + 1;
            int costd = down[i][j] - i + 1;
            addEdge(i, j, i, left[i][j], min({costu, costd, costr}));
            addEdge(i, j, i, right[i][j], min({costu, costd, costl}));
            addEdge(i, j, up[i][j], j, min({costl, costr, costd}));
            addEdge(i, j, down[i][j], j, min({costl, costr, costu}));
        }
    }
    dist[si][sj] = 0;
    q.emplace(0, si, sj);
    while(!q.empty()){
        auto[cost, u, v] = q.top();
        q.pop();
        if(cost > dist[u][v]) continue;
        FOR(k, 0, 3){
            int x = u + dx[k];
            int y = v + dy[k];
            if(x >= 1 && x <= numRow && v >= 1 && v <= numCol && c[x][y] != '#' && minimize(dist[x][y], cost + 1)){
                q.emplace(cost + 1, x, y);
            }
        }
        for(auto[x, y, w] : adj[u][v]){
            if(minimize(dist[x][y], cost + w)){
                q.emplace(cost + w, x, y);
            }
        }
    }
//    FOR(i, 1, numRow){
//        FOR(j, 1, numCol){
//            if(dist[i][j] == inf)cout  << -1 << ' ';
//            else cout << dist[i][j] << ' ';
//        }
//        cout << '\n';
//    }
//    if(dist[ei][ej] == inf){
//        cout << -1;
//        return;
//    }
    cout << dist[ei][ej];
}
#define LOVE "portals"
int main(){
    if(fopen(LOVE".inp", "r")){
        freopen(LOVE".inp", "r", stdin);
        freopen(LOVE".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
//    cin >> t;
    while(t--)
        process();
//    cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" ;
    return 0;
}




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

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