Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 3190. 뱀 본문

acmicpc

[BAEKJOON] 3190. 뱀

코드와이 2021. 5. 18. 19:42

 

문제링크

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

 

3190번: 뱀

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

www.acmicpc.net

 

package acmicpc.Gold5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;

public class 뱀 {
	static class Point{
		int r;
		int c;
		
		@Override
		public String toString() {
			return "Point [r=" + r + ", c=" + c + "]";
		}
		
		public Point(int r, int c) {
			super();
			this.r = r;
			this.c = c;
		}
		
	}
	static class time{
		int t;
		char d;
		public time(int t, char d) {
			super();
			this.t = t;
			this.d = d;
		}
		@Override
		public String toString() {
			return "time [t=" + t + ", d=" + d + "]";
		}
	}
	static List<Point> list;
	// 우 하 좌 상
	static int[] dr = {0, 1, 0, -1};
	static int[] dc = {1, 0, -1, 0};
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		int n = Integer.parseInt(br.readLine());
		int k = Integer.parseInt(br.readLine());
		int[][] map = new int[n][n];
		for(int i = 0 ; i < k ; i++) {
			st = new StringTokenizer(br.readLine());
			int r = Integer.parseInt(st.nextToken());
			int c = Integer.parseInt(st.nextToken());
			map[r-1][c-1] = 1;
		}
		list = new ArrayList<>();
		list.add(new Point(0,0));

		Queue<time> queue = new LinkedList<>();
		int l = Integer.parseInt(br.readLine());
		for(int i = 0 ; i < l ; i++) {
			st = new StringTokenizer(br.readLine());
			int t = Integer.parseInt(st.nextToken());
			char d = st.nextToken().charAt(0);
			queue.offer(new time(t, d));
		}
		int ans = 0;
		int d = 0;

		time T = queue.poll();
		int t = T.t;
		char d2 = T.d;

		out : while(true) {
			int r = list.get(list.size()-1).r;
			int c = list.get(list.size()-1).c;
			
			ans++;
			r += dr[d];
			c += dc[d];
			if(r < 0 || c < 0 || r >= n || c >= n) {
				break out;
			}
			if(!check(r, c)) {
				break out;
			}

			list.add(new Point(r, c));
			if(map[r][c] == 1) {
				map[r][c] = 0;
			} else {
				list.remove(0);
			}
			
			if(ans == t) {
				if(d2 == 'D') d = (d + 1) % 4;
				else d = (d + 3) % 4;
				
				if(!queue.isEmpty()) {
					T = queue.poll();
					t = T.t;
					d2 = T.d;
				}
			}
		}
		
		System.out.println(ans);
		
	}
	
	public static boolean check(int r, int c) {
		for(int i = 0 ; i < list.size()-1 ; i++) {
			if(r == list.get(i).r && c == list.get(i).c) return false;
		}
		return true;
	}
}

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 14891. 톱니바퀴  (0) 2021.05.21
[BAEKJOON] 14499. 주사위 굴리기  (0) 2021.05.20
[BAEKJOON] 7453. 합이 0인 네 정수  (0) 2021.05.16
[BAEKJOON] 1525. 퍼즐  (0) 2021.05.13
[BAEKJOON] 1202. 보석 도둑  (0) 2021.05.13