diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/README.md b/solution/0900-0999/0959.Regions Cut By Slashes/README.md index 313507c727c20..ec9f7d2195bb4 100644 --- a/solution/0900-0999/0959.Regions Cut By Slashes/README.md +++ b/solution/0900-0999/0959.Regions Cut By Slashes/README.md @@ -279,6 +279,57 @@ func regionsBySlashes(grid []string) int { } ``` +#### TypeScript + +```ts +function regionsBySlashes(grid: string[]): number { + const find = (x: number) => { + if (p[x] !== x) { + p[x] = find(p[x]); + } + return p[x]; + }; + + const union = (a: number, b: number) => { + const pa = find(a); + const pb = find(b); + if (pa !== pb) { + p[pa] = pb; + size--; + } + }; + + const n = grid.length; + let size = n * n * 4; + const p = Array.from({ length: size }, (_, i) => i); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const k = i * n + j; + if (i < n - 1) { + union(4 * k + 2, (k + n) * 4); + } + if (j < n - 1) { + union(4 * k + 1, (k + 1) * 4 + 3); + } + if (grid[i][j] === '/') { + union(4 * k, 4 * k + 3); + union(4 * k + 1, 4 * k + 2); + } else if (grid[i][j] === '\\') { + union(4 * k, 4 * k + 1); + union(4 * k + 2, 4 * k + 3); + } else { + union(4 * k, 4 * k + 1); + union(4 * k + 1, 4 * k + 2); + union(4 * k + 2, 4 * k + 3); + } + } + } + + return size; +} +``` + #### JavaScript ```js @@ -339,4 +390,166 @@ function regionsBySlashes(grid) { + + +### 方法二:DFS + + + +#### TypeScript + +```ts +function regionsBySlashes(grid: string[]): number { + const createGraph = () => { + const n = grid.length; + const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0)); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const [y, x] = [i * 2, j * 2]; + + switch (grid[i][j]) { + case '/': + g[y][x] = g[y + 1][x + 1] = 0; + g[y][x + 1] = g[y + 1][x] = 1; + break; + + case '\\': + g[y][x] = g[y + 1][x + 1] = 2; + g[y][x + 1] = g[y + 1][x] = 0; + break; + + default: + g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0; + break; + } + } + } + + return g; + }; + + const isValid = (x: number) => 0 <= x && x < n; + const dfs = (i: number, j: number) => { + if (!isValid(i) || !isValid(j) || g[i][j]) return; + + g[i][j] = -1; + const dirs = [-1, 0, 1, 0, -1]; + const neighbours: number[] = []; + + for (let d = 0; d < 4; d++) { + const [y, x] = [i + dirs[d], j + dirs[d + 1]]; + + if (isValid(y) && isValid(x)) { + dfs(y, x); + neighbours.push(g[y][x]); + } else { + neighbours.push(-1); + } + } + + const [top, right, bottom, left] = neighbours; + if (top === 1 && right === 1) dfs(i - 1, j + 1); + if (bottom === 1 && left === 1) dfs(i + 1, j - 1); + if (top === 2 && left === 2) dfs(i - 1, j - 1); + if (bottom === 2 && right === 2) dfs(i + 1, j + 1); + }; + + const g = createGraph(); + const n = g.length; + let res = 0; + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + if (g[i][j] === 0) { + dfs(i, j); + res++; + } + } + } + + return res; +} +``` + +#### JavaScript + +```js +function regionsBySlashes(grid) { + const createGraph = () => { + const n = grid.length; + const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0)); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const [y, x] = [i * 2, j * 2]; + + switch (grid[i][j]) { + case '/': + g[y][x] = g[y + 1][x + 1] = 0; + g[y][x + 1] = g[y + 1][x] = 1; + break; + + case '\\': + g[y][x] = g[y + 1][x + 1] = 2; + g[y][x + 1] = g[y + 1][x] = 0; + break; + + default: + g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0; + break; + } + } + } + + return g; + }; + + const isValid = x => 0 <= x && x < n; + const dfs = (i, j) => { + if (!isValid(i) || !isValid(j) || g[i][j]) return; + + g[i][j] = -1; + const dirs = [-1, 0, 1, 0, -1]; + const neighbours = []; + + for (let d = 0; d < 4; d++) { + const [y, x] = [i + dirs[d], j + dirs[d + 1]]; + + if (isValid(y) && isValid(x)) { + dfs(y, x); + neighbours.push(g[y][x]); + } else { + neighbours.push(-1); + } + } + + const [top, right, bottom, left] = neighbours; + if (top === 1 && right === 1) dfs(i - 1, j + 1); + if (bottom === 1 && left === 1) dfs(i + 1, j - 1); + if (top === 2 && left === 2) dfs(i - 1, j - 1); + if (bottom === 2 && right === 2) dfs(i + 1, j + 1); + }; + + const g = createGraph(); + const n = g.length; + let res = 0; + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + if (g[i][j] === 0) { + dfs(i, j); + res++; + } + } + } + + return res; +} +``` + + + + + diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md b/solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md index 33990633ac8fb..20202fba3a0bc 100644 --- a/solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md +++ b/solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md @@ -65,7 +65,7 @@ tags: -### Solution 1 +### Solution 1: Union-Find @@ -269,6 +269,57 @@ func regionsBySlashes(grid []string) int { } ``` +#### TypeScript + +```ts +function regionsBySlashes(grid: string[]): number { + const find = (x: number) => { + if (p[x] !== x) { + p[x] = find(p[x]); + } + return p[x]; + }; + + const union = (a: number, b: number) => { + const pa = find(a); + const pb = find(b); + if (pa !== pb) { + p[pa] = pb; + size--; + } + }; + + const n = grid.length; + let size = n * n * 4; + const p = Array.from({ length: size }, (_, i) => i); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const k = i * n + j; + if (i < n - 1) { + union(4 * k + 2, (k + n) * 4); + } + if (j < n - 1) { + union(4 * k + 1, (k + 1) * 4 + 3); + } + if (grid[i][j] === '/') { + union(4 * k, 4 * k + 3); + union(4 * k + 1, 4 * k + 2); + } else if (grid[i][j] === '\\') { + union(4 * k, 4 * k + 1); + union(4 * k + 2, 4 * k + 3); + } else { + union(4 * k, 4 * k + 1); + union(4 * k + 1, 4 * k + 2); + union(4 * k + 2, 4 * k + 3); + } + } + } + + return size; +} +``` + #### JavaScript ```js @@ -329,4 +380,166 @@ function regionsBySlashes(grid) { + + +### Solution 2: DFS + + + +#### TypeScript + +```ts +function regionsBySlashes(grid: string[]): number { + const createGraph = () => { + const n = grid.length; + const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0)); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const [y, x] = [i * 2, j * 2]; + + switch (grid[i][j]) { + case '/': + g[y][x] = g[y + 1][x + 1] = 0; + g[y][x + 1] = g[y + 1][x] = 1; + break; + + case '\\': + g[y][x] = g[y + 1][x + 1] = 2; + g[y][x + 1] = g[y + 1][x] = 0; + break; + + default: + g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0; + break; + } + } + } + + return g; + }; + + const isValid = (x: number) => 0 <= x && x < n; + const dfs = (i: number, j: number) => { + if (!isValid(i) || !isValid(j) || g[i][j]) return; + + g[i][j] = -1; + const dirs = [-1, 0, 1, 0, -1]; + const neighbours: number[] = []; + + for (let d = 0; d < 4; d++) { + const [y, x] = [i + dirs[d], j + dirs[d + 1]]; + + if (isValid(y) && isValid(x)) { + dfs(y, x); + neighbours.push(g[y][x]); + } else { + neighbours.push(-1); + } + } + + const [top, right, bottom, left] = neighbours; + if (top === 1 && right === 1) dfs(i - 1, j + 1); + if (bottom === 1 && left === 1) dfs(i + 1, j - 1); + if (top === 2 && left === 2) dfs(i - 1, j - 1); + if (bottom === 2 && right === 2) dfs(i + 1, j + 1); + }; + + const g = createGraph(); + const n = g.length; + let res = 0; + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + if (g[i][j] === 0) { + dfs(i, j); + res++; + } + } + } + + return res; +} +``` + +#### JavaScript + +```js +function regionsBySlashes(grid) { + const createGraph = () => { + const n = grid.length; + const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0)); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const [y, x] = [i * 2, j * 2]; + + switch (grid[i][j]) { + case '/': + g[y][x] = g[y + 1][x + 1] = 0; + g[y][x + 1] = g[y + 1][x] = 1; + break; + + case '\\': + g[y][x] = g[y + 1][x + 1] = 2; + g[y][x + 1] = g[y + 1][x] = 0; + break; + + default: + g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0; + break; + } + } + } + + return g; + }; + + const isValid = x => 0 <= x && x < n; + const dfs = (i, j) => { + if (!isValid(i) || !isValid(j) || g[i][j]) return; + + g[i][j] = -1; + const dirs = [-1, 0, 1, 0, -1]; + const neighbours = []; + + for (let d = 0; d < 4; d++) { + const [y, x] = [i + dirs[d], j + dirs[d + 1]]; + + if (isValid(y) && isValid(x)) { + dfs(y, x); + neighbours.push(g[y][x]); + } else { + neighbours.push(-1); + } + } + + const [top, right, bottom, left] = neighbours; + if (top === 1 && right === 1) dfs(i - 1, j + 1); + if (bottom === 1 && left === 1) dfs(i + 1, j - 1); + if (top === 2 && left === 2) dfs(i - 1, j - 1); + if (bottom === 2 && right === 2) dfs(i + 1, j + 1); + }; + + const g = createGraph(); + const n = g.length; + let res = 0; + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + if (g[i][j] === 0) { + dfs(i, j); + res++; + } + } + } + + return res; +} +``` + + + + + diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/Solution.ts b/solution/0900-0999/0959.Regions Cut By Slashes/Solution.ts new file mode 100644 index 0000000000000..c8019d2ffd829 --- /dev/null +++ b/solution/0900-0999/0959.Regions Cut By Slashes/Solution.ts @@ -0,0 +1,46 @@ +function regionsBySlashes(grid: string[]): number { + const find = (x: number) => { + if (p[x] !== x) { + p[x] = find(p[x]); + } + return p[x]; + }; + + const union = (a: number, b: number) => { + const pa = find(a); + const pb = find(b); + if (pa !== pb) { + p[pa] = pb; + size--; + } + }; + + const n = grid.length; + let size = n * n * 4; + const p = Array.from({ length: size }, (_, i) => i); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const k = i * n + j; + if (i < n - 1) { + union(4 * k + 2, (k + n) * 4); + } + if (j < n - 1) { + union(4 * k + 1, (k + 1) * 4 + 3); + } + if (grid[i][j] === '/') { + union(4 * k, 4 * k + 3); + union(4 * k + 1, 4 * k + 2); + } else if (grid[i][j] === '\\') { + union(4 * k, 4 * k + 1); + union(4 * k + 2, 4 * k + 3); + } else { + union(4 * k, 4 * k + 1); + union(4 * k + 1, 4 * k + 2); + union(4 * k + 2, 4 * k + 3); + } + } + } + + return size; +} diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/Solution2.js b/solution/0900-0999/0959.Regions Cut By Slashes/Solution2.js new file mode 100644 index 0000000000000..abe11132ffa51 --- /dev/null +++ b/solution/0900-0999/0959.Regions Cut By Slashes/Solution2.js @@ -0,0 +1,71 @@ +function regionsBySlashes(grid) { + const createGraph = () => { + const n = grid.length; + const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0)); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const [y, x] = [i * 2, j * 2]; + + switch (grid[i][j]) { + case '/': + g[y][x] = g[y + 1][x + 1] = 0; + g[y][x + 1] = g[y + 1][x] = 1; + break; + + case '\\': + g[y][x] = g[y + 1][x + 1] = 2; + g[y][x + 1] = g[y + 1][x] = 0; + break; + + default: + g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0; + break; + } + } + } + + return g; + }; + + const isValid = x => 0 <= x && x < n; + const dfs = (i, j) => { + if (!isValid(i) || !isValid(j) || g[i][j]) return; + + g[i][j] = -1; + const dirs = [-1, 0, 1, 0, -1]; + const neighbours = []; + + for (let d = 0; d < 4; d++) { + const [y, x] = [i + dirs[d], j + dirs[d + 1]]; + + if (isValid(y) && isValid(x)) { + dfs(y, x); + neighbours.push(g[y][x]); + } else { + neighbours.push(-1); + } + } + + const [top, right, bottom, left] = neighbours; + if (top === 1 && right === 1) dfs(i - 1, j + 1); + if (bottom === 1 && left === 1) dfs(i + 1, j - 1); + if (top === 2 && left === 2) dfs(i - 1, j - 1); + if (bottom === 2 && right === 2) dfs(i + 1, j + 1); + }; + + const g = createGraph(); + const n = g.length; + let res = 0; + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + if (g[i][j] === 0) { + dfs(i, j); + res++; + } + } + } + + return res; +} diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/Solution2.ts b/solution/0900-0999/0959.Regions Cut By Slashes/Solution2.ts new file mode 100644 index 0000000000000..7be3d87fcccb3 --- /dev/null +++ b/solution/0900-0999/0959.Regions Cut By Slashes/Solution2.ts @@ -0,0 +1,71 @@ +function regionsBySlashes(grid: string[]): number { + const createGraph = () => { + const n = grid.length; + const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0)); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const [y, x] = [i * 2, j * 2]; + + switch (grid[i][j]) { + case '/': + g[y][x] = g[y + 1][x + 1] = 0; + g[y][x + 1] = g[y + 1][x] = 1; + break; + + case '\\': + g[y][x] = g[y + 1][x + 1] = 2; + g[y][x + 1] = g[y + 1][x] = 0; + break; + + default: + g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0; + break; + } + } + } + + return g; + }; + + const isValid = (x: number) => 0 <= x && x < n; + const dfs = (i: number, j: number) => { + if (!isValid(i) || !isValid(j) || g[i][j]) return; + + g[i][j] = -1; + const dirs = [-1, 0, 1, 0, -1]; + const neighbours: number[] = []; + + for (let d = 0; d < 4; d++) { + const [y, x] = [i + dirs[d], j + dirs[d + 1]]; + + if (isValid(y) && isValid(x)) { + dfs(y, x); + neighbours.push(g[y][x]); + } else { + neighbours.push(-1); + } + } + + const [top, right, bottom, left] = neighbours; + if (top === 1 && right === 1) dfs(i - 1, j + 1); + if (bottom === 1 && left === 1) dfs(i + 1, j - 1); + if (top === 2 && left === 2) dfs(i - 1, j - 1); + if (bottom === 2 && right === 2) dfs(i + 1, j + 1); + }; + + const g = createGraph(); + const n = g.length; + let res = 0; + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + if (g[i][j] === 0) { + dfs(i, j); + res++; + } + } + } + + return res; +}