主题:求高手 8 皇后问题
国际象棋的棋盘是8x8 皇后可以竖着走横着走 和走对角线 要在棋盘上摆八个皇后
想用matlab写一个国际象棋的8皇后的程序, 我已经有了一个皇后的程序,只需要写一个script file,在其中利用已有的皇后程序来完成8个皇后的摆放。在程序里先建一个ones(8,8)矩阵,然后用queen的方程来摆
我的皇后程序如下:
%queen functions
function [a,ch]=queen(r,c,a)
% r=row
% c=column
% a=board
% ch=check
% if the board changed, ch=1
% if the board not changed, ch=0
h=a(r,c); % the position that you chose
if h==1 % if the position is 1 means it's safe
a(r,:)=0;% the whole row turn to be 0
a(:,c)=0;% the whole column turn to be 0
a(r,c)=4;% the palce that you choos became 4 which represent queen
for u=1:min(7-(8-r),7-(8-c))
a(r-u,c-u)=0; %the diagonal to the top left turn to be 0
end
for u=1:min(7-(8-r),8-c)
a(r-u,c+u)=0; % the diagonal to the top right turn to be 0
end
for u=1:min(8-r,7-(8-c))
a(r+u,c-u)=0;% the diagonal to the left bottom turn to be 0
end
for u=1:min(8-r,8-c)
a(r+u,c+u)=0;
ch=1; % the diagonal to the right bottom turn to be 0
end
else
ch=0;
end
想用matlab写一个国际象棋的8皇后的程序, 我已经有了一个皇后的程序,只需要写一个script file,在其中利用已有的皇后程序来完成8个皇后的摆放。在程序里先建一个ones(8,8)矩阵,然后用queen的方程来摆
我的皇后程序如下:
%queen functions
function [a,ch]=queen(r,c,a)
% r=row
% c=column
% a=board
% ch=check
% if the board changed, ch=1
% if the board not changed, ch=0
h=a(r,c); % the position that you chose
if h==1 % if the position is 1 means it's safe
a(r,:)=0;% the whole row turn to be 0
a(:,c)=0;% the whole column turn to be 0
a(r,c)=4;% the palce that you choos became 4 which represent queen
for u=1:min(7-(8-r),7-(8-c))
a(r-u,c-u)=0; %the diagonal to the top left turn to be 0
end
for u=1:min(7-(8-r),8-c)
a(r-u,c+u)=0; % the diagonal to the top right turn to be 0
end
for u=1:min(8-r,7-(8-c))
a(r+u,c-u)=0;% the diagonal to the left bottom turn to be 0
end
for u=1:min(8-r,8-c)
a(r+u,c+u)=0;
ch=1; % the diagonal to the right bottom turn to be 0
end
else
ch=0;
end