이 문제는 주어진대로 구현하면 된다.
0,0에서 시작한다.
사과를 먹으면 꼬리는 그대로이며 머리쪽이 길어진다. 즉 몸의 길이가 1 길어진다.
벽에 부딪히거나 뱀의 몸통에 부딪히면 게임이 끝난다.
언제 게임이 끝나는지 출력하면 된다.
나는 queue를 2개 사용하였다.
1개는 뱀이 이동할 수 없을때까지 이동하기 위한 queue를 만들었고
1개는 뱀이 1칸 이동했을 때 머리를 1칸 이동시키고 꼬리부분을 지우기 위한 queue를 만들었다.
초기값을 큐에다가 넣어주고 맵에다가 뱀을 7로 그린다.
Queue<Snake> queue = new LinkedList<>();
Queue<Snake> snakeBody = new LinkedList<>();
queue.add(new Snake(0, 0, 0));
matrix[0][0] = 7;
snakeBody.add(new Snake(0, 0, 0));
while문을 통해 이동할수있으면 계속 이동시킨다.
//오른쪽-0 아래-1 왼쪽-2 위-3
while(!queue.isEmpty()){
answer++;
Snake snake = queue.poll();
int snakeX = snake.x;
int snakeY = snake.y;
int snakeDirection = snake.currentDirection;
//snakeBody.add(snake);
if(snakeDirection == 0){
snakeY += 1;
} else if(snakeDirection == 1){
snakeX += 1;
} else if(snakeDirection == 2){
snakeY -= 1;
} else if(snakeDirection == 3){
snakeX -= 1;
}
//뱀이 벽이나 몸통에 부딪히면 queue를 빠져나온다.
if(snakeX < 0 || snakeY < 0 || snakeX >= matrix.length || snakeY >= matrix.length || matrix[snakeX][snakeY] == 7){
break;
}
맵에 사과가 있는지 확인하고 있으면 현재 위치를 뱀 몸통 queue에 저장한다.
없으면 뱀의 꼬리부분을 0으로 초기화하고 현재 위치를 뱀 몸통 queue에 저장한다.
//사과 있는지 확인
if(matrix[snakeX][snakeY] == 1){
matrix[snakeX][snakeY] = 7;
snakeBody.add(new Snake(snakeX, snakeY, snake.currentDirection));
} else{
Snake lastBody = snakeBody.poll();
matrix[lastBody.x][lastBody.y] = 0;
snakeBody.add(new Snake(snakeX,snakeY, snake.currentDirection));
matrix[snakeX][snakeY] = 7;
}
방향을 체크해서 D이면 시계방향, L이면 반시계방향으로 queue에 저장한다.
//방향 전환
if(direIdx != direList.size() &&direList.get(direIdx).second == answer){
String rotate = direList.get(direIdx).direction;
direIdx++;
//시계방향
if(rotate.equals("D")){
snakeDirection = (snakeDirection + 1) >= 4 ? 0 : snakeDirection+1;
} else{
snakeDirection = (snakeDirection - 1) < 0 ? 3 : snakeDirection-1;
}
}
queue.add(new Snake(snakeX, snakeY, snakeDirection));
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
class Dire{
int second;
String direction;
public Dire(int second, String direction) {
this.second = second;
this.direction = direction;
}
}
class Snake{
int x;
int y;
int currentDirection;
public Snake(int x, int y, int currentDirection) {
this.x = x;
this.y = y;
this.currentDirection = currentDirection;
}
}
public class Main {
public static int[][] matrix = null;
public static int answer = 0;
public static int direIdx = 0;
public static int changeTime = 0;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
matrix = new int[n][n];
int appleLen = Integer.parseInt(br.readLine());
for(int i=0;i<appleLen;i++){
String[] arr = br.readLine().split(" ");
int appleX = Integer.parseInt(arr[0]);
int appleY = Integer.parseInt(arr[1]);
matrix[appleX-1][appleY-1] = 1;
}
int direLen = Integer.parseInt(br.readLine());
List<Dire> direList = new ArrayList<>();
for(int i=0;i<direLen;i++){
String[] arr = br.readLine().split(" ");
int second = Integer.parseInt(arr[0]);
String direction = arr[1];
direList.add(new Dire(second, direction));
}
changeTime = direList.get(direIdx).second;
Queue<Snake> queue = new LinkedList<>();
Queue<Snake> snakeBody = new LinkedList<>();
queue.add(new Snake(0, 0, 0));
matrix[0][0] = 7;
snakeBody.add(new Snake(0, 0, 0));
//오른쪽-0 아래-1 왼쪽-2 위-3
while(!queue.isEmpty()){
answer++;
Snake snake = queue.poll();
int snakeX = snake.x;
int snakeY = snake.y;
int snakeDirection = snake.currentDirection;
if(snakeDirection == 0){
snakeY += 1;
} else if(snakeDirection == 1){
snakeX += 1;
} else if(snakeDirection == 2){
snakeY -= 1;
} else if(snakeDirection == 3){
snakeX -= 1;
}
if(snakeX < 0 || snakeY < 0 || snakeX >= matrix.length || snakeY >= matrix.length || matrix[snakeX][snakeY] == 7){
break;
}
//사과 있는지 확인
if(matrix[snakeX][snakeY] == 1){
matrix[snakeX][snakeY] = 7;
snakeBody.add(new Snake(snakeX, snakeY, snake.currentDirection));
} else{
Snake lastBody = snakeBody.poll();
matrix[lastBody.x][lastBody.y] = 0;
snakeBody.add(new Snake(snakeX,snakeY, snake.currentDirection));
matrix[snakeX][snakeY] = 7;
}
//방향 전환
if(direIdx != direList.size() &&direList.get(direIdx).second == answer){
changeTime = direList.get(direIdx).second;
String rotate = direList.get(direIdx).direction;
direIdx++;
//시계방향
if(rotate.equals("D")){
snakeDirection = (snakeDirection + 1) >= 4 ? 0 : snakeDirection+1;
} else{
snakeDirection = (snakeDirection - 1) < 0 ? 3 : snakeDirection-1;
}
}
queue.add(new Snake(snakeX, snakeY, snakeDirection));
}
System.out.println(answer);
}
}