|
| 1 | +package org.sean.backtracking; |
| 2 | + |
| 3 | +import java.util.Deque; |
| 4 | +import java.util.LinkedList; |
| 5 | +import java.util.Queue; |
| 6 | + |
| 7 | +/*** |
| 8 | + * 542. 01 Matrix |
| 9 | + */ |
| 10 | +public class MatrixMinDistFinder { |
| 11 | + private int[][] moves = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; |
| 12 | + private boolean[][] visited; |
| 13 | + |
| 14 | + // Lesson learnt : for searching target path, normally BFS is way more efficient than DFS |
| 15 | + // (e.g. similar problem : WordLadder). |
| 16 | + |
| 17 | + // region Solution O(m*n) : BFS |
| 18 | + public int[][] updateMatrix(int[][] mat) { |
| 19 | + Deque<int[]> deque = new LinkedList<>(); |
| 20 | + int rowLen = mat.length; |
| 21 | + int colLen = mat[0].length; |
| 22 | + visited = new boolean[rowLen][colLen]; |
| 23 | + |
| 24 | + for (int i = 0; i < rowLen; i++) { |
| 25 | + for (int j = 0; j < colLen; j++) { |
| 26 | + if (mat[i][j] == 0) { |
| 27 | + visited[i][j] = true; |
| 28 | + deque.offer(new int[]{i, j}); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + search(mat, deque, rowLen, colLen); |
| 34 | + |
| 35 | + return mat; |
| 36 | + } |
| 37 | + |
| 38 | + private void search(int[][] mat, Queue<int[]> deque, int rowLen, int colLen) { |
| 39 | + int level = 0; |
| 40 | + |
| 41 | + while (!deque.isEmpty()) { |
| 42 | + int sz = deque.size(); |
| 43 | + |
| 44 | + // ArrayList<int[]> list = new ArrayList<>(deque); |
| 45 | + // list.forEach(ints -> System.out.printf("(%d:%d) ", ints[0],ints[1])); |
| 46 | + // System.out.println("\n"); |
| 47 | + |
| 48 | + for (int k = 0; k < sz; k++) { |
| 49 | + int[] coord = deque.poll(); |
| 50 | + int currR = coord[0]; |
| 51 | + int currC = coord[1]; |
| 52 | + |
| 53 | + mat[currR][currC] = level; |
| 54 | + |
| 55 | + for (int[] mv : moves) { |
| 56 | + int nR = currR + mv[0]; |
| 57 | + int nC = currC + mv[1]; |
| 58 | + |
| 59 | + if (nR >= 0 && nR < rowLen && nC >= 0 && nC < colLen && !visited[nR][nC]) { |
| 60 | + visited[nR][nC] = true; |
| 61 | + deque.offer(new int[]{nR, nC}); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + level++; |
| 67 | + } |
| 68 | + } |
| 69 | + // endregion |
| 70 | + |
| 71 | + // region Solution O(m*n*4^(m*n)) : DFS - TLE! |
| 72 | + public int[][] updateMatrix0(int[][] mat) { |
| 73 | + int rowCnt = mat.length; |
| 74 | + int colCnt = mat[0].length; |
| 75 | + |
| 76 | + visited = new boolean[rowCnt][colCnt]; |
| 77 | + copy = new int[rowCnt][colCnt]; |
| 78 | + |
| 79 | + for (int i = 0; i < rowCnt; i++) { |
| 80 | + for (int j = 0; j < colCnt; j++) { |
| 81 | + if (mat[i][j] == 1) { |
| 82 | + visited[i][j] = true; |
| 83 | + copy[i][j] = traverse(mat, i, j, 0); |
| 84 | + visited[i][j] = false; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return copy; |
| 90 | + } |
| 91 | + |
| 92 | + int[][] copy; |
| 93 | + |
| 94 | + private int traverse(int[][] mat, int i, int j, int len) { |
| 95 | + int rowCnt = mat.length; |
| 96 | + int colCnt = mat[0].length; |
| 97 | + |
| 98 | + if (mat[i][j] == 0) |
| 99 | + return len; |
| 100 | + |
| 101 | + int increased = -1; |
| 102 | + for (int[] move : moves) { |
| 103 | + int nR = i + move[0]; |
| 104 | + int nC = j + move[1]; |
| 105 | + |
| 106 | + if (nR >= 0 && nR < rowCnt && nC >= 0 && nC < colCnt) { |
| 107 | + if (!visited[nR][nC]) { |
| 108 | + visited[nR][nC] = true; |
| 109 | + |
| 110 | + if (mat[i][j] == 1) { |
| 111 | + int offset = traverse(mat, nR, nC, len + 1) - len; |
| 112 | + if (increased < 0) { |
| 113 | + increased = offset; |
| 114 | + } else { |
| 115 | + increased = Math.min(increased, offset); |
| 116 | + } |
| 117 | + } else |
| 118 | + return len; |
| 119 | + |
| 120 | + visited[nR][nC] = false; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return len + Math.max(increased, 0); |
| 125 | + } |
| 126 | + |
| 127 | + // endregion |
| 128 | +} |
0 commit comments