🟦/백준

[골드 4] 뱀

진뚱이용 2023. 5. 28. 14:42

https://www.acmicpc.net/problem/3190

 

3190번: 뱀

'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임

www.acmicpc.net

import java.io.*;
import java.util.*;

class Point {
    int y;
    int x;

    Point(int y, int x) {
        this.y = y;
        this.x = x;
    }
}

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st;

        int N = Integer.parseInt(br.readLine());
        int K = Integer.parseInt(br.readLine());

        boolean[][] apple_exist = new boolean[N + 2][N + 2];
        for (int i = 0; i < K; i++) {
            st = new StringTokenizer(br.readLine());
            apple_exist[Integer.parseInt(st.nextToken())][Integer.parseInt(st.nextToken())] = true;
        }

        int L = Integer.parseInt(br.readLine());
        String[][] command = new String[L][2];
        for (int i = 0; i < L; i++) {
            st = new StringTokenizer(br.readLine());
            command[i][0] = st.nextToken();
            command[i][1] = st.nextToken();
        }

        boolean[][] map = new boolean[N + 2][N + 2];
        for (int i = 0; i < N + 2; i++) {
            map[0][i] = true;
            map[N + 1][i] = true;
            map[i][0] = true;
            map[i][N + 1] = true;
        }

        ///////////////////////////////////////////////////

        int time = 0;

        // First가 뱀의 머리, Last가 뱀의 꼬리
        Deque<Point> deque = new ArrayDeque<>();
        deque.addFirst(new Point(1, 1));
        map[1][1] = true;

        String direction = "RIGHT";
        int command_index = 0;

        while (true) {
            time++;

            // 다음 칸 좌표
            int new_head_y = deque.peekFirst().y;
            int new_head_x = deque.peekFirst().x;
            if (direction.equals("RIGHT"))
                new_head_x++;
            else if (direction.equals("LEFT"))
                new_head_x--;
            else if (direction.equals("DOWN"))
                new_head_y++;
            else
                new_head_y--;

            // 끝이거나 내 몸에 부딪히면
            if (map[new_head_y][new_head_x])
                break;

            // 다음 칸 방문 처리와 머리 추가
            map[new_head_y][new_head_x] = true;
            deque.addFirst(new Point(new_head_y, new_head_x));

            // 사과 존재하면 사과 없애기
            if (apple_exist[new_head_y][new_head_x]) {
                apple_exist[new_head_y][new_head_x] = false;
            }
            // 사과 없으면 꼬리 제거 후 꼬리 방문 제거
            else {
                Point tmp = deque.removeLast();
                map[tmp.y][tmp.x] = false;
            }

            // 방향 바꿔주기
            if (command_index < L && time == Integer.parseInt(command[command_index][0])) {
                if (direction.equals("RIGHT")) {
                    if (command[command_index][1].equals("L"))
                        direction = "UP";
                    else
                        direction = "DOWN";
                } else if (direction.equals("DOWN")) {
                    if (command[command_index][1].equals("L"))
                        direction = "RIGHT";
                    else
                        direction = "LEFT";
                } else if (direction.equals("LEFT")) {
                    if (command[command_index][1].equals("L"))
                        direction = "DOWN";
                    else
                        direction = "UP";
                } else {
                    if (command[command_index][1].equals("L"))
                        direction = "LEFT";
                    else
                        direction = "RIGHT";
                }
                command_index++;
            }
        }

        System.out.println(time);
    }
}