Matlab NQueens Algorithm Recursion -
For an assignment, I had to use Metlab and Recurs to create an enucleation algorithm. So the way I set it up, I have 2 helpful tasks, it is valid, and for a recursive quinn test, which removes the vacancy of an MXM board or removes the queen and adds one Or removes the queen from every possible move. For space I have removed the add function from the recursive quinn function, but it does all this, which adds or decreases in 1 in 8 directions.
The main problem I have is that my solution is in the NQF function, if no solution is found in the next line, then it is getting to go to the next column. I have removed my steps from 6 things:
1) Keep a queen in the first line
2) Put a queen on the next line in the next line
3) Repeat the two lines until there is no valid placement for the line.
4) Remove the queen from the last line
5) Place the queen in the next valid place row
6) Repeat step 1-5 until all queues do not have a queen (I have not coded in this step)
Funk Connection out = NQueens (m)% main function board = zero (m, m); % Intializes Out Board = Solved NQ (1, Board)% Recurrence Function End Function Out = Hull NQ (Colonel, Board) N = Length (Board); Out = wrong; % If no solution is found then returns false returns if call & gt; I = colon: j = 1: n if is the valid (board, i, j) board = recursive quinn (board, i, j, 'place')% place queen out = solve nq (col + 1, board) % Recursive call end and board = recurring quinn (board, i-1, colle, 'remove')% if there is no valid placement for the line = Hull NQ (Colonel-1, Board)% re-end and end function Out to = isValid (board, row, cola) if the board (row, color) == 0 = true; And out = wrong; And Function Board = Recursive quinn (board, row, col, move) board = white (board, row, col, move); % Right Board = Goalite (board, row, col, move); % Left Board = Godown (board, row, col, move); % Down Board = GoAppe (board, row, col, move); % Up board = goRightUp (board, row, kernel, move); % Diagnostics Right Board = goLeftUp (board, row, col, move); % Diagnostic Left Board = goRightDown (board, row, col, move); % Diagnosis Right Down Board = goLeftDown (board, row, col, move); % Diagnosis is left if strcmp (moved, 'location')% places queen board (row, color) = -1; Athiff SRCMP (move, 'remove')% removes queen board (row, color) = 0; Finally,
You will not need a second loop for j = Your call + 1 will allow you to move to the next column
I have this concept as follows
1) Keep a queen in the top left corner
2) Go to the next column and place a queen on it. It is valid
3) Repeat steps 2, so now we are on 3 columns. If you can not put a queen there, then remove the last queen. You can remove the queen by solving the main problem with your current code. The reason behind this is that when a queen can not be placed in that line, Then nothing really will be done for the loop (since Valid is wrong). When the loop is stopped for loop (it is for loop inside the recurring), MATLAB will pick up the place where it was left before, which is the solution to your clone, and jump to the next line, Where should you remove your queen?
Do not forget that if n & gt; Colonel, which means that you can keep N Queens on the board, your output is correct.
I had to become friends with this problem as well. If my interpretation was bad, feel free to answer.
Comments
Post a Comment