| 57 | } |
| 58 | |
| 59 | public static SquareCell[][] processSquare(int[][] matrix) { |
| 60 | SquareCell[][] processed = new SquareCell[matrix.length][matrix.length]; |
| 61 | |
| 62 | for (int r = matrix.length - 1; r >= 0; r--) { |
| 63 | for (int c = matrix.length - 1; c >= 0; c--) { |
| 64 | int rightZeros = 0; |
| 65 | int belowZeros = 0; |
| 66 | if (matrix[r][c] == 0) { // only need to process if it's a black cell |
| 67 | rightZeros++; |
| 68 | belowZeros++; |
| 69 | if (c + 1 < matrix.length) { // next column over is on same row |
| 70 | SquareCell previous = processed[r][c + 1]; |
| 71 | rightZeros += previous.zerosRight; |
| 72 | } |
| 73 | if (r + 1 < matrix.length) { |
| 74 | SquareCell previous = processed[r + 1][c]; |
| 75 | belowZeros += previous.zerosBelow; |
| 76 | } |
| 77 | } |
| 78 | processed[r][c] = new SquareCell(rightZeros, belowZeros); |
| 79 | } |
| 80 | } |
| 81 | return processed; |
| 82 | } |
| 83 | |
| 84 | public static void main(String[] args) { |
| 85 | int[][] matrix = AssortedMethods.randomMatrix(7, 7, 0, 1); |