[BOJ] 3190 뱀

SSOYEONG·2022년 7월 5일
0

Problem Solving

목록 보기
52/60
post-thumbnail

🔗 Problem

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

👩‍💻 Code

package baekjoon;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

// 뱀

public class BJ3190 {
	
	static class Point {
		
		int x;
		int y;
		
		Point(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}
	
	static int n, k, l;
	static int sec = 0;
	static int cvtIdx = 0;
	static int direction = 0;	// 0: E, 1: S, 2: W, 3: N
	static Point head;
	static Point tail;
	static int[] dx = {0, 1, 0, -1};
	static int[] dy = {1, 0, -1, 0};
	static int[] cvtTime;
	static int[] cvtInfo;	// right: +1, left: +3
	static int[][] map;		// 0: empty, 1: apple, 2: snake
	static Queue<Point> queue = new LinkedList<>();
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(br.readLine());
		k = Integer.parseInt(br.readLine());
		map = new int[n+1][n+1];
		
		StringTokenizer st;
		for(int i = 0; i < k; i++) {
			st = new StringTokenizer(br.readLine());
			int x = Integer.parseInt(st.nextToken());
			int y = Integer.parseInt(st.nextToken());
			map[x][y] = 1;
		}
		
		l = Integer.parseInt(br.readLine());
		cvtTime = new int[l];
		cvtInfo = new int[l];
		
		for(int i = 0; i < l; i++) {
			st = new StringTokenizer(br.readLine());
			cvtTime[i] = Integer.parseInt(st.nextToken());
			
			if(st.nextToken().equals("D")) cvtInfo[i] = 1;
			else cvtInfo[i] = 3;
		}
		
		head = new Point(1, 1);
		tail = new Point(1, 1);
		map[1][1] = 2;
		queue.add(head);
		
		while(true) {
			
			sec++;
			int xx = head.x + dx[direction];
			int yy = head.y + dy[direction];
			
			if(xx < 1 || xx > n || yy < 1 || yy > n) break;
			if(map[xx][yy] == 2) break;
			
			head = new Point(xx, yy);
			queue.add(head);
			
			if(map[xx][yy] == 0) {
				map[tail.x][tail.y] = 0;
				queue.poll();
				Point peek = queue.peek();
				tail = new Point(peek.x, peek.y);
			}
			
			map[xx][yy] = 2;
			
			if(cvtIdx < l && sec == cvtTime[cvtIdx]) {
				direction = (direction + cvtInfo[cvtIdx]) % 4;
				cvtIdx++;
			}
		}
		
		System.out.println(sec);
		
	}
}

📌 Note

아이디어

  • Queue 사용하여 tail을 설정한다.
  • 방향변환 정보를 stack에 저장할까 했는데 배열을 사용하였다.
    cvtIdx 사용하여 체크하는 것이 깔끔하다고 생각했기 때문
profile
Übermensch

0개의 댓글