Repository navigation
Expand file tree
/
Copy pathPopulationMove.java
More file actions
106 lines (85 loc) · 2.83 KB
/
Copy pathPopulationMove.java
File metadata and controls
106 lines (85 loc) · 2.83 KB
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
package implementation;
import java.io.*;
import java.util.*;
class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
}
public class PopulationMove {
static final int dx[] = {0,0,1,-1};
static final int dy[] = {1,-1,0,0};
static ArrayList<Pair> unionXY = new ArrayList<>();
static boolean visit[][];
static int map[][];
static int n,l,r,cnt;
static boolean isMove = false;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
l = Integer.parseInt(st.nextToken());
r = Integer.parseInt(st.nextToken());
map = new int[n][n];
for(int i=0; i<n; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<n; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
move();
System.out.println(cnt);
}
static void move() {
while(true) {
isMove = false;
visit = new boolean[n][n]; //새로 BFS 시작할때마다 방문 초기화
for(int i=0; i<n; i++) {
for(int j=0;j<n; j++) {
if(!visit[i][j]){
bfs(i,j); //방문하지 않은상태면 BFS 시작
}
}
}
if(!isMove) break; //국경이동이 없으면 종료.
else cnt++; //국경이동이 있었다면 일수 추가.
}
}
static void bfs(int x, int y) {
Queue<Pair> q = new LinkedList<>();
q.add(new Pair(x,y));
visit[x][y] = true;
unionXY.add(new Pair(x,y));
while(!q.isEmpty()) {
Pair p = q.poll();
x = p.x;
y = p.y;
for(int k=0; k<4; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if(0<=nx && nx<n && 0<=ny && ny<n) {
if(!visit[nx][ny] && l <= Math.abs(map[x][y] - map[nx][ny]) && Math.abs(map[x][y] - map[nx][ny]) <= r) {
isMove = true;
visit[nx][ny] = true;
unionXY.add(new Pair(nx,ny));
q.add(new Pair(nx,ny));
}
}
}
}
//BFS 가 끝나면 인구이동 결과 맵에 집어넣기
int sum = 0;
for(int i=0; i<unionXY.size(); i++) {
Pair p = unionXY.get(i);
sum += map[p.x][p.y];
}
for(int i=0; i<unionXY.size(); i++) {
Pair p = unionXY.get(i);
map[p.x][p.y] = sum / unionXY.size();
}
unionXY.removeAll(unionXY);
}
}