Can anyone tell me what is wrong with my script?
1 回表示 (過去 30 日間)
古いコメントを表示
N = input('boardsize ');
NQ = 0;
X = zeros(N);
X(sub2ind([N,N], 1:N, randperm(N, N))) = 1;
while(NQ<N)
for i = 1:N
for j = 1:N
if(X(i,j) == 1)
X(:,j) = 8;
X(i,j) = 1;
end
end
end
X0=logical(X);
[i,j]=find(X0);
[ii,jj]=ndgrid(1:N);
X=reshape(ismember(ii(:)-jj(:),i-j) + ismember(ii(:)+jj(:),i+j),N,N);
X(X>0)=8;
X(X0)=1;
index = find([X] == 1);
NQ = numel(index);
end
disp(X)
>> Queens
boardsize 8
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
The output prints out all 1's when I really want to have an array with N amount of 1's where N is the boardsize NxN. As well as 8's in places that are attacked diagonally and horizontally. I know the individual loops work to "attack the spaces from where the ones are found in the array but together I think my while loop messes it all up. anyone know what I wrong?
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!