|
| 1 | +package DFS; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * Created by 周杰伦 on 2018/3/31. |
| 8 | + * 逆向思维。 |
| 9 | + * 先把靠着大西洋的两条边灌满大西洋水。 |
| 10 | + * 再把靠着太平洋的两条边灌满太平洋水。 |
| 11 | + * 然后往依次灌水,wet和visit表示已灌水和已遍历。 |
| 12 | + * 最后判断两种水都有的点极为所求。 |
| 13 | + * 妙啊,妙啊。 |
| 14 | + */ |
| 15 | +public class 太平洋和大西洋灌水 { |
| 16 | + public List<int[]> pacificAtlantic(int[][] matrix) { |
| 17 | + List<int[]> result = new ArrayList<int[]>(); |
| 18 | + if(matrix.length == 0 || matrix[0].length == 0) return result; |
| 19 | + boolean[][] pacific = new boolean[matrix.length][matrix[0].length]; // the pacific boolean table |
| 20 | + boolean[][] atlantic = new boolean[matrix.length][matrix[0].length]; // the atlantic booean table |
| 21 | + //initially, all the top and left cells are flooded with pacific water |
| 22 | + //and all the right and bottom cells are flooded with atlantic water |
| 23 | + for(int i = 0; i < matrix.length; i++){ |
| 24 | + pacific[i][0] = true; |
| 25 | + atlantic[i][matrix[0].length-1] = true; |
| 26 | + } |
| 27 | + for(int i = 0; i < matrix[0].length; i++){ |
| 28 | + pacific[0][i] = true; |
| 29 | + atlantic[matrix.length-1][i] = true; |
| 30 | + } |
| 31 | + //we go around the matrix and try to flood the matrix from 4 side. |
| 32 | + for(int i = 0; i < matrix.length; i++){ |
| 33 | + boolean[][] pacificVisited = new boolean[matrix.length][matrix[0].length]; |
| 34 | + boolean[][] atlanticVisited = new boolean[matrix.length][matrix[0].length]; |
| 35 | + water(pacific, pacificVisited, matrix, i,0); |
| 36 | + water(atlantic, atlanticVisited, matrix, i, matrix[0].length - 1); |
| 37 | + } |
| 38 | + for(int i = 0; i < matrix[0].length; i++){ |
| 39 | + boolean[][] pacificVisited = new boolean[matrix.length][matrix[0].length]; |
| 40 | + boolean[][] atlanticVisited = new boolean[matrix.length][matrix[0].length]; |
| 41 | + water(pacific, pacificVisited, matrix, 0,i); |
| 42 | + water(atlantic, atlanticVisited, matrix, matrix.length - 1, i); |
| 43 | + } |
| 44 | + //check the shared points among 2 tables |
| 45 | + for(int i = 0; i < matrix.length; i++){ |
| 46 | + for(int j = 0; j < matrix[0].length; j++){ |
| 47 | + if(pacific[i][j] && atlantic[i][j]){ |
| 48 | + int[] element = {i,j}; |
| 49 | + result.add(element); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + return result; |
| 54 | + } |
| 55 | + //the flood function |
| 56 | + private void water(boolean[][] wet, boolean[][] visited, int[][] matrix, int i , int j){ |
| 57 | + wet[i][j] = true; |
| 58 | + visited[i][j] = true; |
| 59 | + int[] x = {0,0,1,-1}; |
| 60 | + int[] y = {1,-1,0,0}; |
| 61 | + for(int k = 0; k < 4; k++){ |
| 62 | + if(i+y[k] >= 0 && i+y[k] < matrix.length && j+x[k] >= 0 && j+x[k] < matrix[0].length |
| 63 | + && !visited[i+y[k]][j+x[k]] && matrix[i+y[k]][j+x[k]] >= matrix[i][j]){ |
| 64 | + water(wet, visited, matrix, i+y[k], j+x[k]); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments