Algorithm 문제풀이/simulation

[BOJ] 백준 3190 - 뱀(JAVA)

cjsong 2019. 7. 23. 00:27

 

백준 제출할때 Class명을 Main으로 바꾼 후 제출했는데 계속 컴파일 오류 떠서 20분간 찾았다, 

밑에 deque 선언해줄때 Main3190_뱀.Info로 되어있어서 오류가 계속 났음. 시간날림~

Deque를 이용 

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
97
98
99
100
101
102
103
104
105
 
public class Main3190_뱀{
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine().trim());
        int[][] map = new int[N][N]; // 0 : 기본, 1 : 사과 , 2 : 뱀의 흔적
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        int K = Integer.parseInt(st.nextToken()); // 사과의 개수
        for (int i = 0; i < K; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            int r = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());
            map[r - 1][c - 1= 1;
        }
        
//        for (int i = 0; i < map.length; i++) {
//            for (int j = 0; j < map.length; j++) {
//                System.out.print(map[i][j]+ " ");
//            }
//            System.out.println();
//        }
 
        int L = Integer.parseInt(br.readLine().trim()); // 뱀의 방향 변환 횟수
        String[][] commandDir = new String[L][2];
        for (int i = 0; i < L; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            for (int j = 0; j < commandDir[i].length; j++) {
                commandDir[i][j] = st.nextToken();
            }
 
        }
        
//        for (int i = 0; i < commandDir.length; i++) {
//            for (int j = 0; j < commandDir[i].length; j++) {
//                System.out.print(commandDir[i][j]+ " ");
//            }
//            System.out.println();
//        }
 
        Deque<Info> snakeInfo = new ArrayDeque<Main3190_뱀.Info>();
        map[0][0= 2;
        snakeInfo.add(new Info(00));
 
        int[] dx = { -1010 }; // 북동남서: 시계방향으로 설정
        int[] dy = { 010-1 };
        int time = 0;
        int dir = 1// 처음에 오른쪽이니깐 동쪽방향
        int idx = 0// commandDir 처리해주기 위해서 변수 선언
 
        while (true) {
            if (idx < L && 
                    Integer.parseInt(commandDir[idx][0]) == time) {
                if (commandDir[idx][1].equals("D") ) {
                    dir = (dir == 3) ? 0 : dir+1;
                    idx++// commandDir의 다음인덱스로 계산해줘야됨
                } else {
                    dir = (dir == 0) ? 3 : dir-1;
                    idx++;
                }
            }
 
            int nx = snakeInfo.getFirst().x + dx[dir];
            int ny = snakeInfo.getFirst().y + dy[dir];
 
            if (nx < 0 || ny < 0 || nx >= N || ny >= N) { // 벽에 부딪힘
                break;
            }
 
            if (map[nx][ny] == 2) { // 자기 몸에 부딪힘
                break;
            }
 
            if (map[nx][ny] == 1) { // 사과가 있으면
                map[nx][ny] = 2// 뱀이 위치하고 있으니깐 2로 바꿔줌
                snakeInfo.addFirst(new Info(nx, ny));
            } else if (map[nx][ny] == 0) { // 사과가 없으면 머리 위치 이동해주고 꼬리 삭제 후 뱀이 위치하고 있던 표시 0값으로 변환
                map[nx][ny] = 2;
                snakeInfo.addFirst(new Info(nx, ny));
 
                Info info = snakeInfo.removeLast();
                map[info.x][info.y] = 0;
            }
            time++;
 
        }
        System.out.println(time + 1);
    }// end of main
 
    public static class Info {
        int x;
        int y;
 
        public Info(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }
 
    }
}// end of class
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs