Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 29 | 30 |
| 31 |
Tags
- 문자열
- 시뮬레이션
- COS PRO
- 러닝일지
- 프로그래머스
- oracle
- DP
- 알고리즘
- 건강
- 문제풀이
- dfs
- priorityqueue
- 코테
- math
- 코딩테스트
- 백준
- greedy
- 자바
- BFS
- 운동기록
- SWEA
- 투포인터
- binary search
- Java
- 이분탐색
- BOJ
- MySQL
- 문제해결
- SQL
- db
Archives
- Today
- Total
슈콩
[BOJ] 백준 3190 뱀 본문
[문제]
https://www.acmicpc.net/problem/3190

[소스 코드]
import java.io.*;
import java.util.*;
public class Main {
static int[] dr = {0,1,0,-1};
static int[] dc = {1,0,-1,0};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int n = Integer.parseInt(br.readLine());
int[][] map = new int[n+1][n+1];
int k = Integer.parseInt(br.readLine());
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][c] = 1;
}
int l = Integer.parseInt(br.readLine());
HashMap<Integer,String> hm = new HashMap<>();
for(int i=0;i<l;i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
String c = st.nextToken();
hm.put(x, c);
}
boolean[][] visit = new boolean[n+1][n+1];
Deque<int[]> q = new LinkedList<>();
visit[1][1] = true;
q.offer(new int[] {1,1});
int time = 1;
int d = 0;
while(!q.isEmpty()) {
int[] head = q.peekLast();
int nr = head[0] + dr[d];
int nc = head[1] + dc[d];
if(nr<1 || nr>n || nc<1 || nc>n || visit[nr][nc]) break;
if(map[nr][nc]==1) {
map[nr][nc] = 0;
}
else {
int[] tail = q.poll();
visit[tail[0]][tail[1]] = false;
}
visit[nr][nc] = true;
q.offer(new int[] {nr,nc});
if(hm.containsKey(time)) {
String c = hm.get(time);
if(c.equals("L")) d = (d+3) % 4;
else d = (d+1) % 4;
}
time++;
}
System.out.println(time);
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 백준 28215 대피소 (0) | 2025.10.04 |
|---|---|
| [BOJ] 백준 13458 시험 감독 (0) | 2025.10.02 |
| [BOJ] 백준 9205 맥주 마시면서 걸어가기 (0) | 2025.09.30 |
| [BOJ] 백준 2573 빙산 (0) | 2025.09.30 |
| [BOJ] 백준 2468 안전 영역 (0) | 2025.09.30 |