|
| 1 | +package problems.ongraph; |
| 2 | + |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Queue; |
| 6 | + |
| 7 | +public class MatrixEnclosedRegions { |
| 8 | + |
| 9 | + public static void fillSurroundedRegions(List<List<Character>> board) { |
| 10 | + |
| 11 | + // Identifies the regions that are reachable via white path starting from |
| 12 | + // the first or last columns. |
| 13 | + for (int i = 0; i < board.size(); ++i) { |
| 14 | + markBoundaryRegion(i, 0, board); |
| 15 | + markBoundaryRegion(i, board.get(i).size() - 1, board); |
| 16 | + } |
| 17 | + // Identifies the regions that are reachable via white path starting from |
| 18 | + // the first or last rows. |
| 19 | + for (int j = 0; j < board.get(0).size(); ++j) { |
| 20 | + markBoundaryRegion(0, j, board); |
| 21 | + markBoundaryRegion(board.size() - 1, j, board); |
| 22 | + } |
| 23 | + |
| 24 | + // Marks the surrounded white regions as black. |
| 25 | + for (int i = 0; i < board.size(); ++i) { |
| 26 | + for (int j = 0; j < board.get(i).size(); ++j) { |
| 27 | + board.get(i).set(j, board.get(i).get(j) != 'T' ? 'B' : 'W'); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private static class Coordinate { |
| 33 | + public Integer x; |
| 34 | + public Integer y; |
| 35 | + |
| 36 | + public Coordinate(Integer x, Integer y) { |
| 37 | + this.x = x; |
| 38 | + this.y = y; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private static void markBoundaryRegion(int i, int j, |
| 43 | + List<List<Character>> board) { |
| 44 | + Queue<Coordinate> q = new ArrayDeque<>(); |
| 45 | + q.add(new Coordinate(i, j)); |
| 46 | + // Uses BFS to traverse this region. |
| 47 | + while (!q.isEmpty()) { |
| 48 | + Coordinate curr = q.poll(); |
| 49 | + if (curr.x >= 0 && curr.x < board.size() && curr.y >= 0 && |
| 50 | + curr.y < board.get(curr.x).size() && |
| 51 | + board.get(curr.x).get(curr.y) == 'W') { |
| 52 | + board.get(curr.x).set(curr.y, 'T'); |
| 53 | + q.add(new Coordinate(curr.x - 1, curr.y)); |
| 54 | + q.add(new Coordinate(curr.x + 1, curr.y)); |
| 55 | + q.add(new Coordinate(curr.x, curr.y - 1)); |
| 56 | + q.add(new Coordinate(curr.x, curr.y + 1)); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + /** |
| 61 | + * This problem is concerned with computing regions within a 2D grid that are enclosed. |
| 62 | + * See Figure 19.7 for an illustration of the problem. |
| 63 | + * Figure 19.7: Three of the four white squares in (a) are enclosed, i.e., there is no path from any of |
| 64 | + * them to the boundary that only passes through white squares, (b) shows the white squares that are not |
| 65 | + * enclosed. |
| 66 | + * The computational problem can be formalized using 2D arrays of Bs (blacks) and |
| 67 | + * Ws (whites). Figure 19.7(a) is encoded by |
| 68 | + * B B B B |
| 69 | + * W B W B |
| 70 | + * B W W B |
| 71 | + * ' |
| 72 | + * B B B B |
| 73 | + * 359 |
| 74 | + * Figure 19.7(b) on the previous page is encoded by |
| 75 | + * B B B B |
| 76 | + * W B B B |
| 77 | + * B B B B |
| 78 | + * ' |
| 79 | + * B B B B |
| 80 | + * Let A be a 2D array whose entries are either WorB. Write a program that takes A, |
| 81 | + * and replaces all Ws that cannot reach the boundary with a B. |
| 82 | + * |
| 83 | + * |
| 84 | + *It is easier to focus on the inverse problem, namely identifying Ws that can |
| 85 | + * reach the boundary. The reason that the inverse is simpler is that if a W is adjacent |
| 86 | + * to a W that is can reach the boundary, then the first W can reach it too. The Ws on |
| 87 | + * the boundary are the initial set. Subsequently, we find Ws neighboring the boundary |
| 88 | + * Ws, and iteratively grow the set. Whenever we find a new W that can reach the |
| 89 | + * boundary, we need to record it, and at some stage search for new Ws from it. A queue |
| 90 | + * is a reasonable data structure to track Ws to be processed. The approach amounts to |
| 91 | + * breadth-first search starting with a set of vertices rather than a single vertex. |
| 92 | + */ |
| 93 | +} |
0 commit comments