Solved

8 Queens Problem Recursively

Posted on 1997-11-10
1
1,284 Views
Last Modified: 2012-06-27
Does anyone have an example to solve the 8 Queens problem recursively?

Place 8 queens on a chess board so that no two queens are attacking each other
0
Comment
Question by:sdevine
1 Comment
 
LVL 5

Accepted Solution

by:
y96andha earned 50 total points
ID: 1216722
Here is an example which I just wrote for you. It will lose its indentation when posted, but I hope you can read it anyway. It sets up a chessboard, tries adding a queen at a time in a free position, and for each time marks the squares which cannot be used, and tries to add a new queen recursively.

type
      pos = (Free, Queen, Busy);
      chessboard = array[1..8,1..8] of pos;

Var
  r,k: integer;
  emptyboard : chessboard;

function min(a,b:integer):integer;
begin
      if a<b then min := a else min := b;
end;

function findsolution(board : chessboard; level : Integer): Boolean;
Var
      r,k:integer;
      i:integer;
      ch:char;
      boardcopy:chessboard;
begin
      findsolution:=false;
      boardcopy := board;
      for r:=1 to 8 do
            for k:=1 to 8 do
                  if boardcopy[r, k] = Free Then
                  begin
                        if level+1 = 8 then begin
                              writeln;
                              writeln('Solution:');
                              for r:=1 to 8 do
                              begin
                                    for k:=1 to 8 do
                                          If boardcopy[r, k] = Queen then
                                                write('Q')
                                          else
                                                write(' ');
                                    writeln;
                              end;
                              findsolution:=true;
                              exit;
                        end;


                        board:=boardcopy;
                        for i:=1 to 8 do
                              board[i, k]:=Busy;
                        for i:=1 to 8 do
                              board[r, i]:=Busy;
                        for i:=1-min(r,k) to min(9-r,9-k)-1 do
                              board[r+i, k+i]:=Busy;
                        for i:=1-min(9-r,k) to min(r, 9-k)-1 do
                              board[r-i, k+i]:=Busy;

                        board[r, k]:=Queen;

                        if findsolution(board, level+1) Then
                        begin
                              findsolution:=True;
                              exit;
                        end;

                  end;

end;



begin

      for r:=1 to 8 do
            for k:=1 to 8 do
                  emptyboard[r,k]:=Free;

      if findsolution(emptyboard, 0) then
            writeln('A solution was found');
end.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Setting up Idhttp indy component to use a proxy server 2 1,621
HTTP-POST Delphi 7 5 2,908
proper way to parse text with delphi 7 121
Using idhttp to login to instagram 2 89
One of the biggest threats in the cyber realm pertains to advanced persistent threats (APTs). This paper is a compare and contrast of Russian and Chinese APT's.
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question