์๊ฐ์ ํ
1์ด
๋ฌธ์
๋๋น์ด๋ N X M ํฌ๊ธฐ์ ์ง์ฌ๊ฐํ ํํ์ ๋ฏธ๋ก์ ๊ฐํ ์๋ค.
๋ฏธ๋ก์๋ ์ฌ๋ฌ ๋ง๋ฆฌ์ ๊ดด๋ฌผ์ด ์์ด ์ด๋ฅผ ํผํด ํ์ถํด์ผ ํ๋ค.
๋๋น์ด์ ์์น๋ (1, 1)์ด๊ณ ๋ฏธ๋ก์ ์ถ๊ตฌ๋(N, M)์ ์์น์ ์กด์ฌํ๋ฉฐ ํ ๋ฒ์ ํ ์นธ์ฉ ์ด๋ํ ์ ์๋ค.
์ด๋ ๊ดด๋ฌผ์ด ์๋ ๋ถ๋ถ์ 0์ผ๋ก, ๊ดด๋ฌผ์ด ์๋ ๋ถ๋ถ์ 1๋ก ํ์๋์ด ์๋ค.
๋ฏธ๋ก๋ ๋ฐ๋์ ํ์ถํ ์ ์๋ ํํ๋ก ์ ์๋๋ค.
์ด๋ ๋๋น์ด๊ฐ ํ์ถํ๊ธฐ ์ํด ์์ง์ฌ์ผ ํ๋ ์ต์ ์นธ์ ๊ฐ์๋ฅผ ๊ตฌํ์์ค.
์นธ์ ์
๋๋ ์์ ์นธ๊ณผ ๋ง์ง๋ง ์นธ์ ๋ชจ๋ ํฌํจํด์ ๊ณ์ฐํ๋ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์ ๋ ์ฅ์ N, M (4 < N, M < 200)์ด ์ฃผ์ด์ง๋๋ค.
๋ค์ N๊ฐ์ ์ค์๋ ๊ฐ๊ฐ M๊ฐ์ ์ฅ์(0 ํน์ 1)๋ก ๋ฏธ๋ก์ ์ ๋ณด๊ฐ ์ฃผ์ด์ง๋ค.
๊ฐ๊ฐ์ ์๋ค์ ๊ณต๋ฐฑ ์์ด ๋ถ์ด์ ์
๋ ฅ์ผ๋ก ์ ์๋๋ค.
๋ํ ์์ ์นธ๊ณผ ๋ง์ง๋ง ์นธ์ ํญ์ 1์ด๋ค.
์ ๋ ฅ ์์
5 6
101010
111111
000001
111111
111111
์ถ๋ ฅ
์ฒซ์งธ ์ค์ ์ต์ ์ด๋ ์นธ์ ๊ฐ์๋ฅผ ์ถ๋ ฅํ๋ค.
์ถ๋ ฅ ์์
10
๋ฌธ์ ํ์ด ๊ณผ์
๋ฏธ๋ก ์ ๋ณด ์ ๋ ฅ๋ฐ๊ธฐ
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
maze = new int[N][M];
visited = new boolean[N][M];
for (int i = 0; i < N; i++) {
String[] split = br.readLine().split("");
for (int j = 0; j < M; j++) {
maze[i][j] = Integer.parseInt(split[j]);
}
}
N๊ณผ M์ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋์ด BufferedReader์ StringTokenizer๋ฅผ ํตํด ์ ๋ ฅ๋ฐ๊ณ
์ผ์ ํ์ ์ ๋ณด๋ ๊ณต๋ฐฑ ์์ด ๊ตฌ๋ถํด์ผ ํ๋ฏ๋ก split์ ํตํด ์ ๋ ฅ๋ฐ์๋ค.
์ขํ๋ฅผ ์ ์ฅํ Point ํด๋์ค ๊ตฌํ
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
bfsํจ์ ๊ตฌํํ๊ธฐ
BFS๋ ์์ ์ง์ ์์ ๊ฐ๊น์ด ๋ ธ๋๋ถํฐ ์ฐจ๋ก๋๋ก ๊ทธ๋ํ์ ๋ชจ๋ ๋ ธ๋๋ฅผ ํ์ํ๋ฏ๋ก BFS๋ฅผ ํตํด ํด๊ฒฐํ๋ฉด ์ฝ๋ค.
BFS์ ๋ํด ๊ฐ๋จํ๊ฒ ์ดํด๋ณด๊ณ ์ถ๋ค๋ฉด ์ฐธ๊ณ ํ๊ธธ ๋ฐ๋๋ค.
[์๊ณ ๋ฆฌ์ฆ] BFS (Java)
BFS๋? Breadth First Search, ๋๋น ์ฐ์ ํ์ ๊ฐ๊น์ด ๋ ธ๋๋ถํฐ ํ์ํ๋ ์๊ณ ๋ฆฌ์ฆ ๋์ ๊ณผ์ 1. ํ์ ์์ ๋ ธ๋๋ฅผ ํ์ ์ฝ์ ํ๊ณ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ๋ฅผ ํ๋ค. 2. ํ์์ ๋ ธ๋๋ฅผ ๊บผ๋ด ํด๋น ๋ ธ๋์ ์ธ์ ๋ ธ๋ ์ค์์
hyeon-z.tistory.com
์ฒ์ ๋๋น์ด๊ฐ ์์นํด ์๋ (0, 0) ์ขํ, ์ฆ ์ฒ์ ์ขํ๋ฅผ ํ์ ๋ฃ๊ณ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ ํ๋ค.
Queue<Point> queue = new LinkedList<>();
queue.offer(new Point(0, 0));
visited[0][0] = true;
ํ๊ฐ ๋ ธ๋๊ฐ ์๋ ๋์ ํด๋น ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
๋จผ์ ํ์ ์ต์๋จ ๋ ธ๋๋ฅผ ๊บผ๋ด๊ณ ํด๋น ๋ ธ๋์ ์ํ์ข์ฐ๋ฅผ ํ์ํ๋ฉฐ ๋ค์ 3๊ฐ์ง ๊ฒฝ์ฐ์ ํด๋นํ์ง ์๋ ๊ฒฝ์ฐ ํ์ ์ฝ์ ํ๊ณ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ๋ฅผ ํ๋ค.
1. ๋ฏธ๋ก์ ํฌ๊ธฐ๋ฅผ ๋ฒ์ด๋๋ ๊ฒฝ์ฐ
2. ๊ดด๋ฌผ์ด ์๋ ๊ฒฝ์ฐ(1์ธ ๊ฒฝ์ฐ)
3. ๋ฐฉ๋ฌธํ ๊ฒฝ์ฐ
์ฆ, ๋ฏธ๋ก์ ํฌ๊ธฐ๋ฅผ ๋ฒ์ด๋์ง ์๊ณ ๊ดด๋ฌผ์ด ์๊ณ ๋ฐฉ๋ฌธํ์ง ์์ ๊ฒฝ์ฐ ์ด๋ํ๋ค.
while (!queue.isEmpty()) {
Point point = queue.poll();
int x = point.x;
int y = point.y;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= N || ny >= M) {
continue;
}
if (maze[nx][ny] == 0 || visited[nx][ny]) {
continue;
}
queue.offer(new Point(nx, ny));
visited[nx][ny] = true;
}
}
๋๋น์ด๊ฐ ํ์ถํ๊ธฐ ์ํด ์์ง์ฌ์ผ ํ๋ ์ต์ ์นธ์ ๊ฐ์๋ฅผ ๊ตฌํด์ผ ํ๋ฏ๋ก ์ํ์ข์ฐ๋ก ์ด๋ํ๋ ๊ฒฝ์ฐ ์ ์ ์นธ์์ 1์ฉ ๋ํด์ค๋ค.
์ฆ ์ฒ์ ์นธ์ 1์ด๋ฏ๋ก ๊ทธ๋ค์ ์ํ์ข์ฐ๋ก ์ด๋ํ ์นธ์ 2, ์ด๋ ๊ฒ 1์ฉ ๋ํด๊ฐ๋ฉฐ ์ด๋ํ ์นธ์ ๊ฐ์๋ฅผ ์ผ๋ค.
maze[nx][ny] = maze[x][y] + 1;
์์์ ๋งํ๋ BFS๋ ์์ ์ง์ ๋ถํฐ ๊ฐ๊น์ด ๋ ธ๋๋ถํฐ ์ฐจ๋ก๋๋ก ํ์ํ๋ฏ๋ก ์งํํ๋ค ์ถ๊ตฌ๋ฅผ ๋ฐ๊ฒฌํ๊ฒ ๋๋ฉด ๊ทธ๊ฒ์ด ์ต์ ์ด๋ ์นธ์ ๊ฐ์๊ฐ ๋๋ค. ๋ฐ๋ผ์ ๋์๊ฐ ์ ์๋ ์นธ์ ์ขํ์ ์ถ๊ตฌ์ ์ขํ๊ฐ ์ผ์นํ๋ค๋ฉด ํด๋น ์นธ์์ 1์ ๋ํ์ฌ count์ ์ ์ฅํ ํ ํด๋น ํจ์๋ฅผ ์ข ๋ฃํ๋ค.
if (nx == N - 1 && ny == M - 1) {
count = maze[x][y] + 1;
return;
}
์ต์ข bfsํจ์
static void bfs() {
Queue<Point> queue = new LinkedList<>();
queue.offer(new Point(0, 0));
visited[0][0] = true;
while (!queue.isEmpty()) {
Point point = queue.poll();
int x = point.x;
int y = point.y;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= N || ny >= M) {
continue;
}
if (maze[nx][ny] == 0 || visited[nx][ny]) {
continue;
}
if (nx == N - 1 && ny == M - 1) {
count = maze[x][y] + 1;
return;
}
queue.offer(new Point(nx, ny));
visited[nx][ny] = true;
maze[nx][ny] = maze[x][y] + 1;
}
}
}
๊ตฌํ ์ฝ๋
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class TC_5_2 {
static int[][] maze;
static boolean[][] visited;
static int N, M;
static int[] dx = {-1, 1, 0, 0}; // ์ํ์ข์ฐ
static int[] dy = {0, 0, -1, 1};
static int count = 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());
M = Integer.parseInt(st.nextToken());
maze = new int[N][M];
visited = new boolean[N][M];
for (int i = 0; i < N; i++) {
String[] split = br.readLine().split("");
for (int j = 0; j < M; j++) {
maze[i][j] = Integer.parseInt(split[j]);
}
}
bfs();
System.out.println(count);
}
static void bfs() {
Queue<Point> queue = new LinkedList<>();
queue.offer(new Point(0, 0));
visited[0][0] = true;
while (!queue.isEmpty()) {
Point point = queue.poll();
int x = point.x;
int y = point.y;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= N || ny >= M) {
continue;
}
if (maze[nx][ny] == 0 || visited[nx][ny]) {
continue;
}
if (nx == N - 1 && ny == M - 1) {
count = maze[x][y] + 1;
return;
}
queue.offer(new Point(nx, ny));
visited[nx][ny] = true;
maze[nx][ny] = maze[x][y] + 1;
}
}
}
}
Reference
์ด๊ฒ์ด ์ทจ์ ์ ์ํ ์ฝ๋ฉํ ์คํธ๋ค - ๋๋๋น
'๐ฏPS - Baekjoon, etc > BFS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฅ[๋ฐฑ์ค, 1260] DFS์ BFS (Java) (0) | 2023.04.18 |
---|---|
๐ฅ[๋ฐฑ์ค, 7576] ํ ๋งํ (Java) (1) | 2023.03.21 |
๋๊ธ