3190 뱀 - 삼성 sw 역량 기출
1.뱀 회전 구현
델타 배열을 ( 오른쪽 0 아래 1 왼쪽 2 위 3 ) 으로 구현하여 회전하는 방향에 따라 현재 방향을 바꿔준다
왼쪽회전은 빼주고 오른쪽회전은 더해주고
2.입력
맵에 사과 좌표에 1을 찍어준다.
문제의 좌표가 1,1부터 시작하기 때문에 사과좌표에 -1씩 해줌
뱀의 회전 정보는 노드를 생성하여 큐에 저장하였다 : 시간오름차순으로 받았기 때문에 해당 시간에 방향을 바꿔주고 큐에서 제거
3.뱀의 이동
뱀의 좌표는 큐에 저장하였다. 
while에 들어가면서 회전시간이면 회전하시키고 time을 늘려줌 : 일단 시간이 흐르고 종료조건 발생
꼬리는 먼저 들어가 있고 머리는 나중에 들어감 : 이동하면서 꼬리의 좌표를 0으로 바꾸고 머리의 좌표를 2로 바꿈
큐에서 막 꺼냈으면 꼬리의 좌표가 있기 때문에 그것을 기준으로 이동할 수가 없음.
따라서 머리의 좌표를 찾기 위해 큐를 뱀의 크기-1만큼 회전하면 머리의 좌표가 나옴.
이 좌표를 가지고 이동을 결정.
뱀의 머리가 사과를 만나면 그냥 큐에 현재좌표를 추가
뱀의 머리가 사과를 만나지 않으면 큐에 현재좌표를 추가하고 큐하나 제거
4.종료조건
머리에 이동하는 방향의 좌표를 더했을 때 맵 끝이거나 2가 체크되어있으면 종료
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main뱀 {
	static class Node{
		int t;
		String d;
		public Node(int t, String d) {
			this.t= t;
			this.d = d;
		}
	}
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		map = new int[N][N];
		int K = Integer.parseInt(br.readLine());
		for(int i = 0; i < K; i++) {
			StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");
			map[Integer.parseInt(st1.nextToken())-1][Integer.parseInt(st1.nextToken())-1]=1;
		}
		int L = Integer.parseInt(br.readLine());
		Queue<Node> dir = new LinkedList<>();
		for(int i = 0; i < L; i++) {
			StringTokenizer st2 = new StringTokenizer(br.readLine(), " ");
			int t = Integer.parseInt(st2.nextToken());
			String d = st2.nextToken();
			dir.add(new Node(t, d));
		}
		Queue<int[]> snake = new LinkedList<>();
		snake.add(new int[] {0,0});
		int direction = 0;
		int time = 0;
		while(true) {
			if(!dir.isEmpty() && time == dir.peek().t) {
				direction = rotate(dir.peek().d, direction);
				dir.poll();
			}
			time++;
			for(int i = 1; i < snake.size(); i++) {
				snake.add(snake.poll());
			}
			int[] now = snake.poll();
			snake.add(now);
			int y = now[0] + dy[direction];
			int x = now[1] + dx[direction];
			if(y >= N || x >= N || y < 0 || x < 0)break;
			if(map[y][x] == 2) break;
			if(map[y][x] == 1) {
				snake.add(new int[] {y,x});
				map[y][x] = 2;
			}else if(map[y][x] == 0) {
				snake.add(new int[] {y,x});
				map[y][x] = 2;
				int[] s = snake.poll();
				map[s[0]][s[1]] = 0;
			}
		}
		System.out.println(time);
	}
	// 오른쪽 0 아래 1 왼쪽 2 위 3
	static int[] dy = {0, 1, 0, -1};
	static int[] dx = {1, 0, -1, 0};
	static int[][] map;
	static int rotate(String dir, int direction) {
		if( dir.equals("D")) {
			direction = (direction+1)%4;
		}else {
			if(direction == 0) {
				direction = 3;
			}else {
				direction--;
			}
		}
		return direction;
	}
}