forked from moshix/mvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nqueens.pli
79 lines (72 loc) · 4.68 KB
/
nqueens.pli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
NQUEENS: PROC OPTIONS (MAIN);
DCL A(35) BIN FIXED(31) EXTERNAL;
DCL COUNT BIN FIXED(31) EXTERNAL;
COUNT = 0;
DECLARE SYSIN FILE;
DCL ABS BUILTIN;
DECLARE SYSPRINT FILE;
DECLARE N BINARY FIXED (31); /* COUNTER */
/* MAIN LOOP STARTS HERE */
GET LIST (N) FILE(SYSIN); /* N QUEENS, N X N BOARD */
PUT SKIP (1) FILE(SYSPRINT);
PUT SKIP LIST('BEGIN N QUEENS PROCESSING *****') FILE(SYSPRINT);
PUT SKIP LIST('SOLUTIONS FOR N: ',N) FILE(SYSPRINT);
PUT SKIP (1) FILE(SYSPRINT);
IF N < 4 THEN DO;
/* LESS THAN 4 MAKES NO SENSE */
PUT SKIP (2) FILE(SYSPRINT);
PUT SKIP LIST (N,' N TOO LOW') FILE (SYSPRINT);
PUT SKIP (2) FILE(SYSPRINT);
RETURN (1);
END;
IF N > 35 THEN DO;
/* WOULD TAKE WEEKS */
PUT SKIP (2) FILE(SYSPRINT);
PUT SKIP LIST (N,' N TOO HIGH') FILE (SYSPRINT);
PUT SKIP (2) FILE(SYSPRINT);
RETURN (1);
END;
CALL QUEEN(N);
PUT SKIP (2) FILE(SYSPRINT);
PUT SKIP LIST (COUNT,' SOLUTIONS FOUND') FILE(SYSPRINT);
PUT SKIP (1) FILE(SYSPRINT);
PUT SKIP LIST ('END OF PROCESSING ****') FILE(SYSPRINT);
RETURN(0);
/* MAIN LOOP ENDS ABOVE */
PLACE: PROCEDURE (PS);
DCL PS BIN FIXED(31);
DCL I BIN FIXED(31) INIT(0);
DCL A(50) BIN FIXED(31) EXTERNAL;
DO I=1 TO PS-1;
IF A(I) = A(PS) THEN RETURN(0);
IF ABS ( A(I) - A(PS) ) = (PS-I) THEN RETURN(0);
END;
RETURN (1);
END PLACE;
QUEEN: PROCEDURE (N);
DCL N BIN FIXED (31);
DCL K BIN FIXED (31);
DCL A(50) BIN FIXED(31) EXTERNAL;
DCL COUNT BIN FIXED(31) EXTERNAL;
K = 1;
A(K) = 0;
DO WHILE (K > 0);
A(K) = A(K) + 1;
DO WHILE ( ( A(K)<= N) & (PLACE(K) =0) );
A(K) = A(K) +1;
END;
IF (A(K) <= N) THEN DO;
IF (K = N ) THEN DO;
COUNT = COUNT + 1;
END;
ELSE DO;
K= K +1;
A(K) = 0;
END; /* OF INSIDE ELSE */
END; /* OF FIRST IF */
ELSE DO;
K = K -1;
END;
END; /* OF EXTERNAL WHILE LOOP */
END QUEEN;
END NQUEENS;