annotate ppc/test/queens2.p @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
mike@0 1 (* N queens with array of choices *)
mike@0 2
mike@0 3 const N = 8;
mike@0 4
mike@0 5 proc queens(k: integer; var choice: array N of integer);
mike@0 6 var y, j, q: integer; ok: boolean;
mike@0 7 begin
mike@0 8 if k = N then
mike@0 9 print(choice)
mike@0 10 else
mike@0 11 y := 0;
mike@0 12 while y < N do
mike@0 13 j := 0; ok := true;
mike@0 14 while ok and (j < k) do
mike@0 15 q := choice[j];
mike@0 16 ok := (q <> y) and (q+j <> y+k) and (q-j <> y-k);
mike@0 17 j := j+1
mike@0 18 end;
mike@0 19 if ok then
mike@0 20 choice[k] := y;
mike@0 21 queens(k+1, choice)
mike@0 22 end;
mike@0 23 y := y+1
mike@0 24 end
mike@0 25 end
mike@0 26 end;
mike@0 27
mike@0 28 proc print(var choice: array N of integer);
mike@0 29 var x: integer;
mike@0 30 begin
mike@0 31 x := 0;
mike@0 32 while x < N do
mike@0 33 print_num(choice[x]+1);
mike@0 34 x := x+1
mike@0 35 end;
mike@0 36 newline()
mike@0 37 end;
mike@0 38
mike@0 39 var choice: array N of integer;
mike@0 40
mike@0 41 begin
mike@0 42 queens(0, choice)
mike@0 43 end.
mike@0 44
mike@0 45 (*<<
mike@0 46 15863724
mike@0 47 16837425
mike@0 48 17468253
mike@0 49 17582463
mike@0 50 24683175
mike@0 51 25713864
mike@0 52 25741863
mike@0 53 26174835
mike@0 54 26831475
mike@0 55 27368514
mike@0 56 27581463
mike@0 57 28613574
mike@0 58 31758246
mike@0 59 35281746
mike@0 60 35286471
mike@0 61 35714286
mike@0 62 35841726
mike@0 63 36258174
mike@0 64 36271485
mike@0 65 36275184
mike@0 66 36418572
mike@0 67 36428571
mike@0 68 36814752
mike@0 69 36815724
mike@0 70 36824175
mike@0 71 37285146
mike@0 72 37286415
mike@0 73 38471625
mike@0 74 41582736
mike@0 75 41586372
mike@0 76 42586137
mike@0 77 42736815
mike@0 78 42736851
mike@0 79 42751863
mike@0 80 42857136
mike@0 81 42861357
mike@0 82 46152837
mike@0 83 46827135
mike@0 84 46831752
mike@0 85 47185263
mike@0 86 47382516
mike@0 87 47526138
mike@0 88 47531682
mike@0 89 48136275
mike@0 90 48157263
mike@0 91 48531726
mike@0 92 51468273
mike@0 93 51842736
mike@0 94 51863724
mike@0 95 52468317
mike@0 96 52473861
mike@0 97 52617483
mike@0 98 52814736
mike@0 99 53168247
mike@0 100 53172864
mike@0 101 53847162
mike@0 102 57138642
mike@0 103 57142863
mike@0 104 57248136
mike@0 105 57263148
mike@0 106 57263184
mike@0 107 57413862
mike@0 108 58413627
mike@0 109 58417263
mike@0 110 61528374
mike@0 111 62713584
mike@0 112 62714853
mike@0 113 63175824
mike@0 114 63184275
mike@0 115 63185247
mike@0 116 63571428
mike@0 117 63581427
mike@0 118 63724815
mike@0 119 63728514
mike@0 120 63741825
mike@0 121 64158273
mike@0 122 64285713
mike@0 123 64713528
mike@0 124 64718253
mike@0 125 68241753
mike@0 126 71386425
mike@0 127 72418536
mike@0 128 72631485
mike@0 129 73168524
mike@0 130 73825164
mike@0 131 74258136
mike@0 132 74286135
mike@0 133 75316824
mike@0 134 82417536
mike@0 135 82531746
mike@0 136 83162574
mike@0 137 84136275
mike@0 138 >>*)
mike@0 139
mike@0 140 (*[[
mike@0 141 MODULE Main 0 0
mike@0 142 IMPORT Lib 0
mike@0 143 ENDHDR
mike@0 144
mike@0 145 PROC _queens 16 0 0
mike@0 146 ! if k = N then
mike@0 147 LDLW 16
mike@0 148 CONST 8
mike@0 149 JNEQ L2
mike@0 150 ! print(choice)
mike@0 151 LDLW 20
mike@0 152 CONST 0
mike@0 153 GLOBAL _print
mike@0 154 PCALL 1
mike@0 155 JUMP L3
mike@0 156 LABEL L2
mike@0 157 ! y := 0;
mike@0 158 CONST 0
mike@0 159 STLW -4
mike@0 160 ! while y < N do
mike@0 161 JUMP L5
mike@0 162 LABEL L4
mike@0 163 ! j := 0; ok := true;
mike@0 164 CONST 0
mike@0 165 STLW -8
mike@0 166 CONST 1
mike@0 167 STLC -13
mike@0 168 ! while ok and (j < k) do
mike@0 169 JUMP L8
mike@0 170 LABEL L7
mike@0 171 ! q := choice[j];
mike@0 172 LDLW 20
mike@0 173 LDLW -8
mike@0 174 LDIW
mike@0 175 STLW -12
mike@0 176 ! ok := (q <> y) and (q+j <> y+k) and (q-j <> y-k);
mike@0 177 LDLW -12
mike@0 178 LDLW -4
mike@0 179 JEQ L12
mike@0 180 LDLW -12
mike@0 181 LDLW -8
mike@0 182 PLUS
mike@0 183 LDLW -4
mike@0 184 LDLW 16
mike@0 185 PLUS
mike@0 186 JEQ L12
mike@0 187 LDLW -12
mike@0 188 LDLW -8
mike@0 189 MINUS
mike@0 190 LDLW -4
mike@0 191 LDLW 16
mike@0 192 MINUS
mike@0 193 JEQ L12
mike@0 194 CONST 1
mike@0 195 JUMP L13
mike@0 196 LABEL L12
mike@0 197 CONST 0
mike@0 198 LABEL L13
mike@0 199 STLC -13
mike@0 200 ! j := j+1
mike@0 201 LDLW -8
mike@0 202 CONST 1
mike@0 203 PLUS
mike@0 204 STLW -8
mike@0 205 LABEL L8
mike@0 206 LDLC -13
mike@0 207 JNEQZ L10
mike@0 208 JUMP L9
mike@0 209 LABEL L10
mike@0 210 LDLW -8
mike@0 211 LDLW 16
mike@0 212 JLT L7
mike@0 213 LABEL L9
mike@0 214 ! if ok then
mike@0 215 LDLC -13
mike@0 216 JNEQZ L16
mike@0 217 JUMP L18
mike@0 218 LABEL L16
mike@0 219 ! choice[k] := y;
mike@0 220 LDLW -4
mike@0 221 LDLW 20
mike@0 222 LDLW 16
mike@0 223 STIW
mike@0 224 ! queens(k+1, choice)
mike@0 225 LDLW 20
mike@0 226 LDLW 16
mike@0 227 CONST 1
mike@0 228 PLUS
mike@0 229 CONST 0
mike@0 230 GLOBAL _queens
mike@0 231 PCALL 2
mike@0 232 LABEL L18
mike@0 233 ! y := y+1
mike@0 234 LDLW -4
mike@0 235 CONST 1
mike@0 236 PLUS
mike@0 237 STLW -4
mike@0 238 LABEL L5
mike@0 239 LDLW -4
mike@0 240 CONST 8
mike@0 241 JLT L4
mike@0 242 LABEL L3
mike@0 243 RETURN
mike@0 244 END
mike@0 245
mike@0 246 PROC _print 4 0 0
mike@0 247 ! x := 0;
mike@0 248 CONST 0
mike@0 249 STLW -4
mike@0 250 ! while x < N do
mike@0 251 JUMP L20
mike@0 252 LABEL L19
mike@0 253 ! print_num(choice[x]+1);
mike@0 254 LDLW 16
mike@0 255 LDLW -4
mike@0 256 LDIW
mike@0 257 CONST 1
mike@0 258 PLUS
mike@0 259 CONST 0
mike@0 260 GLOBAL lib.print_num
mike@0 261 PCALL 1
mike@0 262 ! x := x+1
mike@0 263 LDLW -4
mike@0 264 CONST 1
mike@0 265 PLUS
mike@0 266 STLW -4
mike@0 267 LABEL L20
mike@0 268 LDLW -4
mike@0 269 CONST 8
mike@0 270 JLT L19
mike@0 271 ! newline()
mike@0 272 CONST 0
mike@0 273 GLOBAL lib.newline
mike@0 274 PCALL 0
mike@0 275 RETURN
mike@0 276 END
mike@0 277
mike@0 278 PROC MAIN 0 0 0
mike@0 279 ! queens(0, choice)
mike@0 280 GLOBAL _choice
mike@0 281 CONST 0
mike@0 282 CONST 0
mike@0 283 GLOBAL _queens
mike@0 284 PCALL 2
mike@0 285 RETURN
mike@0 286 END
mike@0 287
mike@0 288 GLOVAR _choice 32
mike@0 289 ! End
mike@0 290 ]]*)