๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/13460)
<๊ตฌ์ฌ ํ์ถ 2>
๋ฌธ์ ํ์ด
bfs๋ฅผ ํ์ฉํ์ฌ ๊ตฌ์ฌ์ ์ํ์ข์ฐ๋ก ์์ง์ธ๋ค. ์ฌ๊ธฐ์ ์๊ฐํด์ผ ํ๋ ๋ถ๋ถ์ ํ๋ ๊ตฌ์ฌ๊ณผ ๋นจ๊ฐ ๊ตฌ์ฌ์ ์์น๋ฅผ ๊ณ ๋ คํด์ผ ํ๋ค! ์ํ์ข์ฐ๋ก ์์ง์ผ ๋ ์ด๋ ๊ตฌ์ฌ์ด ์์ ์๋์ง๋ฅผ ํ์ธํ๋ฉฐ ๊ตฌ์ฌ์ ์์ง์ฌ์ค๋ค. ๋ํ, ํ๋ ๊ตฌ์ฌ์ด ๊ตฌ๋ฉ์ ๋น ์ง ๊ฒฝ์ฐ๋ ๊ณ ๋ คํด์ฃผ์ด์ผ ํ๋ค.
- my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class _13460_ {
static int dx[] = { -1, 1, 0, 0 };
static int dy[] = { 0, 0, -1, 1 };
static Queue<int[]> R, B;
static char map[][];
static int result = -1, x1, x2, y1, y2;
static boolean flag, choice;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
map = new char[n][m];
R = new LinkedList<>();
B = new LinkedList<>();
for (int i = 0; i < n; i++) { // input
String s = bf.readLine();
for (int j = 0; j < m; j++) {
map[i][j] = s.charAt(j);
if (map[i][j] == 'R') {
R.add(new int[] { i, j, 0 });
} else if (map[i][j] == 'B') {
B.add(new int[] { i, j, 0 });
}
}
}
search();
System.out.println(result);
}
private static void R_move(int temp, int dx, int dy) {
while (true) {
x1 += dx;
y1 += dy;
if (map[x1][y1] == '#' || (choice && x2 == x1+dx && y2 == y1+dy && map[x2][y2] != 'O')) {
if(choice) {
R.add(new int[] { x1 - dx, y1 - dy, temp });
B.add(new int[] { x2 - dx, y2 - dy, temp });
}
break;
} else if (map[x1][y1] == 'O') {
result = temp;
if(choice) flag=true;
break;
}
}
}
private static void B_move(int temp, int dx, int dy) {
while (true) {
x2 += dx;
y2 += dy;
if (map[x2][y2] == '#' || (!choice && x1 == x2 + dx && y1 == y2 + dy && map[x1][y1] != 'O')) {
if (!choice) {
if (result != -1)
break;
R.add(new int[] { x1 - dx, y1 - dy, temp });
B.add(new int[] { x2 - dx, y2 - dy, temp });
break;
}else if(map[x1][y1]=='O') {
flag=true;
break;
}else {
break;
}
} else if (map[x2][y2] == 'O') {
if(choice) flag=true;
result = -1;
break;
}
}
}
private static void search() {
while (!R.isEmpty()) {
int r_temp[] = R.poll();
int b_temp[] = B.poll();
int temp = 0;
flag = false;
for (int i = 0; i < 4; i++) {
x1 = r_temp[0];
y1 = r_temp[1];
x2 = b_temp[0];
y2 = b_temp[1];
choice=false;
temp = r_temp[2];
temp += 1;
if(temp>10) break;
if (i == 0) { // up
if (r_temp[1] == b_temp[1] && r_temp[0] > b_temp[0]) { // ํ๋ ๊ตฌ์ฌ์ด ๋ ์์ ์๋ค๋ฉด
choice=true;
B_move(temp, dx[i], dy[i]);
if(flag)continue;
R_move(temp, dx[i], dy[i]);
} else {
R_move(temp, dx[i], dy[i]);
B_move(temp, dx[i], dy[i]);
}
} else if (i == 1) { // down
if (r_temp[1] == b_temp[1] && r_temp[0] < b_temp[0]) {
choice=true;
B_move(temp, dx[i], dy[i]);
if(flag)continue;
R_move(temp, dx[i], dy[i]);
} else {
R_move(temp, dx[i], dy[i]);
B_move(temp, dx[i], dy[i]);
}
} else if (i == 2) { // left
if (r_temp[0] == b_temp[0] && r_temp[1] > b_temp[1]) {
choice=true;
B_move(temp, dx[i], dy[i]);
if(flag)continue;
R_move(temp, dx[i], dy[i]);
} else {
R_move(temp, dx[i], dy[i]);
B_move(temp, dx[i], dy[i]);
}
} else { // right
if (r_temp[0] == b_temp[0] && r_temp[1] < b_temp[1]) {
choice=true;
B_move(temp, dx[i], dy[i]);
if(flag)continue;
R_move(temp, dx[i], dy[i]);
} else {
R_move(temp, dx[i], dy[i]);
B_move(temp, dx[i], dy[i]);
}
}
if(result!=-1)break;
}
if(result!=-1)break;
}
}
}
- Main
- ๋ณด๋(map)์ ์ธ๋ก, ๊ฐ๋ก ํฌ๊ธฐ n, m ๋ฐ ๋ณด๋(map) ์ ๋ ฅ
- Queue R๊ณผ B๋ ๋นจ๊ฐ ๊ตฌ์ฌ๊ณผ ํ๋ ๊ตฌ์ฌ์ ์ ์ฅํ๋ค. [x์ขํ, y์ขํ, count]
- search() : bfs ํ์
- R_move(count, ์ํ์ข์ฐ ์ด๋ ์ค ํ๋)
- x์ขํ์ y์ขํ๋ฅผ ์ํ์ข์ฐ ์ค ํ๋๋ก ๋ฌดํ ์ด๋
- ์ด๋ํ ์ขํ๊ฐ ๊ตฌ์ฌ์ด ์ด๋ํ ์ ์๋ ์์น์ด์ง๋ง ๋นจ๊ฐ ๊ตฌ์ฌ์ด ๋จผ์ ์์ง์ธ ๊ฒ์ด๋ผ๋ฉด ๊ทธ๋๋ก ๋ฐ๋ณต๋ฌธ ํ์ถ
- ํ๋ ๊ตฌ์ฌ์ด ๋จผ์ ์์ง์์ผ๋ฉฐ, ๋นจ๊ฐ ๊ตฌ์ฌ์ด ๋ค์์ ์ด๋ํ ์์น์ ์ขํ๊ฐ ๊ฐ๋ค๋ฉด R, B์ ์ ์ฅ ๋ฐ ํ์ถ
- ๋ง์ฝ ๋นจ๊ฐ ๊ตฌ์ฌ์ด ๊ตฌ๋ฉ์ ๋ค์ด๊ฐ๋ค๋ฉด result์ count๋ฅผ ์ ์ฅ ๋ฐ ํ์ถ but ํ๋ ๊ตฌ์ฌ์ด ๋จผ์ ์์ง์๋ค๋ฉด flag๋ฅผ true๋ก ์ค์
- x์ขํ์ y์ขํ๋ฅผ ์ํ์ข์ฐ ์ค ํ๋๋ก ๋ฌดํ ์ด๋
- B_move(count, ์ํ์ข์ฐ ์ด๋ ์ค ํ๋)
- x์ขํ์ y์ขํ๋ฅผ ์ํ์ข์ฐ ์ค ํ๋๋ก ๋ฌดํ ์ด๋
- if
- ์ด๋ํ ์ขํ๊ฐ ๊ตฌ์ฌ์ด ์ด๋ํ ์ ์๋ ์์น์ด์ง๋ง ํ๋ ๊ตฌ์ฌ์ด ๋จผ์ ์์ง์ธ ๊ฒ์ด๋ผ๋ฉด ๊ทธ๋๋ก ๋ฐ๋ณต๋ฌธ ํ์ถ
- ๋นจ๊ฐ ๊ตฌ์ฌ์ด ๋จผ์ ์์ง์์ผ๋ฉฐ, ํ๋ ๊ตฌ์ฌ์ด ๋ค์์ ์ด๋ํ ์์น์ ์ขํ๊ฐ ๊ฐ๋ค๋ฉด R, B์ ์ ์ฅ ๋ฐ ํ์ถ
- ๋นจ๊ฐ ๊ตฌ์ฌ์ด ๋จผ์ ์ด๋ ํ ํ๋ ๊ตฌ์ฌ์ด ์ด๋ํ ์ขํ๊ฐ ๊ตฌ์ฌ์ด ์ด๋ํ ์ ์๋ ์์น์ด์ง๋ง ๋นจ๊ฐ ๊ตฌ์ฌ์ ์ด๋ฏธ ๊ตฌ๋ฉ์ ๋ค์ด๊ฐ๋ค๋ฉด flag๋ฅผ true๋ก ๋ฐ๊พผ ํ ํ์ถ
- ๊ทธ ์ธ์๋ ๊ทธ๋ฅ ํ์ถ
- else if
- ํ๋ ๊ตฌ์ฌ์ด ๊ตฌ๋ฉ์ ๋จ์ด์ ธ ๋ฒ๋ ธ๋๋ฐ ํ๋ ๊ตฌ์ฌ์ด ๋จผ์ ์์ง์ธ ๊ฑฐ๋ผ๋ฉด flag๋ฅผ true๋ก ๋ฐ๊ฟ. ํ๋ ๊ตฌ์ฌ์ ๊ฐ์ด ๋จ์ด์ง๋ฉด ์ ๋๋ฏ๋ก resut๋ฅผ -1๋ก ๋ฐ๊พธ๊ณ ํ์ถ
- if
- x์ขํ์ y์ขํ๋ฅผ ์ํ์ข์ฐ ์ค ํ๋๋ก ๋ฌดํ ์ด๋
- search()
- ๋นจ๊ฐ ๊ตฌ์ฌ๊ณผ ํ๋ ๊ตฌ์ฌ์ queue์์ ๋นผ๋ด ์ด
- ์ํ์ข์ฐ ๋ฐ๋ณต
- ๋จผ์ ๊ตฌ์ฌ์ ์์น์ count๋ฅผ ์ ์ฅํด์ค
- 10๋ฒ ์ดํ๋ก ์์ง์ฌ์ ๋นจ๊ฐ ๊ตฌ์ฌ์ ๋นผ๋ผ ์ ์๋ค๋ฉด ํ์ถ
- ๊ฐ๊ฐ ์ํ์ข์ฐ๋ก ์์ง์ผ ๋ ์ด๋ ๊ตฌ์ฌ์ด ๋จผ์ ์์ง์ฌ์ผ ํ๋์ง ํ๋จ ํ ๊ฐ๊ฐ ๊ตฌ์ฌ ์์ง์ด๋ ํจ์ ํธ์ถ
- choice: ํ๋ ๊ตฌ์ฌ์ ๋จผ์ ์์ง์ผ ๊ฒฝ์ฐ true๋ก ์ ์ฅ
- flag: ํ๋ ๊ตฌ์ฌ์ด ๊ตฌ๋ฉ์ ๋น ์ง ๊ฒฝ์ฐ true๋ก ์ ์ฅ
- ์ต์ ํ์๋ฅผ ์ถ๋ ฅํ๋ ๊ฒ์ด๋ฏ๋ก result๊ฐ -1์ด ์๋๋ฉด (์ด๊ธฐ๊ฐ์ด ์๋๋ฉด) ํ์ถ ํ ์ถ๋ ฅ
+ ์๊ฐ ๋จ์ถ ์ฝ๋
(์ค๋ช ์ https://melody-coding.tistory.com/267 ์ฐธ๊ณ )
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class _13460_ {
static char map[][];
static Queue<int[]> R, B;
static int result;
static int dx[] = { -1, 1, 0, 0 };
static int dy[] = { 0, 0, -1, 1 };
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
map = new char[n][m];
result = -1;
R = new LinkedList<>();
B = new LinkedList<>();
for (int i = 0; i < n; i++) {
String s = bf.readLine();
for (int j = 0; j < m; j++) {
map[i][j] = s.charAt(j);
if (map[i][j] == 'R')
R.add(new int[] { i, j, 0 });
else if (map[i][j] == 'B')
B.add(new int[] { i, j, 0 });
}
}
search();
System.out.println(result);
}
private static void search() {
boolean check = false;
while (!R.isEmpty()) {
int[] R_temp = R.poll();
int[] B_temp = B.poll();
int x1, y1, x2, y2;
for (int i = 0; i < 4; i++) {
int temp = R_temp[2] + 1;
if (temp > 10)
break; // 10๋ฒ ์ดํ๊ฐ ์๋๋ฉด x
x1 = R_temp[0];
y1 = R_temp[1];
check = false;
while (true) { // ๋นจ๊ฐ ๊ตฌ์ฌ
x1 += dx[i];
y1 += dy[i];
if (map[x1][y1] == '#') {
break;
}
if (map[x1][y1] == 'O') { // ๊ตฌ๋ฉ
check = true;
break;
}
}
x2 = B_temp[0];
y2 = B_temp[1];
while (true) { // ํ๋ ๊ตฌ์ฌ
x2 += dx[i];
y2 += dy[i];
if (map[x2][y2] == 'O') { // ํ๋๊ตฌ์ฌ์ด ๊ตฌ๋ฉ์ ๋น ์ง๋ฉด ์คํจ
result = -1;
break;
}
if (map[x2][y2] == '#') {
if (x1 == x2 && y1 == y2) { // ๋นจ๊ฐ ๊ตฌ์ฌ๊ณผ ํ๋ ๊ตฌ์ฌ ์ขํ๊ฐ ๊ฐ์ผ๋ฉด ์์น ์กฐ์
if (i == 0) {
if (B_temp[0] < R_temp[0]) {
x2 -= dx[i];
x1 = x2 - dx[i];
} else {
x1 -= dx[i];
x2 = x1 - dx[i];
}
} else if (i == 1) {
if (B_temp[0] < R_temp[0]) {
x1 -= dx[i];
x2 = x1 - dx[i];
} else {
x2 -= dx[i];
x1 = x2 - dx[i];
}
} else if (i == 2) {
if (B_temp[1] < R_temp[1]) {
y2 -= dy[i];
y1 = y2 - dy[i];
} else {
y1 -= dy[i];
y2 = y1 - dy[i];
}
} else {
if (B_temp[1] < R_temp[1]) {
y1 -= dy[i];
y2 = y1 - dy[i];
} else {
y2 -= dy[i];
y1 = y2 - dy[i];
}
}
} else {
x1 -= dx[i];
y1 -= dy[i];
x2 -= dx[i];
y2 -= dy[i];
if (check) { // ์ต์ ํ์
result = temp;
break;
}
}
if (!(R_temp[0] == x1 && R_temp[1] == y1 && B_temp[0] == x2 && B_temp[1] == y2)) { // ์๋ ์์น์ ๋์ผํ์ง ์์ผ๋ฉด
R.add(new int[] { x1, y1, temp });
B.add(new int[] { x2, y2, temp });
}
break;
}
}
if (result > 0)
break;
}
if (result > 0)
break;
}
}
}
์๊ฐ๐ค
์ด ๋ฌธ์ ... ์ฑ๊ณต ๋ชปํ ๋ปํ๋ค.. ํฌ๊ธฐํ ๋ปํ๋ค..!
์ฒ์์ ํ๋ ธ์ ๋๋ ํ๋ ๊ณต์ด ๋จผ์ ์์ง์ด๋ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ด๋ค. ๊ทธ๋์ ์ผ๋จ ํฌ๊ธฐํ๊ณ ๋ค์ ๋ ๋ค์ ๋์ ํ๋ค. ํ์ง๋ง ๊ณ์ํด์ ํ๋ ธ์ต๋๋ค๊ฐ ๋ด๊ณ ... ๋ฐ๋ก๋ฅผ ์ฐพ์.. ๋ ๋ฌ๋ค.. ์ ๋ ๋ฐ๋ก๋ฅผ ์ฐพ์๋ด์ง ๋ชปํ๋๊ฐ..
ํ๋ค๊ฒ ํ๋ค๊ฒ ์ฐพ์์.. ๊ฒจ์ฐ ํต๊ณผํ๋ค. ๊ฒจ์ฐ ํต๊ณผํ ๋งํผ ์๊ฐ์ ์์ฒญ๋ฌ๊ณ ... ์ผ๋จ ์ค๋์ ์ฌ๊ธฐ์ ๋ง์กฑ(?)ํด์ผ๊ฒ ๋ค.
+ 13459๋ฒ ๊ตฌ์ฌ ํ์ถ์ ํ๋ฉฐ ์๊ฐ์ ๋จ์ถํ ์ ์์๋ค.
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 15644_๊ตฌ์ฌ ํ์ถ 3 (0) | 2022.12.21 |
---|---|
[Baekjoon] 13459_๊ตฌ์ฌ ํ์ถ (0) | 2022.12.20 |
[Baekjoon] 4179_๋ถ! (0) | 2022.12.16 |
[Baekjoon] 16953_A โ B (0) | 2022.12.14 |
[Baekjoon] 10769_ํ๋ณตํ์ง ์ฌํ์ง (0) | 2022.12.13 |