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

8-kokeunho #28

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -9,4 +9,5 @@
| 5μ°¨μ‹œ | 2024.11.03 | 트리 | [LCA](https://www.acmicpc.net/problem/11437) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/19) |
| 6μ°¨μ‹œ | 2024.11.05 | 자료ꡬ쑰 | [μš”μ„Έν‘ΈμŠ€ 문제](https://www.acmicpc.net/problem/1158) | [#20](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/20) |
| 7μ°¨μ‹œ | 2024.11.12 | κ΅¬ν˜„ | [μΉ˜ν‚¨ 배달](https://www.acmicpc.net/problem/15686) | [#25](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/25) |
| 8μ°¨μ‹œ | 2024.11.17 | κ΅¬ν˜„ | [인ꡬ 이동](https://www.acmicpc.net/problem/16234) | [#28](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/28) |
---
87 changes: 87 additions & 0 deletions kokeunho/κ΅¬ν˜„/8-kokeunho.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.example;

import java.sql.SQLOutput;
import java.util.*;

public class Main {

static int N, R, L;
static int[][] map;
static boolean[][] visited;
static int[][] direction = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

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

N = sc.nextInt();
L = sc.nextInt();
R = sc.nextInt();

map = new int[N][N];

for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
map[i][j] = sc.nextInt();
}
}

int day_count = 0;

while(true) {
visited = new boolean[N][N];
boolean moved = false;

for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(!visited[i][j]) {
List<int[]> union = new ArrayList<> ();
int populationSum = bfs(i, j, union);
if(populationSum > 0) {
moved = true;
int newPopulation = populationSum / union.size();
for(int[] country : union) {
map[country[0]][country[1]] = newPopulation;
}
}
}
}
}
if(!moved) {
break;
}
day_count++;
}

System.out.println(day_count);
}

private static int bfs(int x, int y, List<int[]> union) {
Queue<int[]> queue = new LinkedList<> ();
queue.add(new int[]{x, y});
union.add(new int[]{x, y});
visited[x][y] = true;
int populationSum = map[x][y];

while(!queue.isEmpty()) {
int[] now_position = queue.poll();
int nowX = now_position[0];
int nowY = now_position[1];

for(int[] dir : direction) {
int nx = nowX + dir[0];
int ny = nowY + dir[1];

if(nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {
int diff = Math.abs(map[nowX][nowY] - map[nx][ny]);
if(diff >= L && diff <= R) {
visited[nx][ny] = true;
queue.add(new int[]{nx, ny});
union.add(new int[]{nx, ny});
populationSum += map[nx][ny];
}
}
}
}
return union.size() > 1 ? populationSum : 0;
}
}