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
- 운동기록
- BFS
- BOJ
- 백준
- SQL
- 시뮬레이션
- 건강
- 자바
- 코테
- Java
- DP
- 이분탐색
- 문자열
- 문제풀이
- 프로그래머스
- 러닝일지
- SWEA
- MySQL
- greedy
- db
- 문제해결
- 알고리즘
- math
- 코딩테스트
- COS PRO
- oracle
- 투포인터
- priorityqueue
- binary search
- dfs
Archives
- Today
- Total
슈콩
[BOJ] 백준 13460 구슬 탈출 2 본문
[문제]
https://www.acmicpc.net/problem/13460
[소스 코드]
import java.io.*;
import java.util.*;
public class Main {
static class Marble{
int rr,rc,br,bc,cnt;
Marble(int rr,int rc,int br,int bc,int cnt){
this.rr = rr;
this.rc = rc;
this.br = br;
this.bc = bc;
this.cnt = cnt;
}
}
static char[][] map;
static boolean[][][][] visit;
static int redR,redC,blueR,blueC;
static int[] dr = {-1,1,0,0};
static int[] dc = {0,0,-1,1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
map = new char[n][m];
for(int i=0;i<n;i++) {
String s = br.readLine();
for(int j=0;j<m;j++) {
map[i][j] = s.charAt(j);
if(map[i][j]=='R') {
redR = i;
redC = j;
}
if(map[i][j]=='B') {
blueR = i;
blueC = j;
}
}
}
visit = new boolean[n][m][n][m];
System.out.println(move());
}
private static int move() {
Queue<Marble> q = new LinkedList<>();
visit[redR][redC][blueR][blueC] = true;
q.offer(new Marble(redR,redC,blueR,blueC,1));
while(!q.isEmpty()) {
Marble curr = q.poll();
for(int d=0;d<4;d++) {
boolean goalR = false;
boolean goalB = false;
int newRr = curr.rr;
int newRc = curr.rc;
int newBr = curr.br;
int newBc = curr.bc;
int currCnt = curr.cnt;
while(map[newRr+dr[d]][newRc+dc[d]]!='#') {
newRr += dr[d];
newRc += dc[d];
if(map[newRr][newRc]=='O') {
goalR = true;
break;
}
}
while(map[newBr+dr[d]][newBc+dc[d]]!='#') {
newBr += dr[d];
newBc += dc[d];
if(map[newBr][newBc]=='O') {
goalB = true;
break;
}
}
if(goalB) continue;
if(goalR && !goalB) {
return currCnt;
}
if(newRr==newBr && newRc==newBc) {
if(d==0) {
if(curr.rr>curr.br) {
newRr -= dr[d];
}
else {
newBr -= dr[d];
}
}
else if(d==1) {
if(curr.rr>curr.br) {
newBr -= dr[d];
}
else {
newRr -= dr[d];
}
}
else if(d==2) {
if(curr.rc>curr.bc) {
newRc -= dc[d];
}
else {
newBc -= dc[d];
}
}
else {
if(curr.rc>curr.bc) {
newBc -= dc[d];
}
else {
newRc -= dc[d];
}
}
}
if(currCnt>10) {
return -1;
}
if(!visit[newRr][newRc][newBr][newBc]) {
visit[newRr][newRc][newBr][newBc] = true;
q.offer(new Marble(newRr,newRc,newBr,newBc,currCnt + 1));
}
}
}
return -1;
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 백준 16401 과자 나눠주기 (0) | 2025.09.18 |
|---|---|
| [BOJ] 백준 14921 용액 합성하기 (0) | 2025.09.18 |
| [BOJ] 백준 1477 휴게소 세우기 (0) | 2025.09.18 |
| [BOJ] 백준 1253 좋다 (0) | 2025.09.17 |
| [BOJ] 백준 13335 트럭 (0) | 2025.09.17 |