BBC BASIC,570 514 490字节ASCII
在http://www.bbcbasic.co.uk/bbcwin/download.html下载口译员
435个字节标记
完整程序L.bmp
在屏幕上显示输入,然后对其进行修改以找到解决方案。
*DISPLAY L
t=PI/8q=FNa(1)
DEFFNa(n)IFn=7END
LOCALz,j,p,i,c,s,x,y,m,u,v
F.z=0TO99u=z MOD10*100v=z DIV10*100ORIGINu,v
F.j=0TO12S.4p=0F.i=j+3TOj+9S.2c=9*COS(i*t)s=9*SIN(i*t)p=p*4-(POINT(c,s)<>0)*2-(POINT(9*c,9*s)<>0)N.
m=n:IFn=5A.(43A.p)=0p=0m=7
IF(ASCM."??O|(C",n)-64A.p)=0THEN
F.i=-1TO0GCOL0,-i*n:c=99*COS(j*t)s=99*SIN(j*t)y=402/3^m MOD3-1MOVE-c-s*y,c*y-s:x=n<3MOVEc*x-s*x,s*x+c*x:x=2778/3^m MOD3-1y=5775/3^m MOD3-1PLOT85-32*(n MOD6>3),c*x-s*y,s*x+c*y:IFi q=FNa(n+1)ORIGINu,v
N.
ENDIF
N.N.=0
说明
请注意,在BBC基本中,1像素= 2单位的距离,因此50x50像素网格变为100x100网格。
我们使用递归函数将2个大三角形,中三角形,正方形和平行四边形放置到形状中。在进行下一个递归调用之前,将绘制列表中较早的形状。如果递归调用返回但没有找到解决方案,则将较早的形状覆盖为黑色,并尝试使用较早的形状的新位置。
绘制完这五个形状后,放置两个小三角形仅仅是一种形式。但是,有必要绘制其中之一,以便在它们具有共同优势的情况下使它们消失。我们只给两个小三角形之一上色。另一个留在自然黑色中。
尝试在不同的x,y坐标和4个不同的旋转位置放置每种形状。为了测试是否有自由空间可以绘制形状,我们使用下面的模板(角度为45度)。围绕进行旋转,*
测试的8个像素位于半径为9和81单位的2个半圆中,并落在辐射线上,相对于x和y轴的奇数倍为22.5度。
对于大三角形,所有8个空格都必须清除。对于其他形状,只有某些单元格必须是透明的,因此要使用遮罩。
+----+---- Shape Mask HGFEDCBA Mask decimal
|\ E/|\G /
| \/F|H\/ 1,2. Large triangle 11111111 -1
|C/\ | / 3. Med triangle 00001111 15
|/ D\|/ 4. Square 00111100 60
+----* 5. Parallelogram 11101000 -24
|\ B/ 6. Small triangle 00000011 3
|A\/ 7. Parallogr reversed 00101011 43
| / Note: reversed parallelogram is checked/drawn at recursion depth n=5
|/ with a special check, but the coordinates are encoded as m=7.
一旦确定形状适合,就必须绘制它。如果是三角形,则用绘制PLOT 85
,如果是平行四边形,则数字高32(请注意,出于PLOT
目的,我们将正方形视为特殊的平行四边形)。无论哪种情况,都必须给出3个连续的顶点。第二个顶点是形状的原点(*
在上表中标记),大三角形除外(在旋转之前)是大三角形-1,-1.
。其他两个顶点可以具有-1,0 or 1
从底数3提取的x和y坐标编码的数字,然后通过99缩放,并通过与转动变换为必要c
和s
。
非高尔夫代码
*DISPLAY L
t=PI/8 :REM Constant 22.5 degrees.
q=FNa(1) :REM Call function, return dummy value to q
END :REM End the program gracefully if no solution. Absent in golfed version.
DEFFNa(n) :REM Recursive function to place shapes.
IFn=7END :REM If n=7 solution found, end program.
LOCALk,z,j,p,i,c,s,x,y,m,u,v :REM declare local variables for function.
k=ASCMID$("??O|(C",n)-64 :REM Bitmasks for big tri, big tri, med tri, sq, normal paralellogram, small tri.
FORz=0TO99 :REM For each point on the grid
u=z MOD10*100:v=z DIV10*100 :REM calculate its x and y coordinates relative to bottom left of screen
ORIGINu,v :REM and set the origin to this point.
FORj=0TO12STEP4 :REM For each rotation 0,90,180,270deg
p=0 :REM assume no non-black pixels found
FORi=j+3TOj+9STEP2 :REM test angles of 3,5,7,9 times 22.5 deg anticlockwise from right x axis.
c=9*COS(i*t) :REM Coords of test points at radius ll
s=9*SIN(i*t)
p*=4 :REM Leftshift any existing data in p
p-=(POINT(c,s)<>0)*2+(POINT(9*c,9*s)<>0) :REM and check pixels at radius 11 and 99.
NEXT
m=n :REM The index of the shape to plot normally corresponds with recursion depth n.
IF n=5 AND (43ANDp)=0 p=0:m=7 :REM If n=5 check if a reverse parallelogram is possible (mask 43). If so, clear p and change m to 7.
REM :REM Check p against mask k, if the shape fits then...
IF (k ANDp)=0 THEN
FOR i=-1 TO 0 :REM draw the shape in colour, and if deeper recursions prove unsuccesful, redraw it in black.
GCOL0,-i*n :REM Colour is equal to n.
c=99*COS(j*t) :REM Set parameters c and s for scaling by 99
s=99*SIN(j*t) :REM and rotation by 0,90,180 or 270 as appropriate.
x=-1 :REM For vertex 1, x=-1 always.
y=402/3^m MOD3-1 :REM Lookup y value for vertex 1.
MOVEc*x-s*y,s*x+c*y :REM Use c and s to transform the vertex and move to it.
x=n<3 :REM For vertex 2, coords are 0,0 except for large triangle where they are -1,-1
y=x :REM in BBC BASIC, TRUE=-1
MOVEc*x-s*y,s*x+c*y :REM Use c and s to transform the vertex and move to it.
x=2778/3^m MOD3-1 :REM Lookup x and y value for vertex 3.
y=5775/3^m MOD3-1 :REM PLOT85 uses last 2 points + specified point to make triangle, PLOT85+32 makes paralelogram (or square.)
PLOT85-32*(n MOD6>3),c*x-s*y,s*x+c*y :REM Use c and s to transform the vertex and draw shape.
IFi q=FNa(n+1):ORIGINu,v :REM If i=-1 recurse to next level. If it fails, reset the origin before replotting this level's shape in black.
NEXT
ENDIF
NEXT
NEXT
=0 :REM Dummy value to return from function
输出量
这是程序为测试用例找到的解决方案的蒙太奇。由于打高尔夫球,使用99而不是100会留下一些小的黑洞。由于在搜索过程中会重新绘制形状,因此在某些情况下可能需要花费几秒钟来运行,并且观看起来非常有趣。