Skip to content

Commit

Permalink
2025-01-20/안전 영역
Browse files Browse the repository at this point in the history
  • Loading branch information
kokeunho committed Jan 19, 2025
1 parent 2b41dec commit 6c30615
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Binary file modified kokeunho/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions kokeunho/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
| 14차시 | 2025.01.08 | 그래프 탐색 | [미로만들기](https://www.acmicpc.net/problem/2665) | [#55](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/55) |
| 15차시 | 2025.01.12 | 조합론 | [격자상의 경로](https://www.acmicpc.net/problem/10164) | [#58](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/58) |
| 16차시 | 2025.01.19 | 그래프 탐색 | [DFS와 BFS](https://www.acmicpc.net/problem/1260) | [#63](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/63) |
| 17차시 | 2025.01.20 | 그래프 탐색 | [안전 영역](https://www.acmicpc.net/problem/2468) | [#64](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/64) |
---
51 changes: 51 additions & 0 deletions kokeunho/그래프 탐색/17-kokeunho.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import java.util.*;

public class Main {
static int n;
static int[][] map;
static boolean[][] visited;
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];
visited = new boolean[n][n];

int max_height = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
map[i][j] = sc.nextInt();
max_height = Math.max(max_height, map[i][j]);
}
}

int max_area = 0;
for (int h = 0; h <= max_height; h++) {
visited = new boolean[n][n];
int safe_area = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!visited[i][j] && map[i][j] > h) {
dfs(i, j, h);
safe_area++;
}
}
}
max_area = Math.max(max_area, safe_area);
}
System.out.print(max_area);
}
public static void dfs(int startX, int startY, int height) {
visited[startX][startY] = true;
for (int i = 0; i < 4; i++) {
int nx = startX + dx[i];
int ny = startY + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < n && !visited[nx][ny] && map[nx][ny] > height) {
dfs(nx, ny, height);
}
}
}
}

0 comments on commit 6c30615

Please sign in to comment.