the image has random shapes filled with 0s, separated by 1s. Find all the shapes. Each shape is represented by coordinates of all the elements inside.
public class Main {
public List<List<Integer>> findMultipleShapes(int[][] board)
{
List<List<Integer>> result = new ArrayList<>();
for(int i = 0; i< board.length; i++)
{
for(int j = 0; j< board[0].length; j++)
{
if(board[i][j] == 0)
{
List<Integer> shape = new ArrayList<Integer>();
dfs(board, i, j, shape);
result.add(shape);
}
}
}
return result;
}
public void dfs(int[][] board, int x, int y, List<Integer> shape)
{
if(!isBound(board, x, y))
{
return;
}
if(board[x][y] == 1)
{
return;
}
shape.add(x);
shape.add(y);
board[x][y] = 1;
dfs(board, x-1, y, shape);
dfs(board, x+1, y, shape);
dfs(board, x, y-1, shape);
dfs(board, x, y+1, shape);
}
public boolean isBound(int[][] board, int x, int y)
{
return x>=0 && x<board.length && y>=0 && y<board[0].length;
}
}