Add the tiger source code bundle from the book site
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
parent
915660c8a7
commit
33d8bac511
87 changed files with 3252 additions and 0 deletions
34
tiger/testcases/queens.tig
Normal file
34
tiger/testcases/queens.tig
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* A program to solve the 8-queens problem */
|
||||
|
||||
let
|
||||
var N := 8
|
||||
|
||||
type intArray = array of int
|
||||
|
||||
var row := intArray [ N ] of 0
|
||||
var col := intArray [ N ] of 0
|
||||
var diag1 := intArray [N+N-1] of 0
|
||||
var diag2 := intArray [N+N-1] of 0
|
||||
|
||||
function printboard() =
|
||||
(for i := 0 to N-1
|
||||
do (for j := 0 to N-1
|
||||
do print(if col[i]=j then " O" else " .");
|
||||
print("\n"));
|
||||
print("\n"))
|
||||
|
||||
function try(c:int) =
|
||||
( /* for i:= 0 to c do print("."); print("\n"); flush();*/
|
||||
if c=N
|
||||
then printboard()
|
||||
else for r := 0 to N-1
|
||||
do if row[r]=0 & diag1[r+c]=0 & diag2[r+7-c]=0
|
||||
then (row[r]:=1; diag1[r+c]:=1; diag2[r+7-c]:=1;
|
||||
col[c]:=r;
|
||||
try(c+1);
|
||||
row[r]:=0; diag1[r+c]:=0; diag2[r+7-c]:=0)
|
||||
|
||||
)
|
||||
in try(0)
|
||||
end
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue