-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16234.java
110 lines (86 loc) · 2.86 KB
/
16234.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.*;
public class Main {
static class Point {
int x,y;
Point(int x, int y) {this.x=x; this.y=y;}
}
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 = 0;
boolean changed;
// 현재 map의 상태에서 에서 연합을 이룰 것들을 검사
do {
changed = false;
check = new boolean[n][n];
int[][] map_new = new int[n][n];
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
// 아래 재귀호출 도중에 연합을 이뤄낸 국가이면 다시 방문할 필요없음
if(check[i][j])
continue;
// 연합을 이룰 국가들의 리스트
List<Point> union = new ArrayList<>();
union.add(new Point(i, j));
check[i][j] = true;
// 연합 가능한 국가를 DFS로 찾고 없으면 끝
dfs(union, i, j);
if(union.size() == 1)
continue;
// 국경을 열어야할 나라들이 존재하는 경우 map_new에 기록
changed = true;
int sum = 0;
for(Point p : union) {
sum += map[p.x][p.y];
}
sum /= union.size();
for(Point p : union) {
map_new[p.x][p.y] = sum;
}
}
}
// 연합하지 못한 국가는 기존의 값을 덮어씀
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if (map_new[i][j] == 0)
map_new[i][j] = map[i][j];
}
}
// 새 맵으로 갱신하고, 바뀐점이 있는지 체크
map = map_new;
if(changed)
day++;
} while(changed);
System.out.println(day);
}
static int[] dx = {1, 0, -1, 0};
static int[] dy = {0, 1, 0, -1};
static int n;
static int l;
static int r;
static boolean[][] check;
static int[][] map;
static void dfs(List<Point> union, int x, int y) {
for(int i=0; i<4; i++) {
int xx = x+dx[i];
int yy = y+dy[i];
if(xx<0 || xx>=n || yy<0 || yy>=n || check[xx][yy])
continue;
int val = Math.abs(map[x][y] - map[xx][yy]);
if(l <= val && val <= r) {
union.add(new Point(xx, yy));
check[xx][yy] = true;
dfs(union, xx, yy);
}
}
}
}