home *** CD-ROM | disk | FTP | other *** search
- Organization: Freshman, CIT general, Carnegie Mellon, Pittsburgh, PA
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!news.sei.cmu.edu!fs7.ece.cmu.edu!crabapple.srv.cs.cmu.edu!andrew.cmu.edu!sz24+
- Newsgroups: comp.lang.pascal
- Message-ID: <8f0FTSW00iUxA1MGAe@andrew.cmu.edu>
- Date: Wed, 11 Nov 1992 09:25:34 -0500
- From: "Stephan A. Zdancewic" <sz24+@andrew.cmu.edu>
- Subject: Re: Chess Puzzle
- In-Reply-To: <1dp5hbINNbu4@west.West.Sun.COM>
- References: <1dp5hbINNbu4@west.West.Sun.COM>
- Lines: 75
-
- >This is my first posting to this group, so I hope it even gets there,
- here >goes!
- >I'm trying to work out a solution to the "Eight Queens" chess problem
- whereby y
- >ou
- >try to place 8 queens on a chessboard in such a way that no one queen can get
- >another queen. I've seen a solution to this problem using the "Icon"
- language b
- >ut
- >I,m interrested in a Pascal solution. I figure 2 arrays [1..8] can
- represent >the
- >chessboard and I've worked out a procedure on how to make a
- horizontal,vertical
- > and
- >both diagonals un-usable upon placement of a queen. The problem is in a
- recursi
- >ve
- >procedure to keep trying the next placement and so on. Anybody have any
- clues >or
- >has anyone seen this before. Steer me in the right direction!!!
-
- We just wrote a program in my computer science class to check a chess
- board as you describe to see if it is 'safe.' Based on the techniques
- we used I have several suggestions that I hope are of some help.
- First, it is probably easier to use a two dimensional array [1..8,1..8]
- of boolean. That way you don't have to worry about arrays of arrays -
- that could get messy.
- One way to write this program (although it would be very slow) without
- recursion would be to start at the upper corner of the board. Here's
- some pseudo-code:
-
- start at corner;
- if (row is safe) and (column is safe) and (diagonals are safe) then put
- a queen;
- move to next square;
-
- To check it a given line of squares is safe use code something like this:
-
- function SafeLine(board : boardType; row, col, rowInc, colInc) : boolean;
-
- var Attacking : boolean;
-
- begin
- Attacking := false;
- row := row + rowInc; {These two lines make sure you don't check the 'current'
- col := col + colInc; square}
- while (row > 0) and (row <= 8) and (col > 0) and (col <= 8) and
- not(Attacking)
- do begin
- if board[row, col] then Attacking := true;
- row := row + rowInc;
- col := col + colInc;
- end;
- SafeLine := not(Attacking);
- end;
-
- This function assumes you have a type boardType which is an array of
- booleans. A true represents a queen. This function would be used like
- this
- If SafeLine(board, row, col, 0, 1) then writeln ('The row to the right
- of this square is safe');
- To check diagonals make rowInc and colInc equal to 1 or -1, depending on
- what direction you'd like to check. The northeast diagonal would be -1,
- 1.
-
- This program would generate the same board each time, and it would be
- slow but it would work. I'm sure that there are much faster ways of
- accomplishing this, but I hope this starts you in the right direction.
-
- ............. . . . . . . . . . . . . . . . . .
- . . . . . . . . .
- Steve Zdancewic : [INSERT APROPRIATE QUOTE HERE]
- sz24@andrew.cmu.edu :
- ............. . . . . . . . . . . . . . . . . .
- . . . . . . . . .
-