알고리즘

백준 3190번 뱀 (JAVA)

박카스마시며코딩 2023. 11. 23. 21:22

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

 

3190번: 뱀

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

www.acmicpc.net

 

저는 구현을 통해 문제를 해결하였습니다.

dir을 통해 현재 방향을 표시하고, 뱀의 머리쪽을 먼저 늘리고 사과가 없다면 deque에서 마지막 것을 빼 꼬리를 줄였습니다. 이렇게 계속 진행하여 게임이 언제 끝나는지를 판단하여 문제를 해결하였습니다.

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Test {
    private static final char LEFT = 'L';
    private static final char RIGHT = 'D';
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int appleCnt = Integer.parseInt(br.readLine());
        StringTokenizer st;
        boolean[][] isApple = new boolean[n][n];
        for(int i = 0 ; i < appleCnt ; i++){
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken())-1;
            int b = Integer.parseInt(st.nextToken())-1;
            isApple[a][b] = true;
        }
        int dirCnt = Integer.parseInt(br.readLine());
        int[] time = new int[dirCnt];
        char[] nextDir = new char[dirCnt];
        for(int i = 0 ; i < dirCnt ; i++){
            st = new StringTokenizer(br.readLine());
            time[i] = Integer.parseInt(st.nextToken());
            nextDir[i] = st.nextToken().charAt(0);
        }
        int result = cal(isApple,n,time,nextDir,dirCnt);
        System.out.println(result);
    }
    private static final int[] DY = {-1,0,1,0};
    private static final int[] DX = {0,1,0,-1};
    private static int cal(boolean[][] isApple, int n, int[] time, char[] nextDir, int dirCnt) {
        int y = 0;
        int x = 0;
        int dir = 1;
        Deque<int[]> route = new LinkedList<>();
        route.offerFirst(new int[]{y,x});
        boolean[][] snake = new boolean[n][n];
        snake[y][x] = true;
        int result = 0;
        int timeIndex = 0;
        while(true){
//            System.out.println(result+" "+y+" "+x+" "+" "+dir);
            // 머리를 늘린다.
            result++;
            y += DY[dir];
            x += DX[dir];
            if(y < 0 || y >= n || x < 0 || x >= n || snake[y][x]){ // 자신 또는 벽에 부딪혔다면 게임을 끝낸다.
                break;
            }
            snake[y][x] = true;
            route.offerFirst(new int[]{y,x});
            if(!isApple[y][x]){ // 사과가 없다면 꼬리 한칸 줄인다.
                int[] position = route.pollLast();
                snake[position[0]][position[1]] = false;
            }
            if(isApple[y][x]){
                isApple[y][x] = false;
            }
            if(timeIndex < dirCnt && time[timeIndex] == result){
                char ch = nextDir[timeIndex++];
                if(ch == LEFT){
                    dir += 3;
                    dir %= 4;
                }
                if(ch == RIGHT){
                    dir += 1;
                    dir %= 4;
                }
            }
        }
        return result;
    }
}

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

백준 12100번 2048 (JAVA)  (1) 2023.11.25
백준 14503번 로봇 청소기 (JAVA)  (0) 2023.11.24
백준 2512번 예산 (JAVA)  (0) 2023.11.22
백준 2003번 수들의 합2 (JAVA)  (1) 2023.11.21
백준 11659번 구간 합 구하기 4 (JAVA)  (1) 2023.11.20