Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!cbosgd!ihnp4!inuxc!pur-ee!pucc-j!rsk From: rsk@pucc-j (Wombat) Newsgroups: net.lang.prolog Subject: Re: PROLOG Digest V4 #22 Message-ID: <1389@pucc-j> Date: Fri, 20-Jun-86 21:16:20 EDT Article-I.D.: pucc-j.1389 Posted: Fri Jun 20 21:16:20 1986 Date-Received: Sun, 22-Jun-86 04:26:08 EDT References: <8606180752.AA07915@ucbvax.Berkeley.EDU> Reply-To: rsk@pucc-j.UUCP (Wombat) Organization: Purdue University Lines: 50 Keywords: eight queens Here is another solution to the eight queens problem; I found this one easier to understand, though others may differ on that point. It was written by Malcolm Slaney while he was at Purdue University; he is reachable at pur-ee!malcolm. % All of the work is done by not_threaten, queens and validlist. findqueens :- repeat, % Find all possible solutions queens([[0,Y0],[1,Y1],[2,Y2],[3,Y3],[4,Y4],[5,Y5], [6,Y6],[7,Y7]|[]]), write([0,Y0]), write(' '), write([1,Y1]), write(' '), write([2,Y2]), write(' '), write([3,Y3]), write(' '), write([4,Y4]), write(' '), write([5,Y5]), write(' '), write([6,Y6]), write(' '), write([7,Y7]), write(' '), nl, fail. not_threaten([X1,Y1],[X2,Y2]) :- % Check to see if two queens are ok not(X1 = X2), not(Y1 = Y2), D1 is X1-X2, D2 is Y1-Y2, D3 is -D2, not(D1 = D2), not(D1 = D3). queens([]). % Generate Valid list of Queens queens([[X,Y]|List]) :- queens(List), isvalid(Y), validlist(X,Y,List). validlist(X,Y,[]). % Check to see if List is OK. validlist(X1,Y1,[[X2,Y2]|List]) :- not_threaten([X1,Y1],[X2,Y2]), validlist(X1,Y1,List). isvalid(0). % Generate Valid Board Positions isvalid(1). % (There must to be a better way!!!) isvalid(2). isvalid(3). isvalid(4). isvalid(5). isvalid(6). isvalid(7). -- Rich Kulawiec, pucc-j!rsk, rsk@j.cc.purdue.edu