Question

In: Computer Science

Give an algorithm to find the number of ways you can place knights on an N...

Give an algorithm to find the number of ways you can place knights on an N by M (M < N) chessboard such that no two knights can attack each other (there can be any number of knights on the board, including zero knights). Clear describe your algorithm and prove its correctness. The runtime should be O(2^3M * N).

Solutions

Expert Solution

  • Consider that  K knights are to placed  on an M*N chessboard such that they don’t attack each other.
  • A knight can move two squares vertically and one square horizontally or two squares horizontally and one square vertically.
  • The knights attack each other if one of them can reach the other one in a single move.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

This can be solved using a backtracking algorithm.
Algorithm:

i) Start

ii) Enter the  knights position one by one starting from first row and first column

ii)Iterate forward to first row and second column such that they don’t attack each other.

iii)When one row is exhausted ,iterate over  to the next row.

iv)Before placing a knight, Insert a checking condition to check if the block is safe i.e. it is not an attacking position of some other knight.

v)If (block == safe),

  place the knight and mark it’s attacking position on the board

else

move forward and check for other blocks.

vi)In this process, make a new board every time a new knight is inserted into the board.

vii)continue until all possible solutions are obtained.

viii)Stop the program

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Correctness of this algorithm:

Since this algorithm is based on backtracking i.e, if we get one solution and we need the rest of the solutions, then we can backtrack to our old board with old configuration of knights and that can be used to find the other possible solutions.


Related Solutions

a) Find the recurrence relation for the number of ways to arrange flags on an n...
a) Find the recurrence relation for the number of ways to arrange flags on an n foot flagpole with 1 foot high red flags, 2 feet high white flags and 1 foot high blue flags. b) solve the recurrence relation of part a
Find and solve a recurrence relation for the number of ways to stack n poker
Find and solve a recurrence relation for the number of ways to stack n poker chips using red, white and blue chips such that no two red chips are together. Use your solution to compute the number of ways to stack 15 poker chips.
5. Find the generating function for the number of ways to create a bunch of n...
5. Find the generating function for the number of ways to create a bunch of n balloons selected from white, gold, and blue balloons so that the bunch contains at least one white balloon, at least one gold balloon, and at most two blue balloons. How many ways are there to create a bunch of 10 balloons subject to these requirements?
In how many ways you can triangulate a simple polygon with n vertices. Give both qualitative...
In how many ways you can triangulate a simple polygon with n vertices. Give both qualitative and quantitative answer.
Explain why ‘white knights’ are often hard to find and can only be seen as a...
Explain why ‘white knights’ are often hard to find and can only be seen as a ‘partial’ solution to fending off a hostile takeover bid. (b) Critically discuss which factors will influence a company to finance a takeover by either a share-for-share offer or a cash offer financed by an issue of bonds. © Two companies called X plc and Y plc are considering a merger. Financial data for the two companies are given below: X Y Number of shares...
1) Find the number of unique ways that the letters in the word can be arranged:...
1) Find the number of unique ways that the letters in the word can be arranged: KERFUFFLE 2) A survey of commuters found that 313 owned a motorcycle, 232 owned a car, 269 owned a moped, 98 owned a car and a moped, 57 owned only a car, 104 owned a motorcycle and moped but not a car, 69 owned all three, and 64 owned none. What proportion of people surveyed owned only a moped? Leave your answer as an...
Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:
Chapter 3 Exercise 1Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:      1. Make a guess at the answer (you can pick n/2 as your initial guess).      2. Computer = n / guess.      3. Set guess = (guess +r) / 2.      4. Go back to step 2 until the last two guess values are within 1% of each other.Write a program that inputs an integer for n, iterates through the Babylonian algorithm until the guess...
You have n numbers and you want to find a number x (out of the n)...
You have n numbers and you want to find a number x (out of the n) such that x is larger than the median. You can create an algorithim that takes time O(nlogn): sort the n numbers and then report any number that is larger than the element in position n2 of the sorted array. You can also create an algo in O(n) time, by finding the median in linear time and then doing a linear scan to find a...
Give the maximum number of orbitals in an atom that can have these quantum numbers: n...
Give the maximum number of orbitals in an atom that can have these quantum numbers: n = 3                                                   _______________ n = 3, l = 1                                           ______________ n=2, l=1, ml= 0                                    _______________ n=0, l=0, ml=0                                     _______________ Removing the electron from a Hydrogen atom corresponds to a raising the electron from n=1 to an orbit that has n=∞.             What is the energy needed to remove the electron from a hydrogen atom? What is the energy in terms of kJ per...
what is the maximum number of inversions that a permutation {1,..,n} can have? give the unique...
what is the maximum number of inversions that a permutation {1,..,n} can have? give the unique permutation with this many inversions. how many permutations have one fewer inversions than the minimum.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT