(Knight's Tour) One of the more interesting puzzlers for chess buffs is the Knight's Tour
problem. The question is this: Can the chess piece called the knight move around an empty chess-
board and touch each of the 64 squares once and only once? We study this intriguing problem in
depth in this exercise.
The knight makes L-shaped moves (over two in one direction then over one in a perpendicular
direction). Thus, from a square in the middle of an empty chessboard. the knight can make
eight different moves (numbered 0 through 7) as shown in Fig. 7.25.
- Draw an 8-by-8 chessboard on a sheet of paper and attempt a Knight's Tour by hand' Put a 1 in the first square you move to. a 2 in the second square, A 3 in the third,
- Now let's develop a program that will move the knight around a chessboard. The board Is represented by an 8-by-8 two-dimensional array board. Each of the squares is initialized
Before starting the tour, estimate how far you think you'll get, remembering that a full
tour consists of 64 moves. How Ear did you get? Was this close to your estimate?
zero. We describe each of the eight possible moves in terms of both their horizontal
and vertical components. For example, a move of type 0. as shown in Fig, 7.25,
consists of moving two squares horizontally to the right and one square vertically up-
ward. Move 2 consists of moving one square horizontally to the left and squares
0 1 2 3 4 5 6 7 0 1 2 1 2 3 0 3 K 4 4 7 5 5 6 6 7
Fig.7.25 The eight possible moves of the knight
Vertically upward. Horizontal moves to the left and vertical moves upward are indicated with negative numbers. The eight moves may be described by two one-dimensional arrays, horizontal and vertical, as follows:
Horizontal [0] =2 vertical [0] = 1 Horizontal [1] = 1 vertical [1] = 2 Horizontal [2] =-1 vertical [2] = -2 Horizontal [3] = -2 vertical [3] = -1 Horizontal [4] = -2 vertical [4] = 1 Horizontal [5] = -1 vertical [5] = 2 Horizontal [6] = 1 vertical [6] = 2 Horizontal [7]= 2 vertical [7] = 1
Let the variables and currentCoIumn indicate the row and column of
the knight's current position. To make a move of type moveNumber, where is moveNumber is
between 0 and 7. your program uses the statements
CurrentRow += vertical [moveNumber];
currentColumn += horizontal [moveNumber];
Keep a counter that varies I to 64. Record latest in each square the knight moves to. Remember to test cach potential move to see if the knight has already visited that square, and of course, test every potential move to make sure that the knight does not land off the chessboard. Now "Tite a program to move the knight around the chessboard. Run the program. How many moves did the knight make?
c) After attempting to write and run a Knight's Tour program. you've probablv developed some value Insights. We'll use these develop a heuristic (or strategy) for moving the knight. Heuristics do not guarantee success, but a carefully developed heuristic greatly improves the chance of success. You may have observed that the outer squares are more troublesome than the squares nearer the center of the board. In fact, the most troublesome, or inaccessible, squares arc the four corners.
Intuition may suggest that you should attempt to move the knight to the most troublesome squares first and leave open those that easiest to get to, so when the board gets congested near the end of the tour. there will be a greater chance of success.
We may may develop an "accessibility heuristic" by classifying each square according to how accessible it's then always moving the knight to the square (within the knight's L shaped moves, of course) that's least accessible. We label a two-dimensional array accessibility with numbers indicating from how many squares each particular square is accessible. On a blank chessboard, each center square is rated as 8, each corner square is rated as 2 and the other squares have accessibility numbers of 3,4 or 6 as follows
-
2 3 4 4 4 4 3 2
3 4 6 6 6 6 4 3
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
4 6 8 8 8 8 6 4
3 4 6 6 6 6 4 3
2 3 4 4 4 4 3 2
Now write a version of the Knight's Tour program using the accessibility heuristic.
At any time, the knight should move to the square with the lowest accessibility num ber. In case of a tie, the knight may move to any of the tied squares. Therefore, the tour may begin in any of the four corners. [Note: As the knight moves around the chess- board, your program should reduce the accessibility numbers as more and more Squares become occupied. In this way, at any given time during the tour, each available square's accessibility number will remain equal to precisely the number of squares from which that square may be reached.] Run this version of your program. Did you get a full tour? Now modify the program to run 64 tours, one starting from each square of the chessboard. How many full tours did you get?
d.Write a version of the Knight's Tour program Which, When encountering a tie between two or more Squares, decides what square to choose by looking ahead to those squares reachable from the "tied" squares. Your program should move to the square for which the next move would arrive at a square with the lowest accessibility number.
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
C++ How to Program (10th Edition)
- Python answer only. Correct answer will upvoted else downvoted. It is the ideal opportunity for your very first race in the game against Ronnie. To make the race intriguing, you have wagered a dollars and Ronnie has wagered b dollars. Yet, the fans appear to be frustrated. The fervor of the fans is given by gcd(a,b), where gcd(x,y) means the best normal divisor (GCD) of integers x and y. To make the race seriously invigorating, you can perform two kinds of activities: Increment both an and b by 1. Diminishing both an and b by 1. This activity must be performed if both an and b are more noteworthy than 0. In one action, you can play out any of these activities. You can perform self-assertive (potentially zero) number of moves. Decide the greatest energy the fans can get and the base number of moves needed to accomplish it. Note that gcd(x,0)=x for any x≥0. Input The principal line of input contains a solitary integer t (1≤t≤5⋅103) — the number of experiments.…arrow_forward[Unbalanced Rod] Given a set of n weights {w₁,..., wn} and a rod of length n - 1 inches, we can attach the weights to the rod at hooks placed at one inch distances apart as shown in the figure below. -1". /10 2 3 12 2 4 We can attach a weight to any hook but no two weights can be attached to the same hook and we have to attach all the weights. For any given assignment of weights to hooks, we can compute the location of the center of mass of the rod and the weights according to the following equation (neglecting the weights of the rod and the hooks). where 0 ≤ Pi≤n-1 is the position of weight along the rod. For example, in the figure shown above, the center of mass is computed as C= C = i Wi Pi Σi Wi 10 0+2 1+3·2+4·3+12.4 +2.5 10+2+3+4+12+2 78 33 The problem is to find an assignment of weights to hooks that makes the center of mass as far as possible to the left, i.e., minimize the value of c. Answer the following questions. 1. Describe a greedy algorithm that finds the assignments that…arrow_forwardPython Programing: Sahil is an outstanding entertainer. He is expert in what he does. At a day, he accepted Samir's challenge to plant 20 million trees by 2020. Currently, there are N trees (numbers 1 to N) planted in different places in the row; for each valid I, the location of the i-Ayi tree. A bunch of trees is good if for each tree in this set (let's say its place in x), there is a tree in x - 1 and / or a tree in x + 1. Sahil task is to plant more trees (perhaps zero) in such a way that the effect of all the trees planted (the first N trees and the ones planted by Sahil) is good. It is only allowed to plant trees in complete (perhaps unfavorable) areas. Help Sahil to find the minimum number of trees they need to plant to achieve that using Python Programming. Input: 1 3 538 Output: 3.arrow_forward
- [using C++]Masterchef pankaj recently baked a big nepotialn pizza that can be represented as a grid of N rows and M columns, each cell can be either empty or contain a jalapeno, pankaj wants to cut out a sub-rectangle from the pizza which contains even number of jalapenos. Before cutting such a sub-rectangle, he is interested in knowing how many sub-rectangles are there which contains even number of jalapenos.First line of input consist of two integers P and Q. Each of the next P lines contains a string of length Q, j-th character of i-th string is 1 if the corresponding cell contains a jalapeno otherwise it's 0.Output a single integer, the number of sub rectangles which contains even number of jalapeno.Sample Input:2 22101Output:5arrow_forwardDo you reach many, do you reach one? def knight_jump(knight, start, end): An ordinary chess knight on a two-dimensional board of squares can make an “L-move” into up to eight possible neighbours. However, we can generalize the entire chessboard into k dimensions from just the puny two. A natural extension of the knight's move to keep moves symmetric with respect to these dimensions is to define the possible moves as some k-tuple of strictly decreasing nonnegative integer offsets. Each one of these k offsets must be used for exactly one dimension of your choice during the move, either as a positive or a negative version.For example, the three-dimensional (4,3,1)-knight makes its way by first moving four steps along any one of the three dimensions, then three steps along any other dimension, and then one step along the remaining dimension, whichever dimensions that was. These steps are considered to be performed together as a single jump that does not visit or is blocked by any of the…arrow_forward(Intro to Java) Explain the answers to the below questions. include a written answer to the question using step-by-step explanation 4. Write the linearSearch method as follows using the algorithm provided in class: The method is named linearSearch It takes on array of doubles as a parameter It takes a double value to locate in the array as a second parameter It returns the integer location (index) in the array where the value is located Or, it returns -1 if the value cannot be foundarrow_forward
- Problem Statement: The mathematician Conway imagined a game, called game of life, whichconsidered cells that are susceptible to reproduce, disappear, or survive when they obey certainrules. These cells are represented by elements on a grid of squares, where a grid has an arbitrarysize. Thus, each cell (except those on the boundaries of the grid) is surrounded by eight squaresthat contain other cells. The rules are stated as follows:1. Survival: Each cell that has two or three adjacent cells survives until the next generation.2. Death: Each cell that has at least four adjacent cells disappears (or dies) by overpopulation.Also, each cell that has at most one adjacent cell dies by isolation.3. Birth: Each empty square (i.e., dead cell) that is adjacent to exactly three cells gives birthto a new cell for the next generation.It is worth noting that all births and deaths occur at the same time during a generation.Write a program that simulates this game and displays all successive…arrow_forwardcan you help me with this code because I am struggling:question that I need help with:it will be investigate the problem of navigation on a two-dimensional grid with obstacles. The goal is to produce the shortest path between a provided pair of points, taking care to maneuver around the obstacles as needed. Path length is measured in Euclidean distance. Valid directions of movement include up, down, left, right, up-left, up-right, down-left, and down-right. Your task is to write a function find_path(start, goal, scene) which returns the shortest path from the start point to the goal point that avoids traveling through the obstacles in the grid. For this problem, points will be represented as two-element tuples of the form (row, column), and scenes will be represented as two-dimensional lists of Boolean values, with False values corresponding empty spaces and True values corresponding to obstacles. Your output should be the list of points in the path, and should explicitly include both…arrow_forwarddef winning_card(cards, trump=None): Playing cards are again represented as tuples of (rank,suit) as in the cardproblems.pylecture example program. In trick taking games such as whist or bridge, four players each play one card from their hand to the trick, committing to their play in clockwise order starting from the player who plays first into the trick. The winner of the trick is determined by the following rules:1. If one or more cards of the trump suit have been played to the trick, the trick is won by the highest ranking trump card, regardless of the other cards played.2. If no trump cards have been played to the trick, the trick is won by the highest card of the suit of the first card played to the trick. Cards of any other suits, regardless of their rank, are powerless to win that trick.3. Ace is the highest card in each suit.Note that the order in which the cards are played to the trick greatly affects the outcome of that trick, since the first card played in the trick…arrow_forward
- Nationality. Prove the function works by testing it 2. BLACKJACK: Sample two cards from a "deck of cards" (ace, 2-10, jack, queen, king). Find the total of the two cards (ace counts as 11 and facecards count as 10). If the total is 21 print "BLACKJACK!" otherwise print "Try again". Keep drawing pairs of two cards until you get blackjack. Set your seed to 30 at the start of this problem ? Write 2 that ohe to frame for NAe If NA is found it should replace NA with thearrow_forwardDo you reach many, do you reach one? def knight_jump(knight, start, end): An ordinary chess knight on a two-dimensional board of squares can make an “L-move” into up toneight possible neighbours. However, we can generalize the entire chessboard into k dimensions from just the puny two. A natural extension of the knight's move to keep moves symmetric with respect to these dimensions is to define the possible moves as some k-tuple of strictly decreasing nonnegative integer offsets. Each one of these k offsets must be used for exactly one dimension of your choice during the move, either as a positive or a negative version. For example, the three-dimensional (4,3,1)-knight makes its way by first moving four steps along any one of the three dimensions, then three steps along any other dimension, and then one step along the remaining dimension, whichever dimensions that was. These steps are considered to be performed together as a single jump that does not visit or is blocked by any of the…arrow_forwardIn python, Problem Description:Sheldon and Leonard are physicists who are fixated on the BIG BANG theory. In order to exchange secret insights they have devised a code that encodes UPPERCASE words by shifting their letters forward. Shifting a letter by S positions means to go forward S letters in the alphabet. For example, shifting B by S = 3 positions gives E. However, sometimes this makes us go past Z, the last letter of the alphabet. Whenever this happens we wrap around, treating A as the letter that follows Z. For example, shifting Z by S = 2 positions gives B. Sheldon and Leonard’s code depends on a parameter K and also varies depending on the position of each letter in the word. For the letter at position P, they use the shift value of S = 3P + K. For example, here is how ZOOM is encoded when K = 3. The first letter Z has a shift valueof S = 3 × 1 + 3 = 6; it wraps around and becomes the letter F. The second letter, O, hasS = 3 × 2 + 3 = 9 and becomes X. The last two letters…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education