-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKama_104.java
71 lines (65 loc) · 2.29 KB
/
Kama_104.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
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Kama_104 {
static int count;
static HashMap<Integer, Integer> map = new HashMap<>();
static int[][] dir = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] grid = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
grid[i][j] = sc.nextInt();
}
}
int num = 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 1) {
count = 1;
grid[i][j] = num;
dfs(i, j, grid, num);
map.put(num++, count);
}
}
}
int res = 0;
boolean flag = true;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 0) {
flag = false;
Set<Integer> set = new HashSet<>();
int tmp = 1;
for (int[] dir : dir) {
int nextX = i + dir[0];
int nextY = j + dir[1];
if (nextX < 0 || nextX > grid.length - 1 || nextY < 0 || nextY > grid[0].length - 1) continue;
int key = grid[nextX][nextY];
if (set.contains(key)) continue;
set.add(key);
tmp += map.getOrDefault(key, 0);
}
res = Math.max(res, tmp);
}
}
}
System.out.println(flag ? n * m : res);
}
public static void dfs(int x, int y, int[][] grid, int num) {
for (int[] dir : dir) {
int nextX = x + dir[0];
int nextY = y + dir[1];
if (nextX < 0 || nextX > grid.length - 1 || nextY < 0 || nextY > grid[0].length - 1) continue;
if (grid[nextX][nextY] == 1) {
grid[nextX][nextY] = num;
count++;
dfs(nextX, nextY, grid, num);
}
}
}
}