|
楼主 |
发表于 2012-7-11 16:24
|
显示全部楼层
本帖最后由 香川群子 于 2012-7-11 16:35 编辑
- Public cnt
- Sub mySerch()
- ActiveCell.CurrentRegion.Interior.ColorIndex = 0
- arr = [a1:z26]
- Randomize
- For i = 2 To 25
- For j = 2 To 25
- If Rnd() > 0.5 Then arr(i, j) = 1 Else arr(i, j) = 0
- Next
- Next
- [a1:z26] = arr
- '以上是每次重新随机生成迷宫
-
- t = ActiveCell
- cnt = 0
- Call schdg(ActiveCell.Row, ActiveCell.Column, t) '调用递归过程
- ActiveCell.Interior.ColorIndex = 4
- End Sub
- '下面是递归过程
- Sub schdg(i, j, t)
- If Cells(i - 1, j) = t And Cells(i - 1, j).Interior.ColorIndex <> 3 Then
- Cells(i - 1, j).Interior.ColorIndex = 3: cnt = cnt + 1: Cells(i - 1, j) = cnt
- Call schdg(i - 1, j, t)
- End If
-
- If Cells(i, j + 1) = t And Cells(i, j + 1).Interior.ColorIndex <> 3 Then
- Cells(i, j + 1).Interior.ColorIndex = 3: cnt = cnt + 1: Cells(i, j + 1) = cnt
- Call schdg(i, j + 1, t)
- End If
-
- If Cells(i + 1, j) = t And Cells(i + 1, j).Interior.ColorIndex <> 3 Then
- Cells(i + 1, j).Interior.ColorIndex = 3: cnt = cnt + 1: Cells(i + 1, j) = cnt
- Call schdg(i + 1, j, t)
- End If
-
- If Cells(i, j - 1) = t And Cells(i, j - 1).Interior.ColorIndex <> 3 Then
- Cells(i, j - 1).Interior.ColorIndex = 3: cnt = cnt + 1: Cells(i, j - 1) = cnt
- Call schdg(i, j - 1, t)
- End If
-
-
- End Sub
复制代码
dg.zip
(23 KB, 下载次数: 147)
|
|