/
ModernArt.java
63 lines (63 loc) · 1.98 KB
/
ModernArt.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
import java.util.*;
import java.io.*;
public class ModernArt {
static HashSet<Character> colorsVis = new HashSet<>();
static char[][] painting;
static HashSet<Character> intruders = new HashSet<>();
static int n;
public static void main(String[] args) throws Exception{
Scanner scan = new Scanner(new BufferedReader(new FileReader("art.in")));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("art.out")));
/*
4
0201
2230
2737
2777
0000
*/
n = scan.nextInt();
painting = new char[n][n];
for (int i = 0; i < n; i++) {
String s = scan.next();
for (int j = 0; j < n; j++) {
char ch = s.charAt(j);
if ((ch != '0')) {
colorsVis.add(ch);
}
painting[i][j] = ch;
}
}
for (char color : colorsVis) {
tryColor(color);
}
pw.println(colorsVis.size() - intruders.size());
scan.close();
pw.close();
}
public static void tryColor(char color) {
int iMin = Integer.MAX_VALUE;
int iMax = Integer.MIN_VALUE;
int jMin = Integer.MAX_VALUE;
int jMax = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (painting[i][j] == color) {
// i is going to be every row at which there is 'color'
// min(i) gives iMin, iMax
iMin = Math.min(i, iMin);
iMax = Math.max(i, iMax);
jMin = Math.min(j, jMin);
jMax = Math.max(j, jMax);
}
}
}
for (int i = iMin; i <= iMax; i++) {
for (int j = jMin; j <= jMax; j++) {
if (painting[i][j] != color) {
intruders.add(painting[i][j]);
}
}
}
}
}