This repository was archived by the owner on Feb 29, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultimoo.java
More file actions
Latest commit
155 lines (152 loc) · 4.53 KB
/
Copy pathmultimoo.java
File metadata and controls
155 lines (152 loc) · 4.53 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
importjava.io.*;
importjava.util.*;
publicclassmultimoo {
staticQueue<Integer> qx = newLinkedList<>();
staticQueue<Integer> qy = newLinkedList<>();
staticSet<Position> visited = newHashSet<>();
publicstaticint[][] grid;
publicstaticintN;
publicstaticintfloodFill(inttx,intty, Segements) {
visited.clear();
intcovered = 0;
//reset();
intcolor = grid[tx][ty];
qx.add(tx);
qy.add(ty);
while(!qx.isEmpty()) {
covered++;
intx = qx.poll();
inty = qy.poll();
s.pieces.add(newPosition(x,y));
//System.out.println(x+" "+y+" "+visited);
visited.add(newPosition(x,y));
if(x-1 >= 0 && x-1 < N && y < N && y >= 0 && grid[x-1][y] == color&& !visited.contains(newPosition(x-1,y))) {
qx.add(x-1);
qy.add(y);
}
if(x+1 >= 0 && x+1 < N && y < N && y >= 0 && grid[x+1][y] == color&& !visited.contains(newPosition(x+1,y))) {
qx.add(x+1);
qy.add(y);
}
if(x >= 0 && x < N && y +1 < N && y+1 >= 0 && grid[x][y+1] == color&& !visited.contains(newPosition(x,y+1))) {
qx.add(x);
qy.add(y+1);
}
if(x >= 0 && x < N && y-1 < N && y-1 >= 0 && grid[x][y-1] == color && !visited.contains(newPosition(x,y-1))) {
qx.add(x);
qy.add(y-1);
}
}
returncovered;
}
publicstaticvoidmain(String[] args) throwsIOException{
BufferedReaderf = newBufferedReader(newFileReader("multimoo.in"));
N = Integer.parseInt(f.readLine());
grid = newint[N][N];
for(inti = 0; i < N; i ++) {
StringTokenizerst = newStringTokenizer(f.readLine());
for(intj = 0 ; j < N; j ++) {
grid[i][j] = Integer.parseInt(st.nextToken());
}
}
f.close();
intmaxID = 0;
intmaxCount = 0;
List<Segement> pieceList = newArrayList<>();
for(inti = 0; i < N; i ++) {
for(intj = 0; j < N; j ++) {
Segements = newSegement(grid[i][j]);
if(!visited.contains(newPosition(i,j))) {
intid = grid[i][j];
intcount = floodFill(i, j,s);
if(count > maxCount) {
maxCount = count;
maxID = id;
}
}
if(s.pieces.size() > 0) {
pieceList.add(s);
}
}
}
intLSize = 0;
for(Segements:pieceList) {
System.out.println(s.pieces);
}
for(inti = 0; i < N; i ++) {
Segementp = pieceList.get(i);
for(Positionpos:p.pieces) {
intx = pos.x;
inty = pos.y;
intcolor= grid[x][y];
Set<Position> check = newHashSet<Position>();
if(x-1 >= 0 && x-1 < N && y < N && y >= 0 && grid[x-1][y] != color&& !visited.contains(newPosition(x-1,y))) {
check.add(newPosition(x-1,y));
}
if(x+1 >= 0 && x+1 < N && y < N && y >= 0 && grid[x+1][y] != color&& !visited.contains(newPosition(x+1,y))) {
check.add(newPosition(x+1,y));
}
if(x >= 0 && x < N && y +1 < N && y+1 >= 0 && grid[x][y+1] != color&& !visited.contains(newPosition(x,y+1))) {
check.add(newPosition(x,y+1));
}
if(x >= 0 && x < N && y-1 < N && y-1 >= 0 && grid[x][y-1] != color && !visited.contains(newPosition(x,y-1))) {
check.add(newPosition(x,y-1));
}
for(intj = 0; j < pieceList.size(); j ++) {
if(i==j) {
continue;
}
Set<Position> checked = newHashSet<>(check);
checked.retainAll(pieceList.get(j).pieces);
if(checked.size() > 0) {
intsize = pieceList.get(j).pieces.size() + pieceList.get(i).pieces.size();
System.out.println("1: "+i+" "+j+" "+size);
System.out.println("2: "+pieceList.get(i).id+" "+pieceList.get(j).id+" "+size);
if(size>LSize) {
LSize = size;
}
}
checked.clear();
checked = null;
}
}
}
PrintWriterpw = newPrintWriter(newBufferedWriter(newFileWriter("multimoo.out")));
System.out.println("Max Count "+maxCount+" (obtained with id "+maxID+")");
System.out.println("Max Count team up "+LSize);
pw.println(maxCount);
pw.println(LSize);
pw.close();
}
}
classPosition{
intx,y;
//boolean perimeter = false;
publicPosition(intx,inty){
this.x = x;
this.y = y;
}
@Override
publicbooleanequals(Objects) {
if(sinstanceofPosition) {
Positionp = (Position) s;
return ((p.y == this.y) && (p.x == this.x));
}
returnfalse;
}
@Override
publicinthashCode() {
return ((Integer.hashCode(this.x) -7)*(Integer.hashCode(this.y)-5));
}
publicStringtoString() {
return"("+this.x+","+this.y+")";
}
}
classSegement{
publicSet<Position> pieces;
publicintid;
publicSegement(intid) {
this.id = id;
this.pieces = newHashSet<>();
}
}