BT NQueens Matlab does not return answer (array)
Afficher commentaires plus anciens
i want the first array answer, but can't return that. if i change last line to BT(array,1,n), code does work correctly, but without returning anything.
error is:
Output argument "answer" (and maybe others) not assigned during call to "BT".
Error in Run (line 5)
a = BT(array,1,n)
main:
clc
clear
array = [];
n = input('chess board is ?*?, inter ?: ');
a = BT(array,1,n)
my BT funcntion:
function answer = BT(array,index,n)
if (is_valid(array,index))
if (index==n+1)
disp(array)
answer = array;
return
end
for i=1:n
array(index)= i;
BT(array,index+1,n);
end
end
end
is_valid (works correctly):
function [flag] = is_valid(array,index)
for i=1:index-1
for j=i+1:index-1
if (array(j) == array(i)) || (abs(array(i)-array(j))==abs(i-j))
flag = false;
return
end
end
end
flag = true;
end
Réponses (0)
Catégories
En savoir plus sur MATLAB Coder dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!