Artificial intelligence Matlab to realize the eight queen problem of backtracking

Source: Internet
Author: User


first, the purpose of the experiment:

To solve the problem of eight queens with backtracking method.

second, the experimental content:

1, read the C language writing backtracking method to solve the eight Queen problem code.

2, reference C language code, using MATLAB to write backtracking method to solve the eight Queen problem code.

3, require comments on the key code, annotated line requirements of 1/3.

Iii. List of procedures:

Queens.m

% recursive search temptation to place Queen

function q= Queens (n)

Global Indexes;

A=ones (N,n); % Initialize Checkerboard

Indexes=zeros (1,n); % record Queen's position coordinates

I=1; % line number

J=1; % column number

count=0; The number of statistical solutions in%

while (i>0)

Whilej<=n

If Judge (i,j)% will not attack the location of the other Queens

A (I,J) = 8; %8 on behalf of the Queen

Indexes (i) =j;

Break % jump out of this loop

Else

j=j+1; % probe Next Location

End

End

Ifj<=n% if you find a suitable location, find the next

i=i+1;

J=1;

Else% if no suitable location is found, backtrack, back to the previous line

I=i-1;

If i~=0

J=indexes (i);

A (I,J) = 1; % Restore Value

Indexes (i) = 0; % remove the reserved coordinates

j=j+1; % starting from the back of the last Queen

End

End

if (Indexes (n) ~=0)% If a solution is found

count=count+1;

Disp (A); % Output matrix

End

End

DISP ([Num2str (n), ' Queen problem solution number: ', NUM2STR (count)]); % output Number of solutions

End

% sub-function to determine if the position can be placed (x, y)

Judge.m

function P=judge (x, y)

Global Indexes;

% determines if the placed Queen is attacked (on the same diagonal or on the same row)

Fora=1:x-1%a as line number

B=indexes (a); %B is the column number where the Queen is placed

% if (x-y==a-b | | x+y==a+b | | x==a | | y==b)

% is judged on the same diagonal line and on the same row

if (x-y==a-b | | x+y==a+b | | y==b)% judgment is on the same diagonal and on the same column

P=0;

Return % Exit Sub-function

End

End

P=1;

End

Main function

Main.m

Clear % Data zeroing

CLC % Command Window record emptied

n=8; % Eight queens problem, set 8*8 matrix, n assigns an initial value of 8

Queens (n)% call function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.