Can anyone tell me what is wrong with my script?

1 回表示 (過去 30 日間)
Shawn Blancett
Shawn Blancett 2017 年 9 月 18 日
コメント済み: Stephen23 2017 年 9 月 18 日
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?
  2 件のコメント
KSSV
KSSV 2017 年 9 月 18 日
Why don't you show us what out put you are expecting....

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by