Problem ๐ป
4485. ๋ น์ ์ท ์ ์ ์ ๊ฐ ์ ค๋ค์ง?
๋งํฌ๊ฐ ‘๋๋๋ฃจํผ’๋ก ๊ฐ๋์ฐฌ ๋๊ตด์์ (0, 0) → (n-1, n-1)๊น์ง ์ต์ํ์ ๋ฃจํผ๋ฅผ ์์ผ๋ฉด์ ๋์ฐฉํด์ผ ํ๋ ๋ฌธ์ ์ด๋ค. ๋๊ตด์ ๊ฐ ์นธ๋ง๋ค ๋๋๋ฃจํผ์ ๊ฐ์น๊ฐ ์ฃผ์ด์ง๊ณ ํด๋น ๊ฐ์น๊ฐ ํด์๋ก ์๋ ๋์ด ๋ง์์ง๋ ๊ฒ์ด๋ค.
Approach 1 โญ
- ๋ค์ต์คํธ๋ผ (00:13:12)
์ด ๋ฌธ์ ์ ํด๊ฒฐ๋ฐฉ๋ฒ์ ‘๋๋๋ฃจํผ’๋ฅผ ์กฐ๊ธ ๋ค๋ฅด๊ฒ ์๊ฐํด ํด๋น ๋ ธ๋๋ก ๊ฐ๋ ‘๊ฐ์ค์น’๋ผ๊ณ ์๊ฐํ๋ ๊ฒ์ด๋ค. ํด๋น ๋ ธ๋๋ก ๊ฐ๋ ๋ชจ๋ ๊ฐ์ ์ ๊ฐ์ค์น๊ฐ ๋๋๋ฃจํผ์ ๊ฐ์น๋ก ๊ณ ์ ๋๋ ๊ฒ์ด๋ค. ๊ทธ๋ผ, ๊ฐ์ค์น ๊ทธ๋ํ์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๊ธฐ ์ํด์ ์ฌ์ฉํ ์ ์๋ ์๊ณ ๋ฆฌ์ฆ์ธ ‘๋ค์ต์คํธ๋ผ’๋ฅผ ์๊ฐํ ์ ์๋ค.
Approach 2 โญ
- BFS (00:10:27)
๋ ๋ฒ์งธ๋ก๋ BFS๋ฅผ ์ด์ฉํ ๋ฐฉ๋ฒ์ด ์๋ค. ๊ฐ ์นธ์ ๋๋ฌํ์ ๋ ๋๋๋ฃจํผ์ ํฉ์ด ํ์ฌ ์นธ์ ์ ์ฅ๋ ํฉ๋ณด๋ค ์๋ค๋ฉด ํด๋น ํฉ์ผ๋ก ๊ฐฑ์ ํ๋ฉด์ ์ต์ ๋๋๋ฃจํผ๋ฅผ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ด๋ค.
2
5 5
3 9
์ด๋ฌํ ๋๊ตด์ด ์์ ๋ ์ฒ์์ (0, 0) → (0, 1) → (1, 1)๋ก ๊ฐ๋ฉด 24์ ๋ฃจํผ๋ฅผ ์๊ฒ๋์ง๋ง (0, 0) → (1, 0) → (1, 1) ๊ฒฝ๋ก๋ก ๊ฐ๊ฒ ๋๋ฉด 17๋งํผ์ ๋ฃจํผ๋ฅผ ์๊ฒ๋๋ฏ๋ก ์ด๋ฅผ ๊ฐฑ์ ์์ผ์ฃผ๋ ๊ฒ์ด๋ค.
๋ง์ฝ ๋ฐ๋๋ก (0, 0) → (1, 0) → (1, 1) ๊ฒฝ๋ก๋ก ๋จผ์ ๊ฐ๋ค๊ฐ (0, 0) → (0, 1) → (1, 1) ๊ฒฝ๋ก๋ก ์จ๋ค๋ฉด ๊ฐฑ์ ํ์ง ์๊ณ ์ฐจ๋ก๋ฅผ ๋๊ธด๋ค.
์ด์ฒ๋ผ visited ๋ฐฐ์ด์ ์๋ bfs์ฒ๋ผ ๊ทธ ์นธ์ ๊ฐ๊ธฐ๊น์ง์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ก ๋ณด์ง์๊ณ ๊ทธ ์นธ์ ๊ฐ๊ธฐ๊น์ง์ ์ต์ ๋๋๋ฃจํผ ๊ฐ์น๋ก ๋ณด๊ณ ๋ฌธ์ ๋ฅผ ํธ๋ ๊ฒ์ด๋ค.
Solution ๐ก
// ๋ค์ต์คํธ๋ผ
public class BOJ_4485 {
private static int[] dy = {-1, 1, 0, 0};
private static int[] dx = {0, 0, -1, 1};
private static int[][] dungeon;
private static int n;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = 1;
while ((n = Integer.parseInt(br.readLine())) != 0) {
dungeon = new int[n][n];
for (int y = 0; y < n; y++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int x = 0; x < n; x++) dungeon[y][x] = Integer.parseInt(st.nextToken());
}
int[][] shortest = dijkstra(0, 0);
System.out.println("Problem " + t++ + ": " + shortest[n-1][n-1]);
}
}
public static int[][] dijkstra(int y, int x) {
int[][] shortest = new int[n][n];
for (int i = 0; i < n; i++) Arrays.fill(shortest[i], 987654321);
shortest[y][x] = dungeon[y][x];
boolean[][] visited = new boolean[n][n];
while (true) {
int smallY = 0;
int smallX = 0;
int value = Integer.MAX_VALUE;
for (int yy = 0; yy < n; yy++) {
for (int xx = 0; xx < n; xx++) {
if (!visited[yy][xx] && value > shortest[yy][xx]) {
value = shortest[yy][xx];
smallY = yy;
smallX = xx;
}
}
}
if (smallY == n-1 && smallX == n-1) break;
visited[smallY][smallX] = true;
for (int i = 0; i < 4; i++) {
int ny = smallY + dy[i];
int nx = smallX + dx[i];
if (ny < 0 || ny >= n || nx < 0 || nx >= n) continue;
if (visited[ny][nx]) continue;
shortest[ny][nx] = Math.min(shortest[smallY][smallX] + dungeon[ny][nx], shortest[ny][nx]);
}
}
return shortest;
}
}
// BFS
public class BOJ_4485_2 {
private static int[] dy = {-1, 1, 0, 0};
private static int[] dx = {0, 0, -1, 1};
private static int[][] dungeon;
private static int[][] visited;
private static int n;
private static class Point {
int y, x;
public Point(int y, int x) {
this.y = y;
this.x = x;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = 1;
while ((n = Integer.parseInt(br.readLine())) != 0) {
dungeon = new int[n][n];
visited = new int[n][n];
for (int i = 0; i < n; i++) Arrays.fill(visited[i], 987654321);
for (int y = 0; y < n; y++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int x = 0; x < n; x++) dungeon[y][x] = Integer.parseInt(st.nextToken());
}
bfs(0, 0);
System.out.printf("Problem %d: %d\\n", t++, visited[n-1][n-1]);
}
}
private static void bfs(int y, int x) {
ArrayDeque<Point> q = new ArrayDeque<>();
q.add(new Point(y, x));
visited[y][x] = dungeon[y][x];
while (!q.isEmpty()) {
Point p = q.poll();
for (int i = 0; i < 4; i++) {
int ny = p.y + dy[i];
int nx = p.x + dx[i];
if (ny < 0 || ny >= n || nx < 0 || nx >= n) continue;
if (visited[ny][nx] <= visited[p.y][p.x] + dungeon[ny][nx]) continue;
visited[ny][nx] = visited[p.y][p.x] + dungeon[ny][nx];
q.add(new Point(ny, nx));
}
}
}
}
'์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
14658. ํ๋์์ ๋ณ๋ฅ๋ณ์ด ๋น๋ฐ์น๋ค (0) | 2025.01.29 |
---|---|
1863. ์ค์นด์ด๋ผ์ธ ์ฌ์ด๊ฑฐ (0) | 2025.01.22 |
1238. ํํฐ (0) | 2025.01.17 |
ํ๋ก์ด๋ ์์ ์๊ณ ๋ฆฌ์ฆ (0) | 2025.01.17 |
ํ์ ํธ๋ฆฌ (1) | 2024.10.03 |