Skip to content

Commit 60ee431

Browse files
authored
Create 959. Regions Cut By Slashes
1 parent ea46d52 commit 60ee431

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

959. Regions Cut By Slashes

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
private:
3+
vector<pair<int, int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
4+
unordered_map<char, vector<vector<int>>> mp = {
5+
{'/', {{0, 0, 1}, {0, 1, 0}, {1, 0, 0}}},
6+
{'\\', {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}},
7+
{' ', {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}}
8+
};
9+
vector<vector<int>> zoom(vector<string>& grid){
10+
int n = grid.size();
11+
vector<vector<int>> res(3*n, vector<int>(3*n, 0));
12+
for(int i = 0; i < n; i++){
13+
for(int j = 0; j < n; j++){
14+
for(int r = 0; r < 3; r++){
15+
for(int c = 0; c < 3; c++){
16+
res[3*i+r][3*j+c] = mp[grid[i][j]][r][c];
17+
}
18+
}
19+
}
20+
}
21+
return res;
22+
}
23+
void markRegion(vector<vector<int>>& grid, int r, int c){
24+
if(grid[r][c] == -1 || grid[r][c] == 1) return;
25+
grid[r][c] = -1;
26+
int n = grid.size();
27+
for(auto& [x, y]: dirs){
28+
int new_x = r + x, new_y = c + y;
29+
if(new_x >= 0 && new_y >= 0 && new_x < n && new_y < n) markRegion(grid, new_x, new_y);
30+
}
31+
}
32+
public:
33+
int regionsBySlashes(vector<string>& grid) {
34+
int cnt = 0;
35+
vector<vector<int>> zoomed = zoom(grid);
36+
int n = zoomed.size();
37+
for(int i = 0; i < n; i++){
38+
for(int j = 0; j < n; j++){
39+
if(zoomed[i][j] == -1 || zoomed[i][j] == 1) continue;
40+
cnt++;
41+
markRegion(zoomed, i, j);
42+
}
43+
}
44+
return cnt;
45+
}
46+
};

0 commit comments

Comments
 (0)