백준 3190번: 뱀 :: 매운코딩
728x90
300x250

오른쪽 왼쪽 90도 회전은 북,동,남,서를 기준으로 두고

왼쪽 회전이면 인덱스--, 오른쪽 회전이면 인덱스++ 을 해줬다.

 

사과를 안먹었을 경우에는 방향대로 1칸 전진하고 꼬리를 삭제해주어야 하는데,

나는 큐를 썼지만 찾아보니 디큐가 적절한듯 하다. (큐는 가장 첨에 넣었던걸 삭제해주는 방식으로 했다.)

 

이동해야하는 시간과 방향을 관리해주는 큐도 있고,

벽에 부딪히는것은 범위를 벗어났을 경우로 처리해줬다.

 

import java.io.*;
import java.util.*;

public class Main {

	public static int N,K,L, dir=1, time=0; //dir 처음에는 오른쪽으로 가기때문에 방향을 1로 줌
	public static int row = 1,col=1;
	public static int[][] map;
	public static Queue<Integer[]> snake = new LinkedList<>();
	public static Queue<String[]> q = new LinkedList<>();
	public static StringTokenizer st;
	public static int[] dx = {-1,0,1,0};
	public static int[] dy = {0,1,0,-1};
	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];
		
		
		//사과 배치하기
		for (int i = 0; i < K; i++) {
			String str = br.readLine();
			st = new StringTokenizer(str);
			int r = Integer.parseInt(st.nextToken());
			int c = Integer.parseInt(st.nextToken());
			map[r][c] = 2; //사과는 2;
		}
		
		//뱀의 방향 전환 횟수
		L = Integer.parseInt(br.readLine());
		for (int i = 0; i < L; i++) {
			String str = br.readLine();
			st = new StringTokenizer(str);
			String transTime = st.nextToken();
			String transDir = st.nextToken();
			
			q.add(new String[]{transTime , transDir});
			
		}
		
		//뱀 시작
		dfs();
		System.out.println(time);

	}
	
	public static void dfs() {
		
		map[row][col]=1; //시작점의 뱀
		snake.add(new Integer[] {row,col});
		
		while(true) {

			time++;
			
			//이동할 위치 조정
			int nx = row + dx[dir];
			int ny = col + dy[dir];
			
			//범위 벗어나냐?
			if(nx < 1 || ny < 1 || nx >= N+1 || ny >= N+1)
				return;
			//뱀 자기자신의 몸에 부딪히냐?
			if(map[nx][ny]==1)
				return;

			
			snake.add(new Integer[] {nx,ny});
			//사과가 없으면 꼬리 위치한 칸 비워주기
			if(map[nx][ny]!=2) {
				map[snake.peek()[0]][snake.peek()[1]]=0;
				snake.poll();
			}

			map[nx][ny]=1;
			row = nx;
			col = ny;
			
			//이동해야하는 시간인가?
			String[] sArr = q.peek();
			
			if(!q.isEmpty() && time==Integer.parseInt(sArr[0])) {
				q.poll(); //제거
				
				if(sArr[1].equals("L")) //왼쪽90도
					dir = (dir-1 < 0 ? dx.length-1 : dir-1);
				else if(sArr[1].equals("D")) //오른쪽90도
					dir = (dir+1 >= dx.length ? 0 : dir+1);
				
			}
			
			//print();
		}
	}

	public static void print() {
		System.out.println("================");
		for (int i = 1; i < map.length; i++) {
			for (int j = 1; j < map.length; j++) {
				System.out.print(map[i][j]+" ");
			}
			System.out.println();
		}
	}
}
728x90

'알고리즘 > DFS & BFS' 카테고리의 다른 글

SWEA - 2814. 최장 경로  (1) 2020.04.04
백준 14501번: 퇴사  (0) 2020.03.21
백준 2667번: 단지번호붙이기  (1) 2020.03.02
백준 10026번: 적록색약  (0) 2020.03.02
백준 2468번: 안전 영역  (0) 2020.03.02

+ Recent posts