www.acmicpc.net/problem/3190

 

3190번: 뱀

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

www.acmicpc.net

입력으로 보드의 크기와 사과의 갯수, 좌표, 방향 회전 정보가 주어집니다.

여기서 좌표는 (1, 1) ~ (N, N)으로 주어지기 때문에 주어지는 크기에 맞게 접근을 해야합니다.

(그거때문에 런타임에러가 떴었습니다... 문제좀 제대로 읽어봐야하는데..)

 

저는 뱀의 위치를 덱으로 구현하여 덱의 맨 앞에는 뱀의 머리, 맨 뒤에는 뱀의 꼬리로 두었습니다.

그리고 뱀의 방향전환 정보가 최대 10000개라서 char배열을 이용하여 하였습니다.

 

뱀을 움직일 때는

1. 뱀이 움직인다. (움직인 곳으로 push)

2. 다음 칸이 사과면 꼬리가 위치한 칸을 그대로 둔다. (길이 +1)

3. 다음 칸이 사과가 아닌 빈칸이면 꼬리가 위치한 칸을 비운다. (길이 그대로)

4. 뱀을 움직이고 해당 시간에 회전명령이 있으면 회전한다.

5. 다음 칸이 뱀이 있는 위치이거나 맵 밖이면 게임이 끝난다. (T 출력)

위의 규칙대로 움직여주시면 됩니다.

 

 

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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static char ch[] = new char[10001];
    static int direct[][] = { { 01 }, { 10 }, { 0-1 }, { -10 } };
    static int list[][];
    static Deque<int[]> dq = new ArrayDeque<>();
    static int N, K, L;
 
    static void func() {
        int idx = 0;
        dq.addLast(new int[] { 11 });
        list[1][1= 1;
        for (int T = 1;; T++) {
            int x = dq.peekFirst()[0];
            int y = dq.peekFirst()[1];
 
            int nx = x + direct[idx][0];
            int ny = y + direct[idx][1];
            if (nx <= 0 || ny <= 0 || nx > N || ny > N) {
                System.out.println(T);
                return;
            }
 
            if (list[nx][ny] == 2) {
                dq.addFirst(new int[] { nx, ny });
            } else if (list[nx][ny] == 0) {
                dq.addFirst(new int[] { nx, ny });
                list[dq.peekLast()[0]][dq.peekLast()[1]] = 0;
                dq.removeLast();
            } else {
                System.out.println(T);
                return;
            }
 
            if (ch[T] == 'L')
                idx = (idx + 3) % 4;
            else if (ch[T] == 'D')
                idx = (idx + 1) % 4;
 
            list[nx][ny] = 1;
        }
    }
 
    static void input() throws Exception {
        int x, y;
        st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        list = new int[N + 1][N + 1];
 
        st = new StringTokenizer(br.readLine());
        K = Integer.parseInt(st.nextToken());
        while (K-- > 0) {
            st = new StringTokenizer(br.readLine());
            x = Integer.parseInt(st.nextToken());
            y = Integer.parseInt(st.nextToken());
 
            list[x][y] = 2;
        }
 
        st = new StringTokenizer(br.readLine());
        L = Integer.parseInt(st.nextToken());
        while (L-- > 0) {
            st = new StringTokenizer(br.readLine());
            x = Integer.parseInt(st.nextToken());
            char c = st.nextToken().charAt(0);
            ch[x] = c;
        }
    }
 
    public static void main(String[] args) throws Exception {
        input();
        func();
    }
}
cs

'algorithm > data-structure' 카테고리의 다른 글

boj 2493 탑  (0) 2021.02.04
boj 6416 트리인가?  (0) 2021.02.04
boj 2346 풍선 터뜨리기  (0) 2021.02.02
boj 5397 키로거  (0) 2021.02.02
boj 10866 덱  (0) 2021.02.02

+ Recent posts