|
| 1 | +package com.github.chen0040.leetcode.day20.medium; |
| 2 | + |
| 3 | + |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Queue; |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * Created by xschen on 15/8/2017. |
| 12 | + * |
| 13 | + * link: https://leetcode.com/problems/pacific-atlantic-water-flow/description/ |
| 14 | + */ |
| 15 | +public class PacificAtlanticWaterFlow { |
| 16 | + public class Solution { |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + public List<int[]> pacificAtlantic(int[][] matrix) { |
| 23 | + int rowCount =matrix.length; |
| 24 | + if(rowCount == 0) return new ArrayList<int[]>(); |
| 25 | + int colCount = matrix[0].length; |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + int totalCount = rowCount * colCount; |
| 30 | + int[] types = new int[totalCount]; |
| 31 | + for(int i=0; i < rowCount; ++i) { |
| 32 | + types[i * colCount] = 1; |
| 33 | + types[i * colCount + colCount-1] = 2; |
| 34 | + } |
| 35 | + for(int i=0; i < colCount; ++i) { |
| 36 | + types[i] = 1; |
| 37 | + types[(rowCount-1) * colCount + i] = 2; |
| 38 | + } |
| 39 | + |
| 40 | + List<int[]> res = new ArrayList<int[]>(); |
| 41 | + types[colCount - 1] = 3; |
| 42 | + types[(rowCount-1) * colCount] = 3; |
| 43 | + |
| 44 | + if(rowCount != 1 && colCount != 1) { |
| 45 | + res.add(new int[] { 0, colCount-1}); |
| 46 | + res.add(new int[] { rowCount-1, 0}); |
| 47 | + } |
| 48 | + else if(rowCount == 1) { |
| 49 | + for(int i=0; i < colCount; ++i) { |
| 50 | + res.add(new int[] { 0, i }); |
| 51 | + } |
| 52 | + return res; |
| 53 | + } else if(colCount == 1) { |
| 54 | + for(int i=0; i < rowCount; ++i) { |
| 55 | + res.add(new int[] { i, 0}); |
| 56 | + } |
| 57 | + return res; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + boolean[] can_reach_pacific = new boolean[totalCount]; |
| 62 | + boolean[] can_reach_atlantic = new boolean[totalCount]; |
| 63 | + for(int i=0; i < totalCount; ++i) { |
| 64 | + if(types[i] == 3) continue; |
| 65 | + boolean[] marked = new boolean[totalCount]; |
| 66 | + |
| 67 | + Queue<Integer> queue = new LinkedList<Integer>(); |
| 68 | + queue.add(i); |
| 69 | + boolean reach_pacific = false; |
| 70 | + boolean reach_atlantic = false; |
| 71 | + while(!queue.isEmpty()) { |
| 72 | + int v = queue.remove(); |
| 73 | + marked[v] = true; |
| 74 | + int row = v / colCount; |
| 75 | + int col = v % colCount; |
| 76 | + if(can_reach_pacific[v]) reach_pacific = true; |
| 77 | + if(can_reach_atlantic[v]) reach_atlantic = true; |
| 78 | + if(!(reach_pacific && reach_atlantic) || row == 0 || col == 0 || row == rowCount-1 || col == colCount - 1) { |
| 79 | + if(types[v] == 1) reach_pacific = true; |
| 80 | + else if(types[v] == 2) reach_atlantic = true; |
| 81 | + else if(types[v] == 3) { |
| 82 | + reach_pacific = true; |
| 83 | + reach_atlantic = true; |
| 84 | + } |
| 85 | + } |
| 86 | + if(reach_pacific) can_reach_pacific[i] = true; |
| 87 | + if(reach_atlantic) can_reach_atlantic[i] = true; |
| 88 | + if(reach_pacific && reach_atlantic) { |
| 89 | + types[i] = 3; |
| 90 | + res.add(new int[] { i / colCount, i % colCount }); |
| 91 | + break; |
| 92 | + } |
| 93 | + for(int ii=-1; ii <= 1; ++ii){ |
| 94 | + if(ii == 0) continue; |
| 95 | + int row_i = ii + row; |
| 96 | + int col_i = ii + col; |
| 97 | + if(row_i >= 0 && row_i < rowCount && matrix[row_i][col] <= matrix[row][col]) { |
| 98 | + int w = row_i * colCount + col; |
| 99 | + if(!marked[w]) { |
| 100 | + queue.add(w); |
| 101 | + } |
| 102 | + } |
| 103 | + if(col_i >= 0 && col_i < colCount && matrix[row][col_i] <= matrix[row][col]) { |
| 104 | + int w = row * colCount + col_i; |
| 105 | + if(!marked[w]) { |
| 106 | + queue.add(w); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return res; |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments