Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14-kokeunho #55

Open
wants to merge 1 commit into
base: 13-kokeunho
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kokeunho/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
| 11μ°¨μ‹œ | 2024.12.21 | 그리디 μ•Œκ³ λ¦¬μ¦˜ | [νšŒμ˜μ‹€ λ°°μ •](https://www.acmicpc.net/problem/1931) | [#43](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/43) |
| 12μ°¨μ‹œ | 2024.12.25 | 그리디 μ•Œκ³ λ¦¬μ¦˜ | [크게 λ§Œλ“€κΈ°](https://www.acmicpc.net/problem/2812) | [#45](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/45) |
| 13μ°¨μ‹œ | 2024.12.30 | κ·Έλž˜ν”„ 탐색 | [μˆ¨λ°”κΌ­μ§ˆ](https://www.acmicpc.net/problem/1697) | [#47](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/47) |
| 14μ°¨μ‹œ | 2025.01.08 | κ·Έλž˜ν”„ 탐색 | [λ―Έλ‘œλ§Œλ“€κΈ°](https://www.acmicpc.net/problem/2665) | [#55](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/55) |
---
62 changes: 62 additions & 0 deletions kokeunho/κ·Έλž˜ν”„ 탐색/14-kokeunho.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.example;

import java.util.*;

public class Main {

static int n;
static int[][] map;
static int[][] distance;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

n = sc.nextInt();
map = new int[n][n];
distance = new int[n][n];

sc.nextLine();
for (int i = 0; i < n; i++) {
String line = sc.nextLine();
for (int j = 0; j < n; j++) {
map[i][j] = line.charAt(j) - '0';
distance[i][j] = Integer.MAX_VALUE;
}
}

System.out.println(bfs(0,0));
}
public static int bfs(int sx, int sy) {
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[2] - b[2]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

비ꡐ 기쀀을 μ •ν•˜λŠ” λžŒλ‹€μ‹μ΄ κΉ”λ”ν•˜λ„€μš”!

μ €λŠ” c++ ꡬ쑰체λ₯Ό μ‚¬μš©ν–ˆμ—ˆλŠ”λ° μš°μ„ μˆœμœ„ 큐에 λ„£μ„λ•Œ 비ꡐ 기쀀을 μ–΄λ–»κ²Œ μ„Έμ›Œμ•Ό 할지 λͺ¨λ₯΄κ² λ”κ΅°μš”
κ·Έλž˜μ„œ 검색도해보고 gpt μ°¬μŠ€λ„ μΌμŠ΅λ‹ˆλ‹€.

struct Point {
    int x, y, broke;
    bool operator<(const Point& other) const { return broke > other.broke; }
};

μ΄λŸ°μ‹μœΌλ‘œ 비ꡐ 기쀀을 μ •ν–ˆμŠ΅λ‹ˆλ‹€.

pq.offer(new int[]{0, 0, 0});
distance[sx][sy] = 0;

while (!pq.isEmpty()) {
int[] current = pq.poll();
int x = current[0];
int y = current[1];
int brokenwall = current[2];

if (x == n - 1 && y == n - 1) return brokenwall;

if (distance[x][y] < brokenwall) continue;

for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];

if (nx >= 0 && nx < n && ny >= 0 && ny < n) {
int newBrokenwall = brokenwall + (map[nx][ny] == 0 ? 1 : 0);

if (newBrokenwall < distance[nx][ny]) {
distance[nx][ny] = newBrokenwall;
pq.offer(new int[]{nx, ny, newBrokenwall});
}
}
}
}
return -1;
}
}
Loading