(int row, int col, int matrix[][])
| 4 | int dir[][] = {{-1,0},{0,1},{1,0},{0,-1}}; |
| 5 | //visit all nodes of a component using DFS |
| 6 | public void dfs(int row, int col, int matrix[][]){ |
| 7 | //out of bound, already visited, obstacle |
| 8 | if(row<0 || row>=rows || col<0 || col>=cols || matrix[row][col]==1){ |
| 9 | return; |
| 10 | } |
| 11 | matrix[row][col] = 1; |
| 12 | for(int i=0;i<4;i++){ |
| 13 | dfs(row + dir[i][0], col + dir[i][1], matrix); |
| 14 | } |
| 15 | } |
| 16 | public int regionsBySlashes(String[] grid) { |
| 17 | //create a 3*3 grid |
| 18 | int size = grid.length; |