Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- OpenFileDialog1.Filter = "文字檔案(*.txt)|*.txt"
- Label1.Text = "" : Label2.Text = "" : Label3.Text = "" : Label4.Text = ""
- Dim txtname As String = ""
- If OpenFileDialog1.ShowDialog = DialogResult.OK Then txtname = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
- Label4.Text = OpenFileDialog1.SafeFileName
- Label1.Text = txtname
- Dim Map(8, 8), Cinx(2, 8, 8), count As Integer : Dim Memory(8, 8) As Boolean
- Dim Maze As String = Replace(Replace(txtname, Chr(13), ""), Chr(10), "")
- For i = 1 To 8 '讀取與重置
- For j = 1 To 8
- Memory(i, j) = True
- count += 1 : Map(i, j) = Mid(Maze, count, 1)
- If Mid(Maze, count, 1) = "1" Then Memory(i, j) = False
- Next
- Next
- Dim qx, qy As String : qx = 1 : qy = 1 'Stack
- Dim dx() As Integer = {0, -1, 0, 1} : Dim dy() As Integer = {-1, 0, 1, 0} '移動方向
- Cinx(0, 0, 0) = 0 : Cinx(1, 0, 0) = 0
- Dim nx, ny, MIDcount, Bug As Integer : nx = 1 : ny = 1
- Do While Len(qx) <> 0 And Len(qy) <> 0 'BFS
- MIDcount += 1 : Bug += 1
- nx = Val(Mid(qx, MIDcount, 1)) : ny = Val(Mid(qy, MIDcount, 1))
- If nx = 8 And ny = 8 Then Exit Do
- For i = 0 To 3
- If nx + dx(i) <= 8 And nx + dx(i) >= 1 And ny + dy(i) <= 8 And ny + dy(i) >= 1 Then
- If Memory(nx + dx(i), ny + dy(i)) = True Then
- qx = qx & (nx + dx(i)) : qy = qy & (ny + dy(i))
- Memory(nx, ny) = False
- Cinx(0, nx + dx(i), ny + dy(i)) = nx
- Cinx(1, nx + dx(i), ny + dy(i)) = ny
- End If
- End If
- Next
- If Bug > 100 Then Label2.Text = "No way" : Exit Sub
- Loop
- Dim x, y, fx, fy As Integer : x = 8 : y = 8
- Dim NewMap(8, 8), MazeStep As Integer
- For i = 1 To 8
- For j = 1 To 8
- NewMap(i, j) = 0
- Next
- Next
- NewMap(8, 8) = 5
- Do While x <> -1 And y <> -1
- fx = Cinx(0, x, y) : fy = Cinx(1, x, y)
- x = fx : y = fy : MazeStep += 1
- NewMap(x, y) = 5
- If x = 0 And y = 0 Then Exit Do
- Loop
- For i = 1 To 8
- For j = 1 To 8
- Label2.Text &= NewMap(i, j)
- Next
- Label2.Text &= vbNewLine
- Next
- Label3.Text = "step" & MazeStep - 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement