-
Notifications
You must be signed in to change notification settings - Fork 0
/
16234(BFS)(UnSolved).java
110 lines (89 loc) · 2.87 KB
/
16234(BFS)(UnSolved).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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class B {
static int N, L, R, total, tempCount;
static int arr[][];
static boolean visited[][];
static int dx[] = {0, 0, -1, 1};
static int dy[] = {-1, 1, 0, 0};
static ArrayList<Pair> arrayList = new ArrayList<>();
public static void bfs(int x, int y) {
Queue<Pair> queue = new LinkedList<>();
queue.add(new Pair(x, y));
visited[x][y] = true;
while (!queue.isEmpty()) {
Pair current = queue.remove();
arrayList.add(current);
total += arr[current.x][current.y];
tempCount++;
for(int i = 0; i < 4; i++) {
int nextX = current.x + dx[i];
int nextY = current.y + dy[i];
if(nextX < 0 || nextY < 0 || nextX >= N || nextY >= N){
continue;
}
int diff = Math.abs(arr[current.x][current.y] - arr[nextX][nextY]);
if(!visited[nextX][nextY] && (diff >= L && diff <= R)){
visited[nextX][nextY] = true;
queue.add(new Pair(nextX, nextY));
}
}
}
}
public static void init(int num) {
for (Pair temp : arrayList) {
arr[temp.x][temp.y] = num;
}
}
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
L = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
arr = new int[N][N];
visited = new boolean[N][N];
for(int i = 0; i < N; i++){
st = new StringTokenizer(br.readLine());
for(int j = 0; j < N; j++){
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
int count = 0;
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(!visited[i][j]){
arrayList = new ArrayList<>();
total = 0;
tempCount = 0;
bfs(i, j);
int num = (total / arrayList.size());
init(num);
count++;
}
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
if(count == (N * N)){
System.out.println(0);
}else{
System.out.println(count);
}
}
}
class Pair{
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}