๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/16954)
< ์์ง์ด๋ ๋ฏธ๋ก ํ์ถ >
๋ฌธ์ ํ์ด
์ด ๋ฌธ์ ๋ ๋ฒฝ์ด 1์ด๋ง๋ค ์๋๋ก ํ ์นธ์ฉ ๋ด๋ ค๊ฐ๋ค๋ ๋ถ๋ถ์ด ์ค์ํ๋ค.
1์ด๋ง๋ค ์๋๋ก ํ ์นธ์ฉ ์์ง์ด๋ ๋ถ๋ถ์ ์ค์ ๋ก ๋ฐฐ์ด์ ์ ์ฅํ์ง ์๊ณ ์ธ๋ฑ์ค๋ก ์ ๊ทผํ์ฌ ์ด๋ํ ์์น๊ฐ ๋น์นธ์ธ์ง ๋ฒฝ์ธ์ง ํ์ธํ๋ค.
- ์์ธํ ํ์ด๋ ๋ค์์ ์๊ฐ์ด ๋ ๋ ์์ฑ
- my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class _16954_ { // ์์ง์ด๋ ๋ฏธ๋ก ํ์ถ
static boolean arr[][], visited[][], result;
static int dx[] = { 0, -1, 1, 0, 0, -1, -1, 1, 1 };
static int dy[] = { 0, 0, 0, -1, 1, -1, 1, -1, 1 };
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
arr = new boolean[8][8];
visited = new boolean[8][8];
result = false;
for (int i = 0; i < 8; i++) {
String str = bf.readLine();
for (int j = 0; j < 8; j++) {
if (str.charAt(j) == '#') {
arr[i][j] = true; // ๋ฒฝ
}
}
}
search(7, 0, 0);
if (result)
System.out.println(1);
else
System.out.println(0);
}
private static void search(int start_x, int start_y, int depth) {
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[] { start_x, start_y, depth });
while (!queue.isEmpty()) {
int temp[] = queue.poll();
for (int i = 0; i < 9; i++) {
int x = temp[0] + dx[i];
int y = temp[1] + dy[i];
if (x >= 0 && x < 8 && y >= 0 && y < 8 ) {
if(x-temp[2]>=0 && arr[x-temp[2]][y]) continue;
if(i!=0 && visited[x][y]) continue;
if (x - (temp[2]+1) >= 0) {
if (!arr[x - (temp[2]+1)][y]) {
if (x == 0 && y == 7) {
result = true;
break;
}
visited[x][y] = true;
queue.add(new int[] { x, y, temp[2] + 1 });
}
} else {
if (x == 0 && y == 7) {
result = true;
break;
}
visited[x][y] = true;
queue.add(new int[] { x, y, temp[2] + 1 });
}
}
}
if (result)
break;
}
}
}
- Main
- arr : ์ฒด์คํ ์ํ ์ ๋ ฅ
- visited : ๋ฐฉ๋ฌธ ์ฌ๋ถ
- search ํจ์๋ฅผ ํตํด ์ผ์ชฝ ์๋ซ์นธ๋ถํฐ ์์
- ๊ฒฐ๊ณผ ์ถ๋ ฅ
- search
- queue์ ์์ ์์น์์น์ ์ด๋ ํ์ ์ ์ฅ {x, y, depth}
- queue๊ฐ ๋น ๋๊น์ง ๋ฐ๋ณต -> ํ์ฌ ์์น, ์ํ์ข์ฐ, ๋๊ฐ์ ์ด๋ -> 1์ด๋ง๋ค ๋ฒฝ์ด ์ด๋ํ๋ ์์น๋ฅผ ๊ณ์ฐํด์ ์ด๋ํ๋ ค๋ ์์น๊ฐ ๋น์นธ์ด๋ผ๋ฉด ์ด๋ํ๊ธฐ
์๊ฐ๐ค
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 14217_๊ทธ๋ํ ํ์ (0) | 2023.05.26 |
---|---|
[Baekjoon] 1937_์์ฌ์์ด ํ๋ค (1) | 2023.05.25 |
[Baekjoon] 2665_๋ฏธ๋ก๋ง๋ค๊ธฐ (0) | 2023.05.23 |
[Baekjoon] 27211_๋๋ ํ์ฑ (0) | 2023.05.21 |
[Baekjoon] 1261_์๊ณ ์คํ (1) | 2023.05.12 |