Algorithm/백준
백준 [3190] 뱀 (JAVA)
youngjae-kim
2024. 7. 8. 11:57
728x90
반응형
문제
풀이
뱀의 위치를 저장할 큐를 선언 뱀이 움직일 때 마다 뱀 위치 정보를 갱신 해주어야 한다.
- 시간을 측정
- 뱀의 다음 위치가 범위를 벗어나는지, 자기 자신과 부딫히는지 검사 (더 이상 게임을 진행할 수 없음).
- 뱀의 다음 위치에 사과가 있을 때 칸의 사과를 먹어서 없애고, 꼬리는 움직이지 않음. 큐에 머리가 움직인 칸의 위치 정보를 넣어준다.
- 뱀의 다음 위치에 사과가 없을 때 큐에 머리가 움직인 칸의 위치 정보를 넣고, 꼬리 위치 정보를 큐에서 뺀다.
- 현재 진행중인 게임의 시간과 처음에 입력으로 받았던 게임 시간과 일치하면 방향을 바꿔준다.
- 더이상 게임을 진행할 수 없을 때 까지 진행한다.
정답코드
package com.baekjoon.samsung_sw_ps.p3190;
import java.io.*;
import java.util.*;
public class Main {
static int N, time, X, C;
static int[][] map;
static LinkedList<Node> q = new LinkedList<>();
static Map<Integer, String> exec = new HashMap<>();
static int[] dr = { 0, 1, 0, -1 };
static int[] dc = { 1, 0, -1, 0 };
static class Node {
int r, c;
Node(int r, int c) {
this.r = r;
this.c = c;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
map = new int[N][N];
int K = Integer.parseInt(br.readLine());
StringTokenizer st;
while (K-- > 0) {
st = new StringTokenizer(br.readLine());
int r = Integer.parseInt(st.nextToken()) - 1;
int c = Integer.parseInt(st.nextToken()) - 1;
map[r][c] = 1; // 사과 위치 마킹
}
int L = Integer.parseInt(br.readLine());
while (L-- > 0) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
String c = st.nextToken();
exec.put(x, c);
}
bfs();
System.out.println(time);
}
static void bfs() {
int cr = 0, cc = 0;
time = 0;
int d = 0;
q.add(new Node(cr, cc));
while (true) {
time++;
int nr = cr + dr[d];
int nc = cc + dc[d];
// 벽을 마주했을 때
if (nr < 0 || nc < 0 || nr >= N || nc >= N) {
return;
}
// 자기 자신과 부딫혔을 때
for (Node node : q) {
if (node.r == nr && node.c == nc) {
return;
}
}
if (map[nr][nc] == 1) {
map[nr][nc] = 0;
q.add(new Node(nr, nc));
} else {
q.add(new Node(nr, nc));
q.poll();
}
if (exec.containsKey(time)) {
if (exec.get(time).equals("D")) {
d += 1;
if (d == 4) {
d = 0;
}
} else {
d -= 1;
if (d == -1) {
d = 3;
}
}
}
cr = nr;
cc = nc;
}
}
}
728x90
반응형