티스토리 뷰

반응형


#. Problem

* The copyright in this matter is in BOJ





#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Solve


빌딩에 갇힌 상법이를 살려주자..!


BFS 기본 문제이다.

여러 상자가 쌓인 토마토문제를 풀어보기 전에 풀어보면 좋을 것 같다.


상범이는 주변 or 다른 층으로 이동할 수 있다.

visit 상태 배열을 사용하지 않고 상범이가 이동했던 자리는 벽으로 수정해주면 

메모리를 절약할 수 있다.


#. Code


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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
 
public class BOJ6593 {
 
    static int L, R, C, res;
    static char building[][][];
    static int[] dr = { -101000}, dc = { 0-10100}, df = { 00001-1 };
    static Info start;
 
    public static void main(String[] args) throws IOException {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
 
        while (true) {
            st = new StringTokenizer(br.readLine());
            
            L = Integer.parseInt(st.nextToken()); // 빌딩의 층 수
            R = Integer.parseInt(st.nextToken()); // row
            C = Integer.parseInt(st.nextToken()); // column
            if (L + R + C == 0break;
 
            building = new char[L][R][C];
            for (int i = 0; i < L; i++) {
                for (int j = 0; j < R; j++) {
                    building[i][j] = br.readLine().toCharArray();
                    for (int k = 0; k < C; k++) {
                        // 시작점
                        if (building[i][j][k] == 'S') {
                            start = new Info(i, j, k);
                            building[i][j][k] = '#';
                        }
                    }
                }
                br.readLine();
            }
 
            res = process();
            if (res == -1System.out.println("Trapped!");
            else System.out.println("Escaped in " + res + " minute(s).");
        }
    }
 
    private static int process() {
 
        int time = 0;
        Queue<Info> q = new LinkedList<>();
        q.add(start);
 
        int ff = 0, rr = 0, cc = 0, size = 0;;
        while (!q.isEmpty()) {
 
            ++time;
            size = q.size();
            while(size-- > 0) {
                Info now = q.poll();
                // 주변 or 다른 층으로 이동
                for (int d = 0; d < 6; d++) {
                    ff = now.f + df[d];
                    rr = now.r + dr[d];
                    cc = now.c + dc[d];
                    
                    // 범위를 벗어날 경우
                    if (rr < 0 || cc < 0 || rr >= R || cc >= C || ff < 0 || ff >= L) continue;
                    // 이동할 수 없다면
                    if (building[ff][rr][cc] == '#'continue;
                    
                    // 출구에 도달
                    if (building[ff][rr][cc] == 'E'return time;
                    
                    building[ff][rr][cc] = '#';
                    q.add(new Info(ff, rr, cc));
                }
            }
        }
 
        return -1;
    }
 
    static class Info {
        int f, r, c;
 
        public Info(int f, int r, int c) {
            this.f = f;
            this.r = r;
            this.c = c;
        }
        
    }
}
 
cs





반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday