-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmooyomooyo.txt
114 lines (100 loc) · 3.77 KB
/
mooyomooyo.txt
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
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class mooyomooyo {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("mooyomooyo.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("mooyomooyo.out")),true);
StringTokenizer st = new StringTokenizer(f.readLine());
int numrows = Integer.parseInt(st.nextToken());
int clusterSize = Integer.parseInt(st.nextToken());
int[][] grid = new int[numrows][10];
for(int i=0; i<numrows; i++){
String lol = f.readLine();
String[] row = lol.split("");
for(int j=0; j<10; j++){
grid[i][j]=Integer.parseInt(row[j]);
}
}
while (clearclusters(grid,clusterSize)){
gravity(grid);
}
StringBuilder answer = new StringBuilder();
for(int[] i:grid){
for (int j:i){
answer.append(j);
}
answer.append("\n");
}
out.print(answer);
out.close();
}
static int count =0;
public static ArrayList<int[]> findClusters(int[][] grid, int maxsize){
boolean[][] seen = new boolean[grid.length][grid[0].length];
ArrayList<int[]> startpoints = new ArrayList<>();
for(int i=0; i<seen.length; i++){
for(int j=0; j<seen[0].length;j++ ){
if(!seen[i][j]){
count=0;
flood(grid,false,seen,grid[i][j],i,j);
if(count>=maxsize){
startpoints.add(new int[]{i,j});
}
}
}
}
return startpoints;
}
public static void flood(int[][] arr, boolean shouldZero, boolean[][] seen, int searchPart,int startX, int startY){
if((startX < arr.length && startX > -1)&&(startY < arr[0].length && startY > -1) && arr[startX][startY]== searchPart&&!seen[startX][startY] && searchPart!=0){
count +=1;
if(shouldZero) {
arr[startX][startY] = 0;
}
seen[startX][startY]=true;
flood(arr,shouldZero,seen,searchPart,startX+1,startY);
flood(arr,shouldZero,seen,searchPart,startX-1,startY);
flood(arr,shouldZero,seen,searchPart,startX,startY+1);
flood(arr,shouldZero,seen,searchPart,startX,startY-1);
}else{
return;
}
}
public static boolean clearclusters(int[][] grid, int maxsize){
ArrayList<int[]> targets= findClusters(grid,maxsize);
if(targets.size()==0){
return false;
}
boolean[][] seen = new boolean[grid.length][grid[0].length];
for(int[] x: targets){
Integer count =0;
int startx = x[0];
int starty = x[1];
flood(grid,true,seen,grid[startx][starty],startx,starty);
}
return true;
}
public static void gravity(int[][] grid){
for(int i=0; i<grid[0].length; i++){
int[] meow = new int[grid.length];
for(int j=0; j<grid.length; j++){
if(grid[j][i]==0){
meow[j]=1;
}
}
for(int j = meow.length-2; j>-1; j--){
meow[j]=meow[j]+meow[j+1];
}
for(int j=grid.length-1; j>-1; j--){
if(grid[j][i]!=0){
if(meow[j]!=0) {
grid[j + meow[j]][i] = grid[j][i];
grid[j][i] = 0;
}
}
}
}
}
}