-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
97 lines (71 loc) · 2.6 KB
/
Main.java
File metadata and controls
97 lines (71 loc) · 2.6 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
static int[] dc = {-1, 1, 0, 0};
static int[] dr = {0, 0, -1, 1};
static int N;
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());
ArrayList<String>[] board = new ArrayList[N];
for (int i = 0; i < N; i++) {
String candy = br.readLine();
board[i] = new ArrayList<>();
for (int j = 0; j < N; j++) {
board[i].add(String.valueOf(candy.charAt(j)));
}
}
int total = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int cc = 0;
int cr = 0;
for (int k = 0; k < 4; k++) {
cc = i + dc[k];
cr = j + dr[k];
if (!(0 > cc || cc >= N || 0 > cr || cr >= N)) {
if (!(board[cc].get(cr).equals(board[i].get(j)))) {
String tmp = board[cc].get(cr);
String Nowtmp = board[i].get(j);
board[i].set(j, tmp);
board[cc].set(cr, Nowtmp);
total = Integer.max(total,CountCandy(board));
board[i].set(j, Nowtmp);
board[cc].set(cr, tmp);
}
}
}
}
}
System.out.println(total);
}
public static int CountCandy(ArrayList<String>[] board) {
int maxRow = -(int) 1e9;
int maxCol = -(int) 1e9;
int totalMax;
for (int i = 0; i < N; i++) {
int rowCnt = 1;
int colCnt = 1;
for (int j = 0; j < N - 1; j++) {
if (board[i].get(j).equals(board[i].get(j + 1))) {
rowCnt += 1;
} else {
rowCnt = 1;
}
if (board[j + 1].get(i).equals(board[j].get(i))) {
colCnt += 1;
} else {
colCnt = 1;
}
maxRow = Integer.max(maxRow, rowCnt);
maxCol = Integer.max(maxCol, colCnt);
}
}
totalMax = Integer.max(maxRow, maxCol);
return totalMax;
}
}