답안 #59138

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
59138 2018-07-20T16:20:19 Z Benq 포탈들 (BOI14_portals) C++14
100 / 100
452 ms 49908 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 1000;

int R,C, distWall[MX][MX], dist[MX][MX];
pi fst[MX][MX][4];
char g[MX][MX];
int xd[4] = {0,1,0,-1}, yd[4] = {1,0,-1,0};

bool isWall(int x, int y) {
    if (x < 0 || x >= R || y < 0 || y >= C) return 1;
    return g[x][y] == '#';
}

 
void genWall() {
    queue<pi> q;
    F0R(i,R) F0R(j,C) distWall[i][j] = MOD;
    F0R(i,R) F0R(j,C) {
        if (isWall(i,j)) distWall[i][j] = 0;
        else {
            F0Rd(z,4) if (isWall(i+xd[z],j+yd[z])) {
                distWall[i][j] = 1;
                q.push({i,j});
            }
        }
    }
    
    while (sz(q)) {
        auto a = q.front(); q.pop();
        F0R(i,4) {
            pi A = {a.f+xd[i],a.s+yd[i]};
            if (isWall(A.f,A.s)) continue;
            if (distWall[A.f][A.s] != MOD) continue;
            distWall[A.f][A.s] = distWall[a.f][a.s]+1;
            q.push(A);
        }
    }
}

void genEdges() {
    F0R(i,R) F0R(j,C) {
        if (j == 0 || g[i][j-1] == '#') fst[i][j][0] = {i,j};
        else fst[i][j][0] = fst[i][j-1][0];
        
        if (i == 0 || g[i-1][j] == '#') fst[i][j][1] = {i,j};
        else fst[i][j][1] = fst[i-1][j][1];
    }
    F0Rd(i,R) F0Rd(j,C) {
        if (j == C-1 || g[i][j+1] == '#') fst[i][j][2] = {i,j};
        else fst[i][j][2] = fst[i][j+1][2];
        
        if (i == R-1 || g[i+1][j] == '#') fst[i][j][3] = {i,j};
        else fst[i][j][3] = fst[i+1][j][3];
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> R >> C;
    F0R(i,R) F0R(j,C) cin >> g[i][j];
    genWall();
    genEdges();
    pi st,en;
    F0R(i,R) F0R(j,C) {
        if (g[i][j] == 'S') st = {i,j};
        if (g[i][j] == 'C') en = {i,j};
    }
    F0R(i,R) F0R(j,C) dist[i][j] = MOD;
    priority_queue<pair<int,pi>,vector<pair<int,pi>>,greater<pair<int,pi>>> p;
    p.push({dist[st.f][st.s] = 0,st});
    while (sz(p)) {
        auto a = p.top(); p.pop();
        if (dist[a.s.f][a.s.s] < a.f) continue;
        
        F0R(z,4) {
            pi A = {a.s.f+xd[z],a.s.s+yd[z]};
            if (isWall(A.f,A.s)) continue;
            if (dist[A.f][A.s] > a.f+1) 
                p.push({dist[A.f][A.s] = a.f+1,A});
        }
        
        F0R(z,4) {
            pi A = fst[a.s.f][a.s.s][z];
            if (dist[A.f][A.s] > a.f+distWall[a.s.f][a.s.s]) 
                p.push({dist[A.f][A.s] = a.f+distWall[a.s.f][a.s.s],A});
        }
    }
    cout << dist[en.f][en.s];
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 552 KB Output is correct
3 Correct 3 ms 552 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 4 ms 708 KB Output is correct
6 Correct 3 ms 708 KB Output is correct
7 Correct 3 ms 748 KB Output is correct
8 Correct 2 ms 748 KB Output is correct
9 Correct 3 ms 748 KB Output is correct
10 Correct 2 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 748 KB Output is correct
2 Correct 3 ms 748 KB Output is correct
3 Correct 2 ms 752 KB Output is correct
4 Correct 3 ms 752 KB Output is correct
5 Correct 3 ms 752 KB Output is correct
6 Correct 3 ms 752 KB Output is correct
7 Correct 3 ms 752 KB Output is correct
8 Correct 2 ms 820 KB Output is correct
9 Correct 3 ms 1332 KB Output is correct
10 Correct 4 ms 1332 KB Output is correct
11 Correct 3 ms 1332 KB Output is correct
12 Correct 5 ms 1332 KB Output is correct
13 Correct 4 ms 1332 KB Output is correct
14 Correct 3 ms 1332 KB Output is correct
15 Correct 3 ms 1332 KB Output is correct
16 Correct 3 ms 1332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1332 KB Output is correct
2 Correct 3 ms 1332 KB Output is correct
3 Correct 2 ms 1332 KB Output is correct
4 Correct 3 ms 1332 KB Output is correct
5 Correct 12 ms 4552 KB Output is correct
6 Correct 19 ms 4552 KB Output is correct
7 Correct 19 ms 4624 KB Output is correct
8 Correct 12 ms 4624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4624 KB Output is correct
2 Correct 3 ms 4624 KB Output is correct
3 Correct 3 ms 4624 KB Output is correct
4 Correct 2 ms 4624 KB Output is correct
5 Correct 2 ms 4624 KB Output is correct
6 Correct 3 ms 4624 KB Output is correct
7 Correct 2 ms 4624 KB Output is correct
8 Correct 3 ms 4624 KB Output is correct
9 Correct 4 ms 4624 KB Output is correct
10 Correct 3 ms 4624 KB Output is correct
11 Correct 3 ms 4624 KB Output is correct
12 Correct 3 ms 4624 KB Output is correct
13 Correct 4 ms 4624 KB Output is correct
14 Correct 14 ms 4716 KB Output is correct
15 Correct 14 ms 4716 KB Output is correct
16 Correct 13 ms 4716 KB Output is correct
17 Correct 17 ms 4716 KB Output is correct
18 Correct 18 ms 4716 KB Output is correct
19 Correct 18 ms 4716 KB Output is correct
20 Correct 17 ms 4716 KB Output is correct
21 Correct 12 ms 4716 KB Output is correct
22 Correct 19 ms 4716 KB Output is correct
23 Correct 19 ms 4716 KB Output is correct
24 Correct 17 ms 4716 KB Output is correct
25 Correct 3 ms 4716 KB Output is correct
26 Correct 3 ms 4716 KB Output is correct
27 Correct 2 ms 4716 KB Output is correct
28 Correct 11 ms 4716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4716 KB Output is correct
2 Correct 3 ms 4716 KB Output is correct
3 Correct 3 ms 4716 KB Output is correct
4 Correct 4 ms 4716 KB Output is correct
5 Correct 2 ms 4716 KB Output is correct
6 Correct 2 ms 4716 KB Output is correct
7 Correct 3 ms 4716 KB Output is correct
8 Correct 2 ms 4716 KB Output is correct
9 Correct 4 ms 4716 KB Output is correct
10 Correct 4 ms 4716 KB Output is correct
11 Correct 3 ms 4716 KB Output is correct
12 Correct 3 ms 4716 KB Output is correct
13 Correct 4 ms 4716 KB Output is correct
14 Correct 12 ms 4716 KB Output is correct
15 Correct 14 ms 4716 KB Output is correct
16 Correct 14 ms 4716 KB Output is correct
17 Correct 13 ms 4716 KB Output is correct
18 Correct 17 ms 4716 KB Output is correct
19 Correct 17 ms 4716 KB Output is correct
20 Correct 14 ms 4716 KB Output is correct
21 Correct 14 ms 4716 KB Output is correct
22 Correct 18 ms 4716 KB Output is correct
23 Correct 14 ms 4716 KB Output is correct
24 Correct 333 ms 40888 KB Output is correct
25 Correct 432 ms 41020 KB Output is correct
26 Correct 445 ms 41964 KB Output is correct
27 Correct 333 ms 43036 KB Output is correct
28 Correct 267 ms 44148 KB Output is correct
29 Correct 281 ms 45032 KB Output is correct
30 Correct 342 ms 46012 KB Output is correct
31 Correct 14 ms 46012 KB Output is correct
32 Correct 452 ms 46892 KB Output is correct
33 Correct 3 ms 46892 KB Output is correct
34 Correct 5 ms 46892 KB Output is correct
35 Correct 303 ms 47968 KB Output is correct
36 Correct 4 ms 47968 KB Output is correct
37 Correct 13 ms 47968 KB Output is correct
38 Correct 171 ms 48976 KB Output is correct
39 Correct 222 ms 49908 KB Output is correct