An n x n grid is composed of 1 x 1 squares where each 1 x 1 square consists of a '/', '\', or blank space ' '. These characters divide the square into contiguous regions.
Given the grid grid represented as a string array, return the number of regions.
Note that backslash characters are escaped, so a '\' is represented as '\\'.
Example 1:
Input: grid = [" /","/ "] Output: 2
Example 2:
Input: grid = [" /"," "] Output: 1
Example 3:
Input: grid = ["/\\","\\/"] Output: 5 Explanation: Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.
Constraints:
n == grid.length == grid[i].length1 <= n <= 30grid[i][j] is either '/', '\', or ' '.class Solution {
private static int[][] blank = {
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 }
};
private static int[][] fwdSlash = {
{ 0, 0, 1 },
{ 0, 1, 0 },
{ 1, 0, 0 }
};;
private static int[][] backSlash = {
{ 1, 0, 0 },
{ 0, 1, 0 },
{ 0, 0, 1 }
};
public int regionsBySlashes(String[] grid) {
int n = grid.length, regionCt = 0;
int[][] region = new int[3 * n][3 * n];
for (int i = 0; i < n; i++) {
char[] cs = grid[i].toCharArray();
for (int j = 0; j < n; j++)
populateArea(region, cs[j], 3 * i, 3 * j);
}
n *= 3;
// for (int i = 0; i < n; i++)
// System.out.println(Arrays.toString(region[i]));
// System.out.println();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (region[i][j] == 0) {
fillRegion(region, n, i, j);
regionCt++;
}
}
}
return regionCt;
}
private void populateArea(int[][] area, char c, int rowStart, int colStart) {
int[][] cloner = switch (c) {
case ' ' -> blank;
case '/' -> fwdSlash;
case '\\' -> backSlash;
default -> blank;
};
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
area[rowStart + i][colStart + j] = cloner[i][j];
// System.out.println(Arrays.toString(area));
}
private void fillRegion(int[][] region, int n, int i, int j) {
if (Math.min(i, j) < 0 || Math.max(i, j) >= n || region[i][j] == 1)
return;
region[i][j] = 1;
fillRegion(region, n, i - 1, j);
fillRegion(region, n, i + 1, j);
fillRegion(region, n, i, j - 1);
fillRegion(region, n, i, j + 1);
}
}