Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
25
Task/N-queens-problem/MATLAB/n-queens-problem.m
Normal file
25
Task/N-queens-problem/MATLAB/n-queens-problem.m
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
n=8;
|
||||
solutions=[[]];
|
||||
v = 1:n;
|
||||
P = perms(v);
|
||||
for i=1:length(P)
|
||||
for j=1:n
|
||||
sub(j)=P(i,j)-j;
|
||||
add(j)=P(i,j)+j;
|
||||
end
|
||||
if n==length(unique(sub)) && n==length(unique(add))
|
||||
solutions(end+1,:)=P(i,:);
|
||||
end
|
||||
end
|
||||
|
||||
fprintf('Number of solutions with %i queens: %i', n, length(solutions));
|
||||
|
||||
if ~isempty(solutions)
|
||||
%Print first possible solution
|
||||
board=solutions(1,:);
|
||||
s = repmat('-',n);
|
||||
for k=1:length(board)
|
||||
s(k,board(k)) = 'Q';
|
||||
end
|
||||
s
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue