This problem can be solved by BFS or DFS. I don't see much difference, but I used BFS that uses queue data structure. One thing to consider is to find connected cells that is only in passable point.
In order to find connected cells, I used boolean 2D arrays that saves the state of each cell, and blocked cells.
In Pseudo code,
Create queue
while (Check if there is unpainted cell that is not blocked)
getArea of all neighbors
add area into linked list
end while
array = sort linked list
return array
Here is my complete code.
import java.util.*;
public class grafixMask {
boolean[][] fill = new boolean[400][600];
boolean[][] blocked = new boolean[400][600];
public int[] sortedAreas(String[] retangles) {
List<Integer> list = new ArrayList<Integer>();
for (int i=0; i<400; i++) {
for (int j=0; j<600; j++) {
fill[i][j] = false;
}
}
// mark retangles
markRetangles(retangles);
ArrayList<Integer> al = new ArrayList<Integer>();
LinkedList<Node> q = new LinkedList<Node>();
while (isLeft(q)) {
al.add(getArea(q));
}
Collections.sort(al);
int[] ret = new int[al.size()];
for (int i=0;i<ret.length;i++) {
ret[i] = al.get(i);
}
return ret;
}
public void markRetangles(String[] retangles) {
for (int i=0; i<400; i++) {
for (int j=0; j<600; j++) {
blocked[i][j] = false;
}
}
for (String str : retangles) {
String[] block = str.split(" ");
int[] val = new int[4];
int i = 0;
for (String idx : block) {
val[i++] = Integer.valueOf(idx);
}
for (int j=val[0]; j<=val[2]; j++) {
for (int k=val[1]; k<=val[3]; k++) {
blocked[j][k] = true;
}
}
}
}
public int getArea(LinkedList<Node> q) {
int result = 0;
while (!q.isEmpty()) {
Node top = q.removeFirst();
result++;
int[] dx = {-1,1,0,0};
int[] dy = {0,0,-1,1};
for (int k=0;k<4;k++) {
int x = top.x + dx[k];
int y = top.y + dy[k];
if (x>=0 && x <= 399 && y >=0 && y <=599 && !fill[x][y] && !blocked[x][y]) {
q.add(new Node(x,y));
fill[x][y] = true;
}
}
}
return result;
}
public boolean isLeft(LinkedList<Node> q) {
for (int i=0; i<400; i++) {
for (int j=0; j<600; j++) {
if (!fill[i][j] && !blocked[i][j]) {
q.add(new Node(i,j));
fill[i][j] = true;
return true;
}
}
}
return false;
}
class Node {
int x,y;
Node(int x, int y) {
this.x = x;
this.y = y;
}
}
}
No comments:
Post a Comment