ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 상범 빌딩
    백준알고리즘 2019. 9. 17. 22:25

    3차원 토마토가 떠오르는 문제였다

    기본적인 BFS구조를쓰면되고, 3차원이다보니 pair를 사용할수없는점을 유의해야한다.

    3개일때 묶는것도 따로있기는 한데, 어떤건지 모르겠어서 그냥 구조체에서 생성자써서 BFS를 진행했다.

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    #include<iostream>
    #include<queue>
    #include<cstring>
     
    using namespace std;
    struct pt {
        int l = 0int r = 0int c = 0;
        pt(int l, int r, int c) {
            this->= l;
            this->= r;
            this->= c;
        }
    };
    int L, R, C;
    char map[30][30][30];
    int d[30][30][30];
    int dx[] = { 0,0,1,-1,0,0 };
    int dy[] = { 1,-1,0,0,0,0 };
    int dz[] = { 0,0,0,0,1,-1 };
    pt start = pt(000);
    pt ed = pt(000);
    int bfs() {
        queue<pt> q;
        q.push(start);
        d[start.l][start.r][start.c] = 0;
        while (!q.empty()) {
            int l = q.front().l; int r = q.front().r; int c = q.front().c;
            q.pop();
            for (int i = 0; i < 6; i++) {
                int nl = l + dz[i]; int nr = r + dx[i]; int nc = c + dy[i];
                if (nl >= 0 && nl < L && nr >= 0 && nr < R && nc >= 0 && nc < C) {
                    if (map[nl][nr][nc] == '.' || map[nl][nr][nc] == 'E') {
                        if (d[nl][nr][nc] == 0) {
                            d[nl][nr][nc] = d[l][r][c] + 1;
                            q.push(pt(nl, nr, nc));
                            if (map[nl][nr][nc] == 'E') {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
    int main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        while (true) {
            cin >> L >> R >> C;
            if (L == 0 && R == 0 && C == 0break;
            for (int i = 0; i < L; i++) {
                for (int j = 0; j < R; j++) {
                    for (int k = 0; k < C; k++) {
                        cin >> map[i][j][k];
                        if (map[i][j][k] == 'S')
                        {
                            start.l = i; start.r = j; start.c = k;
                        }
                        if (map[i][j][k] == 'E')
                        {
                            ed.l = i; ed.r = j; ed.c = k;
                        }
     
                    }
                }
            }
            if (bfs()) {
                cout <<"Escaped in "<< d[ed.l][ed.r][ed.c] <<" minute(s)."<< '\n';
            }
            else {
                cout << "Trapped!" << '\n';
            }
            memset(d, 0sizeof(d));
        }
        return 0;
     
    }
    http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
     

    '백준알고리즘' 카테고리의 다른 글

    사다리 조작  (0) 2019.09.21
    게리맨더링  (0) 2019.09.20
    역사  (0) 2019.09.17
    경로찾기  (0) 2019.09.16
    텀 프로젝트  (0) 2019.09.16

    댓글

Designed by Tistory.