|
楼主 |
发表于 2024-10-11 10:27
|
显示全部楼层
原代码稍微改一下
Dim s&
Sub test()
ar = Range("a1").CurrentRegion
c = 0
For i = 1 To UBound(ar)
For j = 1 To UBound(ar, 2)
If ar(i, j) > 0 Then
c = c + 1
s = 0
Call dfs(ar, i, j)
Cells(c, "x") = s
End If
Next
Next
MsgBox c
End Sub
Sub dfs(ar, row, col)
'If row = 0 Or row = UBound(ar) + 1 Or col = 0 Or col = UBound(ar, 2) + 1 Then
' Exit Sub
'End If
If ar(row, col) = 0 Then
Exit Sub
End If
s = s + 1
ar(row, col) = 0
Call dfs(ar, row + 1, col)
Call dfs(ar, row - 1, col)
Call dfs(ar, row, col + 1)
Call dfs(ar, row, col - 1)
Call dfs(ar, row + 1, col - 1)
Call dfs(ar, row + 1, col + 1)
Call dfs(ar, row - 1, col - 1)
Call dfs(ar, row - 1, col + 1)
End Sub
|
|