剑指 Offer 12_II. 矩阵中的最大面积
题目描述
[[".",".","*","*"],
[".","*","*","."],
["*",".","*","."]]
.
代表海洋面积
*
代表陆地面积
求连成片的最大陆地面积
解题思路:
该题是https://blog.csdn.net/sqc157400661/article/details/140838769的变形,解题思路类似。本题将不在赘述。
代码:
go
package main
import "fmt"
var sum int
func main(){
board := [][]string{
{"*",".",".","*"},
{"*","*",".","."},
{"*",".","*","*"},
{".","*","*","."},
}
fmt.Println(board)
rows := len(board)
cols := len(board[0])
max := 0
for i:=0; i < rows; i++ {
for j := 0; j < cols; j++ {
sum = 0
tmp := dfsSum(&board,i,j)
if tmp > max {
max = sum
}
}
}
fmt.Println(max)
fmt.Println(board)
}
func dfsSum(board *[][]string, i, j int) int {
rows := len(*board)
cols := len((*board)[0])
if i<0 || j<0|| i>= rows || j >= cols || (*board)[i][j] != "*"{
return 0
}
sum++
(*board)[i][j] = "-"
fmt.Println(*board)
// 右边
dfsSum(board,i,j+1)
// 左边
dfsSum(board,i,j-1)
// 下面
dfsSum(board,i+1,j)
(*board)[i][j] = "*"
return sum
}