本帖最后由 Moneky 于 2017-5-18 00:55 编辑
- Sub vGo(r As Long, c As Long)
- If Cells(r + 1, c) = "出口" Or Cells(r, c - 1) = "出口" Or Cells(r - 1, c) = "出口" Or Cells(r, c + 1) = "出口" Then
- Cells(r, c) = 1
- MsgBox "OK"
- End
- End If
- If Cells(r, c + 1).Interior.Color <> vbBlack And Cells(r, c + 1) = "" Then
- Cells(r, c) = 1
- vGo r, c + 1
- ElseIf Cells(r + 1, c).Interior.Color <> vbBlack And Cells(r + 1, c) = "" Then
- Cells(r, c) = 1
- vGo r + 1, c
- ElseIf Cells(r, c - 1).Interior.Color <> vbBlack And Cells(r, c - 1) = "" Then
- Cells(r, c) = 1
- vGo r, c - 1
- ElseIf Cells(r - 1, c).Interior.Color <> vbBlack And Cells(r - 1, c) = "" Then
- Cells(r, c) = 1
- vGo r - 1, c
- ElseIf Cells(r, c - 1) = 1 Then
- Cells(r, c) = "×"
- vGo r, c - 1
- ElseIf Cells(r + 1, c) = 1 Then
- Cells(r, c) = "×"
- vGo r + 1, c
- ElseIf Cells(r, c + 1) = 1 Then
- Cells(r, c) = "×"
- vGo r, c + 1
- ElseIf Cells(r - 1, c) = 1 Then
- Cells(r, c) = "×"
- vGo r - 1, c
- Else
- MsgBox "No way!"
- End
- End If
- End Sub
- Sub test()
- vGo 13, 18 '入口在cells(2,2)
- End Sub
复制代码
可以在表格中用黑色作为墙直观的画出迷宫,并在出口单元格中填上“出口”二字,然后调用 vGO r,c r,c表示入口的行标和列标,执行结束后,单元格中的1就是路径
从起点开始连续的1才是路径
只能找到路径但并不是最短的路径。
|