Question
Queens Problem MATLAB (Different than normal queens problem) The code of main.m, sol.m, and back.m DOES NOT WORK. (It has been given as a solution
Queens Problem MATLAB
(Different than normal queens problem)
The code of main.m, sol.m, and back.m DOES NOT WORK. (It has been given as a solution to all post of same question)
Need help especially with Part C.
Algorithm
A) Begin the code using a variable that defines the board size we want our code to be flexible enough to handle boards ranging in size from 4x4 up to NxN. Based on this variable, create a board by initializing a matrix that is size NxN using the zeros command.
B) Write code that will randomly place N queens on the board. Use the number 1 to indicate a space on the board that contains a queen. If you have a 4x4 board, your matrix should now contain four entries that are a 1 -- these represent your queens. The remaining entries in the matrix should still be zeros. Note: This random placement is one area of the code that you are allowed to modify. If you truly allow your code to randomly place the queens, it might take a long time to find a solution (especially for large N); the benefit of simple random placement is that the code for the placement of the queens will be much easier to write. Examples of modifications that you can make to the code that will guide the placement of queens:
i. Only allow 1 queen to be placed in each row
ii. Only allow 1 queen to be place in each column
iii. After the placement of any single queen, you may also mark off any spaces that she attacks as being unfit for future placement.
iv. (Note: If you program too many restrictions, then your code may not be able to finish placing queens on the board. If you find yourself in this situation, your code must be able to recognize what has happened and should then re-set the board to all zeros and start the placement process once again.)
C) If you havent implemented an attack procedure as part of the above restrictions, your code will now have to go through an attack procedure (and even if you did, you may still have to do this step depending on the completeness of your attack procedure). That is, once you have a board that has N queens placed on it, you have to evaluate whether any queen threatens another occupied position on the board. Write code that iterates through your matrix and looks for a queen (i.e., a 1), then have that queen attack in all possible directions. To do this, change all positions that she is capable of threatening into an 8 (in other words, all other entries in her row, column, and in both diagonals will become an 8). a. Note: If another queen was in the way, this attack sequence just eliminated her from the board. You can stop that particular solution there, or.
D) After executing the attack sequence for every queen on the board, count how many are remaining. If you still have N queens, youve found a solution congratulations! If you have less than N queens on the board, re-set the board (turn it to all zeros) and go back to step 2.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started