Knight Travels/knight tour-visual Basic Solution

Source: Internet
Author: User

The question of Knight travel is seen in the seventh chapter of the visual Baisc 6 how to program Chinese version.

On the 8x8 chess board, the Knight (walk: Two squares in one direction, one in the other) does not repeat the path of all the blanks on the board.

The idea is to choose the corner of a grid as the starting point, all the road can go all the way to try again. To try 8^63 times, the calculation time is too long. It's better to make the chessboard into 5x5. In addition, the book suggests that, depending on the accessibility of the space (which is difficult to determine by the number of spaces around which it can be accessed), a space access that is more difficult to access is first selected.

Here's how to go through it all over again.

Option explicitoption Base 1Dim Stepmax as Integerconst n = 8Dim anw as integerprivate Sub Command1_Click () Dim x A s integer Dim y As Integer Dim step As Integer Dim c (N, N) As Integer x = 1 y = 1 step = 1 C ( x, y) = Step call Knightsol (x, y, step, C ()) Text2.text = Stepmax Label1.Caption = anwend Sub Private Sub Knightsol (ByVal x As Integer, ByVal y As Integer, ByVal step As Integer, C () As Integer) Dim i As Integer Dim xt As  Integer, yt As Integer, stept as Integer ' List1.AddItem ("-------------------------") for i = 1 to 8 XT =        x yt = y stept = Step ' List1.AddItem ("current position is &" ("& X &", "& Y &") ")        Call MV (i, x, y) ' List1.AddItem ("--Toward" & I & "Direction to" & "(" & X & "," & Y & ")")                If x > 0 and x <= N and y > 0 and y <= n then If c (x, y) = 0 Then step = step + 1 C (x, y) =Step ' List1.AddItem ("--Assignment" & X & "," & Y & "& Step) If Step > St                    Epmax then Stepmax = Step End If step = n * N Then ' List1.AddItem ("Destination completion output path") ' List1.AddItem ("-------------") call PA                    Th (c ()) C (x, y) = 0 MsgBox ("found") Anw = anw + 1                Exit Sub End If call Knightsol (x, y, step, C ()) ' Call Path (c ())                ' List1.AddItem ("Restore C (" & X & "," & Y & ")") c (x, y) = 0 x = xt                y = yt step = Stept ' List1.AddItem ("-------------------------") Else            ' List1.AddItem ("* * is occupied") x = xt y = yt End If Else ' List1.AddItem ("* * crossed the") x = xt y = yt End If Next End SubPrivate Sub mv (i As Integer, x As Integer, y as Int  Eger) Select case I case 1 x = x + 2 y = y + 1 case 2 x = x + 2 y = y-1 case  3 x = x-2 y = y + 1 case 4 x = x-2 y = y-1 Case 5 x = x + 1 y = y +        2 Case 6 x = x + 1 y = y-2 case 7 x = x-1 y = y + 2 case 8 x = x-1        y = Y-2 End Select End subprivate Sub path (c () As Integer) Dim A As Integer, B As Integer for a = 1 to n For b = 1 to n text1.text = Text1.Text & Format (C (A, B), "@@@") Next b text1.text = Te XT1. Text & vbCrLf Next a text1.text = text1.text & Vbcrlfend Sub

Knight travels/knight tour-visual Basic solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.