A Program to Solve the Pentomino Problem by

N
N
D, B. GILLIES, Editor
A Program to Solve the Pentomino
Problem by the Recursive
Use of Macros
JOHN G. FLETCI-[ER
Lawrer,:e Radiation Laboratory, University oj" Califi~rnia,
Livermore, CaliJbrnia
h coding technique is described in which certain macroinstructions are given lists as arguments and are thereby used
recursively. The discussion covers primarily an example in
which the technique is used to solve the pentomino problem
--the problem of fitting 12 pentominos without overlapping into
a plane area formed of 60 elemental squares.
Introduction
The purpose of this report is to describe a technique for
coding with macros which m a y have some general application. The technique can be made clear most readily by
describing its use in a particular instance, namely, in a
program to solve the pentomino problem. This program is
written in the FAil ) language for the I B M 7094.
The Pentornino Problem
i:
~
!
Just as a domino is a platte figure formed of two contiguous equal squares, a pentomino is a plane figure formed
of five contiguous equal squares. A fixed size for the elemental squares being assumed, there are 112 different
pentominos, different meaning noneongruent. These are
each identified by a number front 1 to 12 (see Figure 1).
Ihe pentonfino problem is the problem of fitting the 12
pentomittos without overlapping into a plane area (the
box) formed of 60 elelnetttal squares. The box may be
rectangular (3 X 20, 4 X 16, 5 X 12 or 6 X 10) but is not
necessarily so.
In the program, the box is viewed as a portion of a large
arena, 16 X 32 squares; these squares arc numbered from
1 to 512, 1 to 16 proceeding from left to right across the
first row, 17 to 32 across the second, etc. Each square
The work performed in this paper was supported by the U.S.
Atomic Energy Commission.
Volume 8 / Number 10 / October, 1965
corresponds to one of 512 consecutive storage locations,
square one corresponding to the uppermost, which is just
below a location called ARENA. Art input routine stores
0 in the 60 locations corresponding to the box; the number
13 is stored in the rcmainii~g locations. To avoid difficulty
at the boundaries of the arena, tile box may never include
any square numbered 1-15 or 497-511 or witl~ a number
divisible by 16.
When placed ia the box, the pentominos numbered 1~ 3,
4, 5 and i I m a y assume eight different orientations, numbers 6, 7, 8, 9, and 12 may assume four, number 2 two, and
number 10 one. Thus there arc 63 different pattect~s which
may be placed in the box. A pattern may be uniquely
identified by a set of four umnbers as follows: The pattern
is placed anywhere iu the arena and the munbers of the
five squares covered are noted. The smallest number,
which corresponds to a square called the lead, is then subtracted from each of the other four. The result is the required four numbers. In practice it is convenient to add a
fifth identifying number, namely, the number of the pentomino to which the pattern corresponds. Thus for example, the illustrated orientation of pentomiuo number 7
is the pattern 16, 17, 18, 2, 7. The 63 patterns are assigned
a canonical order in a manner described below.
To the computer, pla@tg a pattern in the box consists of
storing an identifying number in each of the five storage
locations corresponding to the squares thus occupied and
in a storage location corresponding to the particular
pentomino to which the pattern corresponds. (These latter
locations, 12 in number, are initially set to zero by the input rout~itm attd are consecutive and immediately below a
location called MINO.') The l~attern may not be placed if
arty of timse locations already contains a nonzero value,
for this would mean that the ptm, ern is falling outside the
box, that it is overlapping a pattertt previously placed,
FIG. 1. The twelve pentominos
Communications of the ACM
621
a n d / o r that it corresponds to ~ pentomiao ~dre~tdy used
in another orientation. Removing a p ~ t e r n from [he box
consists in zeroing the corresponding six locations. The
identifying numbers are assigned h'om 1 to 12 in the order
that the patterns are plaeed.
The search for solutions consists of an orderly extmustion of possible platings of patterns. The locmions corresponding to the arena are searched dowmvard umil an
empty one is found. The patterns are then searched in
order until one is found that nmy be placed so as 1o have
this location at its lead. Then the downward search for an
empty location continues until another is found, and
another pattern is placed with the new location as lead,
and so forth. If' the list of patterns is exhausted, the search
returns to the previous choice of lead, the pattern with
that lead is removed from tile arena, and the sere'oh is continued with tile next pattern o~ the list. When 12 patterns
have been placed, a solution has been found. An output
routine then uses the information in the A R E N A and
M I N O portions of storage to make an external record of
the solution, after which the last pattern is removed and
the search continues for a new solution. The program
terminates when all arrangements have been attempted.
It is inefficient to test the six locations corresponding to
each pattern every time a new pattern is considered. The
lead location of course is tested only onee. The lead location minus 1 is then tested. If it is not empty a large number (29) of patterns are eliminated, and the lead location
minus 16 is tested. If this location is not empty, no pattern can be placed. On the other hand if lead minus 1 is
empty, then lead minus 2, 16 and 17 are tried, and so
forth. The 7xI1N() locathm coFresp{m~lh G {o 'd. /m.ttern is
tested (mty after lhe o{her {ire /(~</*iot~s/my(, been fomtd
empt:y. Thus the search of /);/t{t~l'ns tins n {tee like sh'uctut'e,
[tow
is t h i s s{d'Hclln'e lo [>e Stff)l'(?(t ill 11/(' n m c h i n e ? I t
was decided to make a. separ:~{e pie<e of coding for each
location (relative to tile lead) to be se;u'che(l, lhese pieces
to be nested and l'e--nested iusi(te cmJ~ other in accordance
with the tree-like strut.lure. This wa> simply a.(:hieved by
the technique with macros which is lhe subject of this report. A study of the contplel,e listit,g (see Appeadix) will
reveal how it works. (The ~l.rrg~ys /A!;AD and PATT are
essentially pushdowu lists to keep track of the leads and
patterns used.)
Tile basic piece of coding is the macro-instruction TEST.
The programmer uses this instructioli only once irl his deck
but with a long second argument which describes as compactly as possible the necess~ry tree-like structure (mid
thereby assigns the canonic~d ()*'dec to the patterns).
Within T E S T , this a r g u l n e l t { , which is of the fot'ln of a
list, is handed ~.o :m I R I ' which breaks it into its separate
items :rod h:mds them ia i:Urlt ;is argument pairs to TEST
again and tlhe process repe~/ts. A counter (K) keeps track
of the depth of nesting, aIld at the appropriate place causes
the macro-instruction I : T E S T to be used ill place of TEST
an thus terminates tile nesting. It nmy be remarked that in
problems for which the nesting lermhmtes at wu'ious
depths (so that a counter cannot be used), ~t'le terminatioa
may be signaled by a unique symbol appropriately placed
throughout the tong second argument of tile origimd TEST
instruction, this symbol to be recogttized by an II<F.
It. should be noted that the behavior of the macro TEST
is very similar to tile operatiol, of a list-processing language
such as l,Ise. [ n fact, the seeond argument of T E S T has a
very LisP-like format. Moreover, lhe unique terminating
symbol referred to above has lhe same function :~s the
LIsP NIL. However, ualike l,~sP, information c~mnot be
handed " u p ; " it ean o~dy be handed "down."
Im
Resuhs
FIt;.2.
622
Some of the solutions obtai~md by t h e p r o g r a . m
C o m m t , r l i e a t i o n ~ o f t h e AC.M
I t may be of some iuieresl, allhough not releva,nt to the
maiI, point, to kn.ow Ill<: resulls of Ill(: l)entomiao progranl.
The program lsed ill pmclice had :u refi,enmnt by which
sohltions differing only by rotalion or reflection front a
solution ah'eady obtained were dimimlted. (For example,
it was demanded that p e n t o m i , o 10 occttr ill the, upper left
quadrant of the 6 X 10 rectangular box.) Also, it) sol:'ted the
solutions ohtai~md into ~ canonical order and then outpul~
them by drawing on a (~l{'l" (see Figure 2). ill) w~s f'oumt
that lhere were 2 solutio ~s f'or the :; X 2(1 box, 368 for tile
4 X 15 box, 1010 for the 5 X 112 box, 2339 for the 6 X 10
box, 2 for a pair of 5 X (i boxes, ~md (;5 for an 8 X 8 box
with a '2 X 2 hole in the ce, ler. (Soluliotls differing by retat:don or reflection are ~,ot multil@ cotlnted.) Total
<qi
v
[:~
A sar,~ pie p ,ge ~f this -utput, is showI~ lie Figure 2. The ~, tpul
rr ~ti :,e made use of I)I)N) sltbrout,itms writt(m by Alex Cecil.
'
):~
V o l u m v 8 / N , , , . l . ~ r JO / ()(qol~er, [965
,%
.,~
;,,
rumfing tiroL, for the ]o~tgesg problem (6 ~ 10 box) is about
10 miuutcs.
A program us(at the same Leclmique -vva.s writA, ert to solve
the hexiamond (el. di~m~ond) problem- Jc[exiarrto~tds colts(st of six contiguous equal equilatera,1 griar~gles a~td ;~re 12
i~ mtmbcr. It w~s found, for example, th~:t.b ~here is no way
to place (,hem it~ a rhomboidal box 3 X 1 2 .
R. M. GRAHAM, Editor
APPENDIX.
TEST
K
K
XX
TEST
TC0
fC0
Listing of the
Program
COUNT
TITLE
8l
MACRO
ZET
IFF
TRA
IFF
TC0
STQ
SET
IRP
IFF
TEST
IFF
FTEST
IRE
SET
STZ
END
EtNEXT,XX
ARENA-Ctl
K-L,OtO
XX*I
K-I,0,I
CONSIDER SQUARE C ( R E L A T I V E TO LEAD).
SEE WHETHER IT IS EMPTY.
(FOR OTHER THAN LEAD (C N O T O ) *
DO NEXT ITEM. I
IF IT IS N0F ENPTY~ TRY NEXT CHOICE FOR C*
(FOR LEAD (C IS O)* DO NEXT I T E M . I
DO AS DESCRIBED IN TED MACFtO.
ARENA-C,[INDICATE SQUARE C TO BE O C C U P I E D g+l
(ADVANCE COUNT OF DEPTH OF MACRO NESTING.)
NEXT
( I T E R A T EDN ITEMS IN NEXT~
WHICH IS A LIST.)
K-S,Ot0
(FOR DEPTH OF NESTING NOT S~ DO NEXT ITEM. I
NEXT
DO AS IN TEST MACRO w I T H ARGUMENTS FROM NEXT.
K-5~Owl
(FOR DEPTH DF NESTING EQUAL ~ i O0 NFXT ITFM*)
NEXT
DO AS IN FTEST MACRO WiTH ARGUMENTS FROM NEXT.
(CEASE ITERATING ON ITEMS I N ~EXT.)
K-I
(REDUCE COUNT OF DEPTH O F M A C R O NESTING,)
ARENA-Cel RESET SQUARE C TO E M P T Y .
SQUARE C AND ITEMS IN NEXT ~OW ALL CONSIDERED.
MACRO
TXI
PXO
STO
END
SFEM~I,I
tl
LEAD,2
ETEST MACRO
ZET
TRA
LET
TRA
STO
STO
STL
TXI
STZ
STZ
FTEST END
THIS PROGRAM GENERATES 7 6 5 E N I S T R U C T I O N S .
I r SOLVES THE PENTONINO pROBLEM BY MACRO RECURSIDN.
LEAD SQUARE HAS BEEN T E S T E D .
IF NOT EMPTY[ TRY NEXT SQUARE AS LEAD.
IF EMPTY[ RECORD ITS N U M B E R
AT PROPER PLACE IN LEAD ARRAYs AND
CONTINUE.
C~M
CONSIDERSQUARE C AND PENTDMINO M.
A R E N A - E l lSEE WHETHER SQUARE C I S EMPTYo
me9
IF NOT, TRY NEXT CHOICE F O R C . OTHERWISE*
MIN0~M
SEE WHETHER PENTOMINO H I S U N U S E D °
*+7
IF NOT, FRY NEXT CHOICE FOR C AND M. 0IHERWISEt
ARENA-C~I INDICATE SQUARE C TO BE O C C U P I E D [
MINO~M INDICATE PENrONI~0 M TO 8E USED,
PATT,2
SAVE CURRENT POINT IN SEARCH OF PATTERNS[ AND
ROOT,Z,1
PROCEED TO TRY TO PLACE A N O T H E R PATTERN.
ARENA-C•IRESET SQUARE C TO EMPTY.
M I N O - M RESETPENTOHINO M TO UNUSED STATUS.
SQUARE C AND PENTOMINO M H A V E UEEN CONSIDERED.
CALL
AXT
AXT
AXC
SET
INPUF~ARENA~MINO
INITIALIZE ARENA AND MIND ARRAYS*
I,!
INDEX I = NO. OF CURRENT LEAD SQUARE.
1,2
INDEX 2 = NO. OF PATTERNS ALREADY PLACED * 1.
1,6
INDEX 6 = - 1 ALWAYS.
1
(g = DEPTH OF MACRO N E S T I N G . I
ROOT
TXH
PXD
IDA
SDLVED,2~I2SOLUTION POUND IF 12 PATTERNS ALREADY PLACED.
~2
SET DECREMENT
OF MQ TO NEW VALUE O F Ir~DEX 2.
STEM
rEST
ETC
EFC
ETC
ETC
ETC
ETC
ETC
ErE
ETC
ETC
ETC
ETC
ETC
ETC
ETC
ETC
ETC
ErO
ETC
OO((OlIIO2I$
BEGIN SEARCH O F PATTERNS.
(03( ( 0 6 , 0 2 ) ( 1 6 e 0 3 ) ( I 7 , 1 1 ) l I B t 11 } ( l ~ , 0 3 ) ) ) $
(16I I I B , 0 6 ) I 1 7 ~ O S I I L R ~ 0 7 ) l 3~,O83 I I $
( 17( ( 10,051 ( 33,06 ) ) )$
( I8( (~9,06) ( 36,0B ) ) ) ) ) ( |6( $
(15((I~06)131,09)II7tOSII32,Ol~|
I$
(17( ( 1B~OSI ( 3 2 t 0 5 I ( 3~* 0 5 I } t g
(32( ( 3 1 ~ 1 2 | 1 3 3 • 0 T ) ( R B ~ 0 3 I I ) ) ) I I T (
( 18( (19~04)133,01)13~073) I t
(33((32tOT) ( 36, 12) (Aq~ 03) I )) ) ) ) ( ~ . 6 | I [ S ( I~
(l.¢lll3,03II3O,12)131,Oll(il~lllI 32~0~1 It$
(31( ( 3 0 ~ 0 9 1 1 1 7 ~ 0 1 ) ( 3 2 , 0 5 1 ( ~ 7 ~ 0 ~ I | I $
( I 7 ( { 18, I I I (3Z, 10)(33,013 ) It,
(321 ( 3 3 , 0 1 I ( 4 8 • 1 1 ) } ) ) )( l T I ss
( 18( ( 0 2 , 0 7 ) ( 1 9 , 0 3 ) (32~D6) ( 3 3 , 0 I I ( 3 ~ 1 2 I ) I •
(32( ( 3t , 0 t ) ( 33,051 (~8, l l ) ) I l i
I 3 3 ( ( 3 6 , 0 9 ) ( 4 9 . 0 4 } ) ) ) ) ( 32( $
(31((30,08) ( ~ 7 ~ 0 4 ) ( 3 3 , 0 6 ) (68~ 1 1 ) I ) $
(33((36,08) ( 68t I I ) ( 4 9 , 0 4 ) ) | $
(RB((67,03)(49~03)(6R~0~)))))
)) I
g
ALL PATTERNS TRIE~ W I T H C U R R E N T LEAD[ SO
IF NO PATTERNS ALREADy PLACEO~ EXIT, ELSE
RECALL PREVIOUS NUMBER OF L E A D SQUAREe
RESTORE IT TD STATUS A S C U R R E N T LEADs
RESET DECREMENT
OF MQ TO REFLECT R E D u c T I O N
I N INDEX Z, AND
RETURN TO EXAMINATION O~ PATTERNS.
FIX
CALL
STEMX CAL
POX
PXD
XCA
TRA*
STEMX,2,I
FINISH
LEAD,2
,1
,Z
SOLVED CALL
AXE
TXI
OUTPUTtARENA,MINO SOLUTION OBTAINED~ SO OUTPUT I T ,
1,~
RESET INOEX 4. l{3 - 1 , ~,ND
STEMX~2~I
RETURN r 0 SEARCH FOR NEW ~ Z T H PA'TTERN.
ARENA BES
~INO
BES
LE'D
BES
0UP
PZE
PATT
BES
END
512
12
12
1,12
,4
0
PATTi2
THIS ARRAY CORRESPONDS TO T H E
S~U&RE~
THIS ARRAY CORRESPONDS TO THE ~
~"
THIS IS A PUSHDOWN LIST OF LEA~E?~OMINO S.
ISINCE INDEX 6 = - I * INDIRECT ADDRESSINGV
~UARE~
THRU THE
PArt ARRAY TRANSFERS TO ~ T D R E O
LOCATION f I )
THIS IS A PUSHDOMN LIST DF PATTERN q
SUBROUTINES gEQUIREG ~RE I ~ I P I ~
" ""
--'*
OUTPUTs FINISH.
I~ECE[VED FEBRUARY,
1965
Volume 8 / N u m b e r 10 / October, 1965
A Fast Storage Allocator
]~{ENNETH @. I{NOWLTON
Bell TelephoneLaboratories, inc., Murray Hill, N. J.
A fast storage bookkeeping method is described which is
particularly appropriate for list-structure operations and other
situations involving many sizes of blocks that are fixed in size
and location. This scheme, used in the LLLLLL or L6 (Bell Telephone Laboratories Low-Level List Language), makes available
blocks of computer registers in several different sizes: the
smaller blocks are obtained by successivMysplitting larger ones
in half, and the larger blocks are reconstituted if and when
their parts are simultaneously free.
Introduetion
A storage allocating and bookkeeping mechanism is
described which makes available blocks of computer
registers in several different sizes, the smaller blocks being
obtained by successively splitting larger ones in half, and
the larger blocks being reconstituted if and when their
parts ace simultaneously free. Each block contains a few
bits of information indicating its size and whether it is
presently free or in use; the rest of the block may be formatted as the user wishes. The operations involved in
obtaining blocks from and returning them to the free
storage lists are very fast, making this scheme particularly
appropriate for list structure operations and for other
situations involving many sizes of blocks which are fixed
in size attd location. This is in fact the storage bookkeeping
method used in the Bell Telephone Laboratories LowLevel List Language t (LLLLLL or Ls, pronounced "Lsixth").
Free Storage Lists
The method to be described is illustrated by an example
in which block sizes are one, two, four attd eight machine
wordsY Figm~ la depicts the originM layout of storage,
i KNOWI:rON,K . C . A programmer's description ot LLLLLL,
Bell Telephone Laboratories lowdevel list language. Bell Tele-.
phone LaboraWries, Inc., Mar. 1965. This language provides for
blocks of the following lengths: 1, 2, ,t, 8, 16, 32, 64 and 128 words.
Upon completing this paper the author has learned that a
similar storage handling system is used in SiMscRiwr where block
sizes are in fact one, two, four arid eight words. For :~ description
of the language, see MAaKOWH'Z,H. M., HAUSNEa, B., and Kault,
tt. W. S I M S C R i P T - - A Simulation Programming Language.
Pretttice-Hall, Englewood Cliff% N. J., 1963.
C o m m u n i c a t i o n s of the ACM
623