|
| 1 | +```java |
| 2 | +import java.awt.Point; |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.LinkedList; |
| 7 | +import java.util.Queue; |
| 8 | +import java.util.Stack; |
| 9 | +import java.util.StringTokenizer; |
| 10 | + |
| 11 | + |
| 12 | +public class Main { |
| 13 | + |
| 14 | + static final int APPLE = -1; |
| 15 | + static final int EMPTY = 0; |
| 16 | + static final int SNAKE = 1; |
| 17 | + |
| 18 | + static Change[] change; |
| 19 | + static Queue<Point> snake; |
| 20 | + |
| 21 | + static int[][] arr; |
| 22 | + static int[] dy = {0,1,0,-1}; // 오른쪽->아래->왼쪽->위 |
| 23 | + static int[] dx = {1,0,-1,0}; |
| 24 | + static int size,appleCnt,changeCnt,direction,cur; |
| 25 | + |
| 26 | + static class Change{ |
| 27 | + int time; |
| 28 | + int direction; |
| 29 | + public Change(int time, int direction){ |
| 30 | + this.time = time; |
| 31 | + this.direction = direction; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + public static void main(String[] args) throws IOException { |
| 37 | + init(); |
| 38 | + process(); |
| 39 | + print(); |
| 40 | + } |
| 41 | + |
| 42 | + private static void init() throws IOException { |
| 43 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 44 | + snake = new LinkedList<>(); |
| 45 | + size = Integer.parseInt(br.readLine()); |
| 46 | + appleCnt = Integer.parseInt(br.readLine()); |
| 47 | + direction = 0; |
| 48 | + snake.add(new Point(0,0)); |
| 49 | + |
| 50 | + arr = new int[size][size]; |
| 51 | + |
| 52 | + for (int i = 0; i < appleCnt; i++) { |
| 53 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 54 | + int y = Integer.parseInt(st.nextToken())-1; |
| 55 | + int x = Integer.parseInt(st.nextToken())-1; |
| 56 | + arr[y][x] = APPLE; |
| 57 | + } |
| 58 | + |
| 59 | + changeCnt = Integer.parseInt(br.readLine()); |
| 60 | + change = new Change[changeCnt]; |
| 61 | + for (int i = 0; i < changeCnt; i++) { |
| 62 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 63 | + int time = Integer.parseInt(st.nextToken()); |
| 64 | + int direction = (st.nextToken().equals("L"))?-1:1; |
| 65 | + change[i] = new Change(time, direction); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static void process() throws IOException { |
| 70 | + Point head = snake.peek(); |
| 71 | + cur = 0; |
| 72 | + int changeIdx= 0; |
| 73 | + |
| 74 | + while (true) { |
| 75 | + cur++; |
| 76 | + int ny = head.y + dy[direction]; |
| 77 | + int nx = head.x + dx[direction]; |
| 78 | + |
| 79 | + if (ny < 0 || nx < 0 || ny >= size || nx >= size) break; |
| 80 | + if (arr[ny][nx] == SNAKE) break; |
| 81 | + |
| 82 | + Point nHead = new Point(nx, ny); |
| 83 | + |
| 84 | + snake.add(nHead); |
| 85 | + if (arr[ny][nx] == EMPTY) { |
| 86 | + Point tail = snake.poll(); |
| 87 | + arr[tail.y][tail.x] = EMPTY; |
| 88 | + } |
| 89 | + //사과를 만난경우 그냥 진행하면됨 |
| 90 | + |
| 91 | + arr[ny][nx] = SNAKE; |
| 92 | + |
| 93 | + head = nHead; |
| 94 | + |
| 95 | + if (changeIdx < changeCnt) { |
| 96 | + Change c = change[changeIdx]; |
| 97 | + if (cur < c.time) continue; |
| 98 | + |
| 99 | + direction = (direction + c.direction + 4)%4 ; |
| 100 | + changeIdx++; |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + private static void print() { |
| 108 | + System.out.println(cur); |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
0 commit comments