C ++ job 02

xiaoxiao2021-03-06  89

Programming Assignment # 2: Function Recursion (Knights Tour)

Knight's Tour:

. In the game of Chess (international chess, at least) the knight moves in an unusual pattern of 2 squares horizontally and then 1 square vertically or 2 squares vertically and then 1 square horizontally The 8 possible moves for a knight are shown below:

8

1

Seduce

2

Knight

6

3

5

4

Somewhere Along The Line The Question Was Asked: Could A Knight Move To Every Location On a Square More Than Once. This Challege Is Known as The Knight's Tour.

Your assignment is to come up with a recursive solution to the Knight's Tour. As your knight travels around the board record the squares that he has touched by storing the move count, so for example the first square that the knight starts from would be marked with A 1 and the next square That The Knight Moves to Would Be Marked with a 2 and So ON A Normal 8x8 Chess Board The Knight Moves To The Last Square and Stores A 64 There ... The Knight Would Have Them Completed His Tour. for this assignment, IT IS OK to Use Some of The Statistics You May Want (Move Count, Tries, ECT).

Remember in recursion you call a function to solve a problem, then the function will call itself to solve the next step of the problem. This will go on until the function gets to some ending solution. In this case, your function would be a move For the Knight, And It Would Call Itself for the next place to try moving to, which would call itself for the next place to move, and so on, and so on.

For output from the program, you need to print out a map of where the knight visited in his tour Here is an example of a Knight's Tour for a 5x5 board starting from the upper left corner:. Yeehaw !!! after 41 tries

And 16 Bad Moves

1 20 17 12 3

16 11 2 7 18

21 24 19 4 13

10 15 6 23 8

25 22 9 14 5

Debugging Suggestions: For debugging use a 4x4 board to see if the recursive calls are working Note:. On a 4x4 board there is no solution when starting in the upper left corner, but it is still nice for following the algorithm for a few moves. Have the board print out after each move when you are first testing to verify that your algorithm is working. Once you are convinced your algorithm is good, you can have the board printed periodically to give you an update (I had mine print out after every 10 Million Tries ... Some of the Tours ON AN 8X8 Board Can Take A While .... Starting At Location 4, 4 (When First Row and Col Are 0) The Program Ran Over Night And Had Tried 62,200,000,000 MOVES AND HAD A Ways to go before solving it with this brute force algorithm). The Solution of the Required Tour Does NOT Take That Long; It Only Takes 3,242,065 Tries To Find The Soltution

Turn in: A paper copy of your code with a print out of your of the tour take by your knight on a 8x8 board For the tour you turn in, start the knight in the upper left corner The tour can be captured from the.. Command Window (Black Screen) by Using SELECT All and the Copy from The Command Windows Menus, THEN You Can Paste your Output Below your program code as a comment.

转载请注明原文地址:https://www.9cbs.com/read-100976.html

New Post(0)