필터 지우기
필터 지우기

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?

답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by