답안 #298598

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
298598 2020-09-13T11:24:57 Z shrek12357 포탈들 (BOI14_portals) C++14
100 / 100
449 ms 42108 KB
#include<bits/stdc++.h>
using namespace std;
 
 
const int N = 1005,
          INF = 1e9,
          dx[] = {-1, 0, 1, 0},
          dy[] = {0, -1, 0, 1};
 
 
int n, m;
char a[N][N];
int dist_to_wall[N][N], d[N][N];
int sx, sy, tx, ty;
pair < int, int > nxt[4][N][N];
 
inline bool is_wall(int x, int y){
    return x < 1 || x > n || y < 1 || y > m || a[x][y] == '#';
}
 
void walls(){
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            dist_to_wall[i][j] = INF;
        }
    }
    queue < pair < int, int > > q;
    for(int i = 0; i <= n + 1; i++){
        for(int j = 0; j <= m + 1; j++){
            if(is_wall(i, j)){
                dist_to_wall[i][j] = 0;
                q.push(make_pair(i, j));
            }
        }
    }
    while(!q.empty()){
        int x = q.front().first,
            y = q.front().second;
        q.pop();
        for(int i = 0; i < 4; i++){
            int xx = x + dx[i],
                yy = y + dy[i];
            if(is_wall(xx, yy)){
                continue;
            }
            if(dist_to_wall[xx][yy] > dist_to_wall[x][y] + 1){
                dist_to_wall[xx][yy] = dist_to_wall[x][y] + 1;
                q.push(make_pair(xx, yy));
            }
        }
    }
}
 
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> a[i][j];
        }
    }
    walls();
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(j == 1 || a[i][j - 1] == '#'){
                nxt[0][i][j] = make_pair(i, j);
            }
            else{
                nxt[0][i][j] = nxt[0][i][j - 1];
            }
            if(i == 1 || a[i - 1][j] == '#'){
                nxt[1][i][j] = make_pair(i, j);
            }
            else{
                nxt[1][i][j] = nxt[1][i - 1][j];
            }
        }
    }
    for(int i = n; i >= 1; i--){
        for(int j = m; j >= 1; j--){
            if(j == m || a[i][j + 1] == '#'){
                nxt[2][i][j] = make_pair(i, j);
            }
            else{
                nxt[2][i][j] = nxt[2][i][j + 1];
            }
            if(i == n || a[i + 1][j] == '#'){
                nxt[3][i][j] = make_pair(i, j);
            }
            else{
                nxt[3][i][j] = nxt[3][i + 1][j];
            }
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            d[i][j] = INF;
            if(a[i][j] == 'S'){
                sx = i;
                sy = j;
            }
            else if(a[i][j] == 'C'){
                tx = i;
                ty = j;
            }
        }
    }
    priority_queue < pair < int, pair < int, int > > > q;
    d[sx][sy] = 0;
    q.push(make_pair(0, make_pair(sx, sy)));
    while(!q.empty()){
        int val = -q.top().first,
            x = q.top().second.first,
            y = q.top().second.second;
        q.pop();
        if(d[x][y] < val){
            continue;
        }
        for(int i = 0; i < 4; i++){
            int xx = x + dx[i],
                yy = y + dy[i];
            if(!is_wall(xx, yy) && d[xx][yy] > d[x][y] + 1){
                d[xx][yy] = d[x][y] + 1;
                q.push(make_pair(-d[xx][yy], make_pair(xx, yy)));
            }
        }
        for(int i = 0; i < 4; i++){
            int xx = nxt[i][x][y].first,
                yy = nxt[i][x][y].second;
            if(d[xx][yy] > d[x][y] + dist_to_wall[x][y]){
                d[xx][yy] = d[x][y] + dist_to_wall[x][y];
                q.push(make_pair(-d[xx][yy], make_pair(xx, yy)));
            }
        }
    }
    if(d[tx][ty] == INF){
        return cout << "nemoguce", 0;
    }
    cout << d[tx][ty];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 768 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 2 ms 1664 KB Output is correct
10 Correct 2 ms 1664 KB Output is correct
11 Correct 2 ms 1664 KB Output is correct
12 Correct 2 ms 1664 KB Output is correct
13 Correct 2 ms 1664 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 2 ms 1664 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 640 KB Output is correct
5 Correct 12 ms 6712 KB Output is correct
6 Correct 13 ms 6704 KB Output is correct
7 Correct 14 ms 6688 KB Output is correct
8 Correct 9 ms 6812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 2 ms 1664 KB Output is correct
10 Correct 2 ms 1664 KB Output is correct
11 Correct 2 ms 1664 KB Output is correct
12 Correct 2 ms 1664 KB Output is correct
13 Correct 2 ms 1664 KB Output is correct
14 Correct 12 ms 6712 KB Output is correct
15 Correct 13 ms 6704 KB Output is correct
16 Correct 12 ms 6688 KB Output is correct
17 Correct 12 ms 6744 KB Output is correct
18 Correct 13 ms 6744 KB Output is correct
19 Correct 14 ms 6656 KB Output is correct
20 Correct 13 ms 6656 KB Output is correct
21 Correct 10 ms 6712 KB Output is correct
22 Correct 11 ms 6708 KB Output is correct
23 Correct 12 ms 6708 KB Output is correct
24 Correct 12 ms 6656 KB Output is correct
25 Correct 1 ms 640 KB Output is correct
26 Correct 2 ms 1664 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 7 ms 6812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 1 ms 768 KB Output is correct
9 Correct 2 ms 1664 KB Output is correct
10 Correct 2 ms 1664 KB Output is correct
11 Correct 2 ms 1664 KB Output is correct
12 Correct 2 ms 1664 KB Output is correct
13 Correct 2 ms 1664 KB Output is correct
14 Correct 10 ms 6712 KB Output is correct
15 Correct 12 ms 6832 KB Output is correct
16 Correct 12 ms 6688 KB Output is correct
17 Correct 11 ms 6744 KB Output is correct
18 Correct 13 ms 6872 KB Output is correct
19 Correct 16 ms 6656 KB Output is correct
20 Correct 15 ms 6656 KB Output is correct
21 Correct 10 ms 6712 KB Output is correct
22 Correct 11 ms 6708 KB Output is correct
23 Correct 14 ms 6868 KB Output is correct
24 Correct 250 ms 41976 KB Output is correct
25 Correct 449 ms 42108 KB Output is correct
26 Correct 349 ms 41932 KB Output is correct
27 Correct 318 ms 41848 KB Output is correct
28 Correct 187 ms 41828 KB Output is correct
29 Correct 209 ms 41752 KB Output is correct
30 Correct 252 ms 41820 KB Output is correct
31 Correct 12 ms 6656 KB Output is correct
32 Correct 314 ms 41792 KB Output is correct
33 Correct 1 ms 512 KB Output is correct
34 Correct 2 ms 1664 KB Output is correct
35 Correct 256 ms 42036 KB Output is correct
36 Correct 1 ms 512 KB Output is correct
37 Correct 7 ms 6812 KB Output is correct
38 Correct 116 ms 41820 KB Output is correct
39 Correct 180 ms 41768 KB Output is correct