-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
64 lines (53 loc) Β· 1.79 KB
/
Main.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
package BruteForce.P2468;
import java.io.*;
import java.util.*;
public class Main {
static int N, ans;
static int[][] map;
static HashSet<Integer> set = new HashSet<>();
static boolean[][] visited;
static int[] di = {-1, 0, 1, 0};
static int[] dj = {0, 1, 0, -1};
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/BruteForce/P2468/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
map = new int[N][N];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
set.add(map[i][j]);
}
}
set.add(0);
for (int n : set) {
visited = new boolean[N][N];
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (map[i][j] > n && !visited[i][j]) {
visited[i][j] = true;
dfs(i, j, n);
cnt ++;
}
}
}
ans = Math.max(ans, cnt);
}
System.out.println(ans);
}
static void dfs(int i, int j, int n) {
for (int t = 0; t < 4; t++) {
int to_i = i + di[t];
int to_j = j + dj[t];
if (isValidPath(to_i, to_j) && !visited[to_i][to_j] && map[to_i][to_j] > n) {
visited[to_i][to_j] = true;
dfs(to_i, to_j, n);
}
}
}
static boolean isValidPath(int i, int j) {
return 0 <= i && i < N && 0 <= j && j < N;
}
}