[JAVA]389480 ์์ ๋ฒ์ฃ
๐ ๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/389480
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก์ Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
๐ ์์ด๋์ด
bfs๋ฅผ ํตํ ์์ ํ์ํ ๊ตฌํ.
but ํ์ธํ๋๊ฒ์ ์ฌํ์ธํ์ง ์๋๋ก ํด์ผ ์๊ฐ ์ด๊ณผ๋ฅผ ๋ฉดํ ์ ์๋ค.
๐ ํ์ด
import java.util.*;
class Solution {
public int solution(int[][] info, int n, int m) {
int min = Integer.MAX_VALUE;
boolean[][] saved;
Queue<int[]> queue = new ArrayDeque<>();
queue.add(new int[]{0, 0});
for (int[] in : info) {
int now = queue.size();
saved = new boolean[n][m];
for (int j = 0; j < now; j++) {
int[] poll = queue.poll();
int nowA = poll[0];
int nowB = poll[1];
int mvA = nowA + in[0];
int mvB = nowB + in[1];
if (mvA < n && !saved[mvA][nowB]) {
queue.add(new int[]{mvA, nowB});
saved[mvA][nowB] = true;
}
if (mvB < m && !saved[nowA][mvB]) {
queue.add(new int[]{nowA, mvB});
saved[nowA][mvB] = true;
}
}
}
while (!queue.isEmpty()) {
int[] poll = queue.poll();
min = Math.min(poll[0], min);
}
return (min == Integer.MAX_VALUE) ? -1 : min;
}
}
'programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [JAVA]388351 ์ ์ฐ๊ทผ๋ฌด์ (0) | 2025.05.24 |
|---|---|
| [JAVA]389479 ์๋ฒ ์ฆ์ค ํ์ (0) | 2025.05.24 |
| [JAVA]68645 ์ผ๊ฐ๋ฌํฝ์ด (0) | 2023.07.02 |
| [JAVA]43164 ์ฌํ๊ฒฝ๋ก , DFS (0) | 2023.06.30 |