๋ฌธ์
[๋ฐฑ์ค 1260๋ฒ]
N×Mํฌ๊ธฐ์ ๋ฐฐ์ด๋ก ํํ๋๋ ๋ฏธ๋ก๊ฐ ์๋ค.
1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 0 1 1
๋ฏธ๋ก์์ 1์ ์ด๋ํ ์ ์๋ ์นธ์ ๋ํ๋ด๊ณ , 0์ ์ด๋ํ ์ ์๋ ์นธ์ ๋ํ๋ธ๋ค. ์ด๋ฌํ ๋ฏธ๋ก๊ฐ ์ฃผ์ด์ก์ ๋, (1, 1)์์ ์ถ๋ฐํ์ฌ (N, M)์ ์์น๋ก ์ด๋ํ ๋ ์ง๋์ผ ํ๋ ์ต์์ ์นธ ์๋ฅผ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. ํ ์นธ์์ ๋ค๋ฅธ ์นธ์ผ๋ก ์ด๋ํ ๋, ์๋ก ์ธ์ ํ ์นธ์ผ๋ก๋ง ์ด๋ํ ์ ์๋ค.
์์ ์์์๋ 15์นธ์ ์ง๋์ผ (N, M)์ ์์น๋ก ์ด๋ํ ์ ์๋ค. ์นธ์ ์ ๋์๋ ์์ ์์น์ ๋์ฐฉ ์์น๋ ํฌํจํ๋ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์ ๋ ์ ์ N, M(2 ≤ N, M ≤ 100)์ด ์ฃผ์ด์ง๋ค. ๋ค์ N๊ฐ์ ์ค์๋ M๊ฐ์ ์ ์๋ก ๋ฏธ๋ก๊ฐ ์ฃผ์ด์ง๋ค. ๊ฐ๊ฐ์ ์๋ค์
๋ถ์ด์
์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ค.
์ถ๋ ฅ
์ฒซ์งธ ์ค์ ์ง๋์ผ ํ๋ ์ต์์ ์นธ ์๋ฅผ ์ถ๋ ฅํ๋ค. ํญ์ ๋์ฐฉ์์น๋ก ์ด๋ํ ์ ์๋ ๊ฒฝ์ฐ๋ง ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ค.
์์
ํ์ด
BFS๋ฅผ ์ด์ฉํ์ฌ ๋ฏธ๋ก์ ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ค.
0. ์ฑ๊ณต ์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static Set<int[]> visited = new HashSet<>(); //๋ฐฉ๋ฌธํ ๋ฆฌ์คํธ
public static int[][] map;
public static int[][] direction = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; //๋ฐฉํฅ๋ฐฐ์ด - ์, ํ, ์ข, ์ฐ
public static int n;
public static int m;
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());
// n*m ํฌ๊ธฐ์ ๋ฐฐ์ด (๋ฏธ๋ก)
map = new int[n][m];
for (int i = 0; i < n; i++) {
String temp = br.readLine();
for (int j = 0; j < m; j++) {
map[i][j] = temp.charAt(j) - '0' ;
}
}
bfs(0, 0); // (0, 0)๋ถํฐ ์์
System.out.println(map[n-1][m-1]);
}
public static void bfs(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
int[] first = {x, y};
queue.offer(first); // Queue์ ํ์ฌ ๋
ธ๋ ๋ฃ๊ธฐ
visited.add(first); // ๋ฐฉ๋ฌธ ๊ธฐ๋ก
while (!queue.isEmpty()) { // Queue๊ฐ ๋น ๋๊น์ง ๋ฐ๋ณต
int[] current = queue.poll(); // Queue์์ ํ์ฌ ๊ฐ ๊บผ๋ด๊ธฐ
//์ธ์ ํ ๋
ธ๋๋ค ๋ชจ๋ ๋ฐฉ๋ฌธ - ์, ํ, ์ข, ์ฐ ๋ชจ๋ ๋ฐฉ๋ฌธ
for (int i = 0; i<4; i++) {
int[] next = {current[0] + direction[i][0], current[1] + direction[i][1]};
if(next[0] >= 0 && next[1] >= 0 && next[0] < n && next[1] < m) { //map bound์ ๋์น๋์ง ๊ฒ์ฌ
if( map[next[0]][next[1]] == 1 && !visited.contains(next)) { //๊ฐ ์ ์๋ ๊ธธ์ธ์ง & ๋ฐฉ๋ฌธ ํ๋ ๊ธธ์ธ์ง ๊ฒ์ฌ
queue.add(next);
map[next[0]][next[1]] = map[current[0]][current[1]] + 1;
visited.add(next);
}
}
}
}
}
}
1. ๋ฌธ์ ํ์ด
input
public static Set<int[]> visited = new HashSet<>(); //๋ฐฉ๋ฌธํ ๋ฆฌ์คํธ
public static int[][] map;
public static int[][] direction = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; //๋ฐฉํฅ๋ฐฐ์ด - ์, ํ, ์ข, ์ฐ
public static int n;
public static int m;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
// n*m ํฌ๊ธฐ์ ๋ฐฐ์ด (๋ฏธ๋ก)
map = new int[n][m];
for (int i = 0; i < n; i++) {
String temp = br.readLine();
for (int j = 0; j < m; j++) {
map[i][j] = temp.charAt(j) - '0' ;
}
}
๋ฏธ๋ก๋ฅผ ์ ๋ ฅ๋ฐ์ 2์ฐจ์ ๋ฐฐ์ด์ ๋ฃ์ด๋์๋ค.
๋ฏธ๋ก ๋ด์ ๋ฐฉํฅ์ ์ฐพ์๊ฐ๊ธฐ ์ํด ๋ฐฉํฅ๋ฐฐ์ด ์, ํ, ์ข, ์ฐ๋ฅผ ์ ์ธํด ๋์๋ค.
BFS
public static void bfs(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
int[] first = {x, y};
queue.offer(first); // Queue์ ํ์ฌ ๋
ธ๋ ๋ฃ๊ธฐ
visited.add(first); // ๋ฐฉ๋ฌธ ๊ธฐ๋ก
while (!queue.isEmpty()) { // Queue๊ฐ ๋น ๋๊น์ง ๋ฐ๋ณต
int[] current = queue.poll(); // Queue์์ ํ์ฌ ๊ฐ ๊บผ๋ด๊ธฐ
//์ธ์ ํ ๋
ธ๋๋ค ๋ชจ๋ ๋ฐฉ๋ฌธ - ์, ํ, ์ข, ์ฐ ๋ชจ๋ ๋ฐฉ๋ฌธ
for (int i = 0; i<4; i++) {
int[] next = {current[0] + direction[i][0], current[1] + direction[i][1]};
if(next[0] >= 0 && next[1] >= 0 && next[0] < n && next[1] < m) { //map bound์ ๋์น๋์ง ๊ฒ์ฌ
if( map[next[0]][next[1]] == 1 && !visited.contains(next)) { //๊ฐ ์ ์๋ ๊ธธ์ธ์ง & ๋ฐฉ๋ฌธ ํ๋ ๊ธธ์ธ์ง ๊ฒ์ฌ
queue.add(next);
map[next[0]][next[1]] = map[current[0]][current[1]] + 1;
visited.add(next);
}
}
}
}
}
Queue๋ฅผ ์ด์ฉํ์ฌ BFS๋ฅผ ๊ตฌํํ์๋ค.
์ด์ BFS ๊ธฐ๋ณธ ๋ฌธ์ ์ ๋ค๋ฅด๊ฒ, ์ขํ (x, y)๋ฅผ ์ด์ฉํ๊ธฐ ์ํด ๋ฐฐ์ด์ ์ด์ฉํด์ ํํํ๋ค.
1. ์ฒ์ ์์ ๋ ธ๋๋ฅผ Queue์ ๋ฃ์ด๋๋ค.
2. while๋ฌธ์ ์์ํ๋ค.
2-1. Queue์์ ํ์ฌ ๋ ธ๋๋ฅผ ๊บผ๋ธ๋ค.
2-2. ์, ํ, ์ข, ์ฐ๋ฅผ ๋ชจ๋ ๋ฐฉ๋ฌธํ๋ for๋ฌธ์ ์์ํ๋ค.
2-2-1. ๋ฐฉํฅ๋ฐฐ์ด์ ํตํด ๋ค์ ์ขํ๋ฅผ ์ ์ฅํด๋๋ค.
2-2-2. map bound๋ฅผ ๋์น๋์ง ๊ฒ์ฌํ๋ค.
2-2-3. ๊ฐ ์ ์๋ ์ขํ์ธ์ง ๊ฒ์ฌํ๋ค.
2-2-4. ์ด๋ฏธ ๋ฐฉ๋ฌธํ ์ขํ์ธ์ง ๊ฒ์ฌํ๋ค.
2-2-5. Queue์ ๋ค์ ์ขํ๋ฅผ ๋ฃ์ด๋๋ค.
2-2-6. Queue์ ํ์ฌ ์ขํ + 1 ๊ฐ์ ๋ค์ ์ขํ์ ๋ฃ์ด๋๋ค.
2-2-7. ๋ค์ ์ขํ๋ฅผ ๋ฐฉ๋ฌธ ๊ธฐ๋ก ํด๋๋ค.
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๋ฐฑ์ค 18111 ๋ง์ธํฌ๋ํํธ ๋ ํจ์จ์ ์ผ๋ก ํ๊ธฐ (0) | 2024.03.03 |
---|---|
[Java] ์ด๋ถํ์ (0) | 2024.03.01 |
[Java] DFS์ BFS (2) | 2024.02.27 |