
2178๋ฒ: ๋ฏธ๋ก ํ์
์ฒซ์งธ ์ค์ ๋ ์ ์ N, M(2 ≤ N, M ≤ 100)์ด ์ฃผ์ด์ง๋ค. ๋ค์ N๊ฐ์ ์ค์๋ M๊ฐ์ ์ ์๋ก ๋ฏธ๋ก๊ฐ ์ฃผ์ด์ง๋ค. ๊ฐ๊ฐ์ ์๋ค์ ๋ถ์ด์ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ค.
www.acmicpc.net
๋ฌธ์

์์

ํ์ด
ํด๋น ๋ฌธ์ ๋ DFS์ BFS๋ฅผ ๋ ๋ค ์ฌ์ฉํด๋ ๋์ง๋ง DFS๋ก ํ๊ฒ ๋๋ฉด ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ค.
๊ทธ๋ ๊ธฐ์ DFS๋ณด๋ค ๋น๊ต์ ๊ณ์ฐ์๋๊ฐ ๋น ๋ฅธ BFS๋ฅผ ํตํด์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํด์ผํ๋ค.
[๋ฐฑ์ค] 1260๋ฒ : DFS์ BFS
1260๋ฒ: DFS์ BFS ์ฒซ์งธ ์ค์ ์ ์ ์ ๊ฐ์ N(1 ≤ N ≤ 1,000), ๊ฐ์ ์ ๊ฐ์ M(1 ≤ M ≤ 10,000), ํ์์ ์์ํ ์ ์ ์ ๋ฒํธ V๊ฐ ์ฃผ์ด์ง๋ค. ๋ค์ M๊ฐ์ ์ค์๋ ๊ฐ์ ์ด ์ฐ๊ฒฐํ๋ ๋ ์ ์ ์ ๋ฒํธ๊ฐ ์ฃผ์ด์ง๋ค.
pids.tistory.com
public static void BFS(int x, int y) {
Queue<int[]> q = new LinkedList<>();
q.add(new int[] { x, y });
while (!q.isEmpty()) {
int now[] = q.poll();
int nowX = now[0];
int nowY = now[1];
for (int i = 0; i < 4; i++) {
int nextX = nowX + dx[i];
int nextY = nowY + dy[i];
if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= m)
continue;
if (visit[nextX][nextY] || check[nextX][nextY] == 0)
continue;
q.add(new int[] {nextX, nextY});
check[nextX][nextY] = check[nowX][nowY] + 1;
visit[nextX][nextY] = true;
}
}
}
์ํ์ข์ฐ๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ฐฐ์ด dx์ dy๋ฅผ ํตํด์ ๋ค ๋ฐฉํฅ์ ํ์ธํ ํ,
์กฐ๊ฑด๋ฌธ์ ํตํด์ ์ด๋ํ ์ ์๋ ๋ฐฐ์ด์ด๋ผ๋ฉด ํ์ ์ถ๊ฐํด ์ฃผ๊ณ visit[][]์ true๋ก ๋ณ๊ฒฝํด ์ฃผ๋ฉด ๋๋ค.
๊ทธ๋ฆฌ๊ณ check๋ฐฐ์ด์ [n-1][m-1]์ ์ถ๋ ฅํด ์ฃผ๋ฉด ํด๋น ๊ฐ์ด ์ถ๋ ฅ๋๋ค.
์์ค ์ฝ๋
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 Main {
static int[][] check;
static int n;
static int m;
static boolean[][] visit;
static int[] dx = { -1, 1, 0, 0 }; // x๋ฐฉํฅ๋ฐฐ์ด - ์ํ
static int[] dy = { 0, 0, -1, 1 }; // y๋ฐฉํฅ๋ฐฐ์ด - ์ข์ฐ
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());
check = new int[n][m];
for (int i = 0; i < n; i++) {
String num = br.readLine();
for (int j = 0; j < m; j++) {
check[i][j] = num.charAt(j) - '0';
}
}
visit = new boolean[n][m];
visit[0][0] = true;
BFS(0,0);
System.out.println(check[n-1][m-1]);
}
public static void BFS(int x, int y) {
Queue<int[]> q = new LinkedList<>();
q.add(new int[] { x, y });
while (!q.isEmpty()) {
int now[] = q.poll();
int nowX = now[0];
int nowY = now[1];
for (int i = 0; i < 4; i++) {
int nextX = nowX + dx[i];
int nextY = nowY + dy[i];
if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= m)
continue;
if (visit[nextX][nextY] || check[nextX][nextY] == 0)
continue;
q.add(new int[] {nextX, nextY});
check[nextX][nextY] = check[nowX][nowY] + 1;
visit[nextX][nextY] = true;
}
}
}
}
์ฐธ๊ณ
[๋ฐฑ์ค 2178] ๋ฏธ๋ก ํ์(Java)
https://www.acmicpc.net/problem/2178 2178๋ฒ: ๋ฏธ๋ก ํ์ ์ฒซ์งธ ์ค์ ๋ ์ ์ N, M(2 ≤ N, M ≤ 100)์ด ์ฃผ์ด์ง๋ค. ๋ค์ N๊ฐ์ ์ค์๋ M๊ฐ์ ์ ์๋ก ๋ฏธ๋ก๊ฐ ์ฃผ์ด์ง๋ค. ๊ฐ๊ฐ์ ์๋ค์ ๋ถ์ด์ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ค. www.a
wiselog.tistory.com
'๐ Algorithm > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [๋ฐฑ์ค] 1874๋ฒ : ์คํ ์์ด (0) | 2023.01.14 |
|---|---|
| [๋ฐฑ์ค] 18111๋ฒ : ๋ง์ธํฌ๋ํํธ (0) | 2023.01.14 |
| [๋ฐฑ์ค] 4949๋ฒ : ๊ท ํ์กํ ์ธ์ (0) | 2023.01.13 |
| [๋ฐฑ์ค] 10773๋ฒ : ์ ๋ก (0) | 2023.01.13 |
| [๋ฐฑ์ค] 10845๋ฒ : ํ (JAVA) (1) | 2023.01.12 |