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
- 문제해결
- 알고리즘
- 자바
- 이분탐색
- 문자열
- SQL
- COS PRO
- Java
- BOJ
- db
- priorityqueue
- 러닝일지
- 백준
- 시뮬레이션
- 문제풀이
- 코딩테스트
- BFS
- 프로그래머스
- math
- DP
- MySQL
- 건강
- 운동기록
- SWEA
- dfs
- 투포인터
- oracle
- greedy
- binary search
- 코테
Archives
- Today
- Total
슈콩
[BOJ] 백준 3190 뱀 본문
[문제]
https://www.acmicpc.net/problem/3190
[소스 코드]
import java.io.*;
import java.util.*;
public class Main {
static int n,dir=0;
static int[][] board;
static Map<Integer,String> moveInfo;
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 = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
board = new int[n][n];
st = new StringTokenizer(br.readLine());
int k = Integer.parseInt(st.nextToken());
for(int i=0;i<k;i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken()) - 1;
int b = Integer.parseInt(st.nextToken()) - 1;
board[a][b] = 1;
}
st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken());
moveInfo = new HashMap<>();
for(int i=0;i<l;i++) {
st = new StringTokenizer(br.readLine());
int time = Integer.parseInt(st.nextToken());
String direction = st.nextToken();
moveInfo.put(time, direction);
}
bfs();
}
private static void bfs() {
Queue<Integer> q = new LinkedList<>();
q.offer(0);
int r = 0;
int c = 0;
int time = 0;
while(true) {
time++;
int nr = r + dr[dir];
int nc = c + dc[dir];
if(nr<0 || nc<0 || nr>=n || nc>=n) break;
if(q.contains(nr*n+nc)) break;
if(board[nr][nc]==1) {
q.offer(nr*n+nc);
board[nr][nc] = 0;
}
else {
q.offer(nr*n+nc);
q.poll();
}
if(moveInfo.containsKey(time)) {
if(moveInfo.get(time).equals("D"))
dir = (dir+1) % 4;
else
dir = (dir+3) % 4;
}
r = nr;
c = nc;
}
System.out.println(time);
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 백준 5373 큐빙 (0) | 2025.08.18 |
|---|---|
| [BOJ] 백준 4991 로봇청소기 (2) | 2025.08.17 |
| [BOJ] 백준 2478 자물쇠 (2) | 2025.08.17 |
| [BOJ] 백준 23291 어항 정리 (2) | 2025.08.13 |
| [BOJ] 백준 23290 마법사 상어와 복제 (2) | 2025.08.12 |