답안 #625109

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
625109 2022-08-09T11:29:16 Z chonka 포탈들 (BOI14_portals) C++17
100 / 100
272 ms 31852 KB
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
#define fi first
#define se second
// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

const int MAXN = 1007 ;

int dx[ 4 ] = { 1 , -1 , 0 , 0 } ;
int dy[ 4 ] = { 0 , 0 , 1 , -1 } ;

int n , m ;
string a[ MAXN ] ;
int aux[ MAXN ][ MAXN ] ;
int dist[ MAXN ][ MAXN ] ;

int up[ MAXN ][ MAXN ] , down[ MAXN ][ MAXN ] ;
int l[ MAXN ][ MAXN ] , r[ MAXN ][ MAXN ] ;


void solve ( ) {
    cin >> n >> m ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> a[ i ] ;
        a[ i ] = '#' + a[ i ] ;
        a[ i ] += '#' ;
    }
    for ( int i = 0 ; i <= m + 1 ; ++ i ) {
        a[ 0 ] += '#' ;
        a[ n + 1 ] += '#' ;
    }
    queue < pair < int , int > > q ;
    for ( int i = 0 ; i <= n + 1 ; ++ i ) {
        for ( int j = 0 ; j <= m + 1 ; ++ j ) {
            if ( a[ i ][ j ] == '#' ) {
                q.push ( { i , j } ) ;
            }
        }
    }
    while ( q.empty ( ) == false ) {
        auto [ x , y ] = q.front ( ) ; q.pop ( ) ;
        for ( int i = 0 ; i < 4 ; ++ i ) {
            int nx = x + dx[ i ] , ny = y + dy[ i ] ;
            if ( nx < 0 || nx > n + 1 ) { continue ; }
            if ( ny < 0 || ny > m + 1 ) { continue ; }
            if ( a[ nx ][ ny ] == '#' ) { continue ; }
            if ( aux[ nx ][ ny ] == 0 ) {
                aux[ nx ][ ny ] = aux[ x ][ y ] + 1 ;
                q.push ( { nx , ny } ) ;
            }
        }
    }

    priority_queue < pair < int , pair < int , int > > > pq ;

    for ( int i = 0 ; i <= n + 1 ; ++ i ) {
        for ( int j = 0 ; j <= m + 1 ; ++ j ) {
            if ( a[ i ][ j ] == '#' ) {
                up[ i ][ j ] = i ;
                l[ i ][ j ] = j ;
            }
            else {
                up[ i ][ j ] = up[ i - 1 ][ j ] ;
                l[ i ][ j ] = l[ i ][ j - 1 ] ;
            }
        }
    }
    for ( int i = n + 1 ; i >= 0 ; -- i ) {
        for ( int j = m + 1 ; j >= 0 ; -- j ) {
            if ( a[ i ][ j ] == '#' ) {
                down[ i ][ j ] = i ;
                r[ i ][ j ] = j ;
            }
            else {
                down[ i ][ j ] = down[ i + 1 ][ j ] ;
                r[ i ][ j ] = r[ i ][ j + 1 ] ;
            }
        }
    }

    
    for ( int i = 0 ; i <= n + 1 ; ++ i ) {
        for ( int j = 0 ; j <= m + 1 ; ++ j ) {
            dist[ i ][ j ] = MAXN * MAXN ; 
            if ( a[ i ][ j ] == 'S' ) {
                dist[ i ][ j ] = 0 ;
                pq.push ( make_pair ( 0 , make_pair ( i , j ) ) ) ;
            }
        }
    }
    while ( pq.empty ( ) == false ) {
        auto hh = pq.top ( ) ;
        int c = -hh.first , x = hh.second.first , y = hh.second.second ;
        pq.pop ( ) ;
        if ( c != dist[ x ][ y ] ) { continue ; }
        for ( int i = 0 ; i < 4 ; ++ i ) {
            int nx = x + dx[ i ] , ny = y + dy[ i ] ;
            if ( nx < 0 || nx > n + 1 ) { continue ; }
            if ( ny < 0 || ny > m + 1 ) { continue ; }
            if ( a[ nx ][ ny ] == '#' ) { continue ; }
            if ( dist[ nx ][ ny ] > c + 1 ) {
                dist[ nx ][ ny ] = c + 1 ;
                pq.push ( make_pair ( -( c + 1 ) , make_pair ( nx , ny ) ) ) ;
            }
        }
        for ( auto nx : { up[ x ][ y ] + 1 , down[ x ][ y ] - 1 } ) {
            int len = c + aux[ x ][ y ] ;
            if ( dist[ nx ][ y ] > len ) {
                dist[ nx ][ y ] = len ;
                pq.push ( make_pair ( -len , make_pair ( nx , y ) ) ) ;
            }
        }
        for ( auto ny : { l[ x ][ y ] + 1 , r[ x ][ y ] - 1 } ) {
            int len = c + aux[ x ][ y ] ;
            if ( dist[ x ][ ny ] > len ) {
                dist[ x ][ ny ] = len ;
                pq.push ( make_pair ( -len , make_pair ( x , ny ) ) ) ;
            }
        }
    }
    for ( int i = 0 ; i <= n + 1 ; ++ i ) {
        for ( int j = 0 ; j <= m + 1 ; ++ j ) {
            if ( a[ i ][ j ] == 'C' ) {
                cout << dist[ i ][ j ] << "\n" ;
                return ;
            }
        }
    }    
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ; // cin >> t ; 
    while ( t -- ) { solve ( ) ; }
    return 0 ;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 596 KB Output is correct
3 Correct 0 ms 596 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 1620 KB Output is correct
10 Correct 1 ms 1620 KB Output is correct
11 Correct 1 ms 1620 KB Output is correct
12 Correct 1 ms 1620 KB Output is correct
13 Correct 2 ms 1620 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 1492 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 552 KB Output is correct
5 Correct 9 ms 5412 KB Output is correct
6 Correct 8 ms 5332 KB Output is correct
7 Correct 9 ms 5492 KB Output is correct
8 Correct 5 ms 5460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 0 ms 596 KB Output is correct
7 Correct 0 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 2 ms 1620 KB Output is correct
10 Correct 2 ms 1640 KB Output is correct
11 Correct 1 ms 1620 KB Output is correct
12 Correct 1 ms 1620 KB Output is correct
13 Correct 1 ms 1620 KB Output is correct
14 Correct 7 ms 5332 KB Output is correct
15 Correct 9 ms 5332 KB Output is correct
16 Correct 9 ms 5460 KB Output is correct
17 Correct 8 ms 5588 KB Output is correct
18 Correct 10 ms 5460 KB Output is correct
19 Correct 9 ms 5244 KB Output is correct
20 Correct 9 ms 5332 KB Output is correct
21 Correct 7 ms 5492 KB Output is correct
22 Correct 9 ms 5496 KB Output is correct
23 Correct 8 ms 5460 KB Output is correct
24 Correct 9 ms 5312 KB Output is correct
25 Correct 1 ms 488 KB Output is correct
26 Correct 1 ms 1620 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 4 ms 5408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 2 ms 1620 KB Output is correct
10 Correct 1 ms 1620 KB Output is correct
11 Correct 1 ms 1620 KB Output is correct
12 Correct 1 ms 1620 KB Output is correct
13 Correct 1 ms 1620 KB Output is correct
14 Correct 7 ms 5332 KB Output is correct
15 Correct 8 ms 5332 KB Output is correct
16 Correct 8 ms 5460 KB Output is correct
17 Correct 10 ms 5460 KB Output is correct
18 Correct 10 ms 5412 KB Output is correct
19 Correct 10 ms 5232 KB Output is correct
20 Correct 9 ms 5340 KB Output is correct
21 Correct 7 ms 5460 KB Output is correct
22 Correct 7 ms 5460 KB Output is correct
23 Correct 9 ms 5496 KB Output is correct
24 Correct 175 ms 30608 KB Output is correct
25 Correct 272 ms 27424 KB Output is correct
26 Correct 208 ms 27232 KB Output is correct
27 Correct 198 ms 27172 KB Output is correct
28 Correct 145 ms 30788 KB Output is correct
29 Correct 140 ms 31820 KB Output is correct
30 Correct 172 ms 31852 KB Output is correct
31 Correct 8 ms 5232 KB Output is correct
32 Correct 207 ms 27112 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 1620 KB Output is correct
35 Correct 146 ms 29124 KB Output is correct
36 Correct 0 ms 468 KB Output is correct
37 Correct 4 ms 5408 KB Output is correct
38 Correct 66 ms 30104 KB Output is correct
39 Correct 124 ms 31212 KB Output is correct