Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 73c0661

Browse files
authored
Update 0130.被围绕的区域.md
1 parent 9928779 commit 73c0661

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

‎problems/0130.被围绕的区域.md‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,58 @@ function solve(board) {
561561
}
562562
```
563563

564+
### Go
565+
566+
```dfs
567+
var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}}
568+
569+
func solve(board [][]byte) {
570+
rows, cols := len(board), len(board[0])
571+
// 列
572+
for i := 0; i < rows; i++ {
573+
if board[i][0] == 'O' {
574+
dfs(board, i, 0)
575+
}
576+
if board[i][cols-1] == 'O' {
577+
dfs(board, i, cols-1)
578+
}
579+
}
580+
// 行
581+
for j := 0; j < cols; j++ {
582+
if board[0][j] == 'O' {
583+
dfs(board, 0, j)
584+
}
585+
if board[rows-1][j] == 'O' {
586+
dfs(board, rows-1, j)
587+
}
588+
}
589+
590+
for _, r := range board {
591+
for j, c := range r {
592+
if c == 'A' {
593+
r[j] = 'O'
594+
}
595+
if c == 'O' {
596+
r[j] = 'X'
597+
}
598+
}
599+
}
600+
}
601+
602+
func dfs(board [][]byte, i, j int) {
603+
board[i][j] = 'A'
604+
for _, d := range DIRECTIONS {
605+
x, y := i+d[0], j+d[1]
606+
if x < 0 || x >= len(board) || y < 0 || y >= len(board[0]) {
607+
continue
608+
}
609+
if board[x][y] == 'O' {
610+
dfs(board, x, y)
611+
}
612+
}
613+
}
614+
```
615+
564616
<p align="center">
565617
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
566618
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /