Pyth,431个字节
这是我的第一个Pyth程序(实际上这是我使用任何代码高尔夫球语言编写的第一个程序),这意味着它可能仍会得到改进。
Jmmck\:cd\%c.Z"xÚU±Ã@DÅ W,J áDPáÒV`ýüw{g$ÍÀÞÇr§o.÷å8èÝÇr{øºy{~1åõ:noßÃú/.yçíäÂ'ëL¢êF¸èÆ\ka´QÒnÒ@tãÒÁµÆ¾õö»bÍH¥¦$¨%5Eyîÿ}ó§ûrh³oÄåËÄqõ XÔHû"\_KYDrGHFN@JGIn=bm:d.*NHHRb)RHDiNTR.turNG.tT;M:jH)hh@JG0DXGHN=Ti+3qG\^HI!},GTK aK,GT aY+eKNI&!g5T}\EjT)N.q;D(GHNT)INIqHhT=ZrGhtTInZhtTXHZ+eTN))).?I&nHhTgGhtTXHhtT+eTH; aK,di2i1r0.z aY+eKk#=N.(Y0(6\vkN)(7\^kN)(8\v\<N)(9\v\>N)(10\^\<N)(11\^\>N
在这里尝试(最后一个测试用例需要太长的时间,必须使用本地Pyth安装进行测试)。
代码的十六进制转储(用于xxd -r <filename>
解码):
00000000: 4a6d 6d63 6b5c 3a63 645c 2563 2e5a 2278 Jmmck\:cd\%c.Z"x
00000010: da55 8eb1 8ac3 400c 447f c58d 2057 2c99 .U....@.D... W,.
00000020: 4aa0 e144 50e1 d2ad 5660 87fd 84fc 7f77 J..DP...V`.....w
00000030: 7b67 1f24 cdc0 8319 de1c c772 a76f 2ef7 {g.$.......r.o..
00000040: e538 e8dd c772 7bf8 9eba 797b 7e31 e5f5 .8...r{...y{~1..
00000050: 8e3a 6e8f 6fdf c3fa 2f2e 0c79 e717 ede4 .:n.o.../..y....
00000060: c21f 27eb 8395 189a 4c15 140b a28d ea82 ..'.....L.......
00000070: 46b8 e8c6 5c05 1b6b 1d61 b490 0251 d28c F...\..k.a...Q..
00000080: 6ed2 4087 74e3 1ad2 c1b5 c6be f5f6 1cbb n.@.t...........
00000090: 6286 cd48 a5a6 24a8 2535 4579 eeff 7df3 b..H..$.%5Ey..}.
000000a0: 8a8a 1613 a7fb 7204 68b3 6fc4 e51b 160c ......r.h.o.....
000000b0: 1304 cbc4 8a71 f57f 2058 d448 fb22 5c5f .....q.. X.H."\_
000000c0: 4b59 4472 4748 464e 404a 4749 6e3d 626d KYDrGHFN@JGIn=bm
000000d0: 3a64 2e2a 4e48 4852 6229 5248 4469 4e54 :d.*NHHRb)RHDiNT
000000e0: 522e 7475 724e 472e 7454 3b4d 3a6a 4829 R.turNG.tT;M:jH)
000000f0: 6868 404a 4730 4458 4748 4e3d 5469 2b33 hh@JG0DXGHN=Ti+3
00000100: 7147 5c5e 4849 217d 2c47 544b 2061 4b2c qG\^HI!},GTK aK,
00000110: 4754 2061 592b 654b 4e49 2621 6735 547d GT aY+eKNI&!g5T}
00000120: 5c45 6a54 294e 2e71 3b44 2847 484e 5429 \EjT)N.q;D(GHNT)
00000130: 494e 4971 4868 543d 5a72 4768 7454 496e INIqHhT=ZrGhtTIn
00000140: 5a68 7454 5848 5a2b 6554 4e29 2929 2e3f ZhtTXHZ+eTN))).?
00000150: 4926 6e48 6854 6747 6874 5458 4868 7454 I&nHhTgGhtTXHhtT
00000160: 2b65 5448 3b20 614b 2c64 6932 6931 7230 +eTH; aK,di2i1r0
00000170: 2e7a 2061 592b 654b 6b23 3d4e 2e28 5930 .z aY+eKk#=N.(Y0
00000180: 2836 5c76 6b4e 2928 375c 5e6b 4e29 2838 (6\vkN)(7\^kN)(8
00000190: 5c76 5c3c 4e29 2839 5c76 5c3e 4e29 2831 \v\<N)(9\v\>N)(1
000001a0: 305c 5e5c 3c4e 2928 3131 5c5e 5c3e 4e 0\^\<N)(11\^\>N
说明
该程序的主要思想是使用正则表达式来修改输入。为了节省空间,所有这些正则表达式都包含在压缩字符串中。该程序的第一步是解压缩该字符串,并将其拆分为单个正则表达式和相应的替换字符串。
.Z"..." Decompress the string
c \_ Split the result into pieces (separator is "_")
m cd\% Split all pieces (separator is "%")
m ck\: Split all sub-pieces (separator is ":")
J Assign the result to variable J
变量的内容如下J
:
[[['\\\\ /', '=M='], ['/ \\\\', '=W=']],
[[' (?=[V6M=-])', 'V'], ['o(?=[V6M=-])', '6']],
[['(?<=[A9W=-]) ', 'A'], ['(?<=[A9W=-])o', '9'], ['(?<=[X0W=-])V', 'X'], ['(?<=[X0W=-])6', '0']],
[['6V', 'V6'], ['0X', 'X0'], ['6-', '6='], ['0-', '0='], ['6M', 'VE'], ['0M', 'XE']],
[['A9', '9A'], ['X0', '0X'], ['-9', '=9'], ['-0', '=0'], ['W9', 'EA'], ['W0', 'EX']],
[['[MW-]']],
[['[60]']],
[['[90]']],
[['V6', '6V'], ['V0', '6X'], ['X6', '0V'], ['X0', '0X']],
[['6V', 'V6'], ['0V', 'X6'], ['6X', 'V0'], ['0X', 'X0']],
[['A9', '9A'], ['A0', '9X'], ['X9', '0A'], ['X0', '0X']],
[['9A', 'A9'], ['0A', 'X9'], ['9X', 'A0'], ['0X', 'X0']]]
KY Set the variable K to an empty list
该函数r
将J
索引中存储的列表中的正则表达式替换应用于列表中的G
所有字符串H
。只要更改了任何字符串,它就会返回。
DrGH Define the function r(G,H)
FN@JG ) Loop for all entries in J[G]
m:d.*NH Regex substitution, replace N[0] with N[1] in all strings in list H
=b Store the result in variable b
In HRb If b != H return b
RH Return H
功能i
类似于功能,但r
有2个区别。它将替换应用于转置列表(垂直而不是水平)。只要更改任何内容,它还会重复执行替换。
DiNT ; Define the function i(N,T)
.tT Transpose the list T
urNG Apply r(N,...) repeatedly as long as something changes
R.t Transpose the result back and return it
该函数g
检查是否可以在列表中的任何字符串中找到存储在J
索引中的列表中的正则表达式。G
H
M Define the function g(G,H)
hh@JG Get the single regex stored in J[G]
jH) Join all strings in H
: 0 Check if the regex is found anywhere in the joined string
其余代码包含实际的程序逻辑。它对可能的运动执行广度优先搜索,直到找到解决方案为止。搜索树中的位置由重力方向和程序输入的修改后的副本唯一定义。为了避免一次又一次地处理相同的位置,已处理的位置会存储在全局列表中K
。仍然需要处理的位置与解决方案的相应部分一起存储在列表中Y
。
输入和初始化的修改K
和Y
由下面的代码执行的:
.z Get all input as a line list
i2i1r0 Apply the regular expressions stored in J[0] horizontally, and the the ones from J[1] and J[2] vertically
,d Create a list with " " (represents "no gravity set") and the modifed input
aK Append the result to the list K
eK Retrieve the appended list again
+ k Append "" to the list (represents the empty starting solution)
aY Append the result to the list Y
输入修改的行为类似于以下内容。输入:
---- --/ \
--- --
o
------ -----
转换为:
VVVVVVVVV----VVV--=W=
---VVVV--AAAXVVVXAAAA
9AXVVVVXAAAAXVVVXAAAA
AAXVVVVXAAAAXVVVXAAAA
AA------AAAA-----AAAA
这些值具有以下含义:
-
必须仍然访问的平台
=
不再需要访问的平台
M
可在重力设为“向下”的情况下输入杯子
W
可在重力设置为“ up”的情况下输入杯子
V
可以将重力设置为“向下”,安全地移动到该位置
A
可以将重力设置为“向上”,安全地移动到该位置
X
无论重力设置如何,都可以安全地移动到这个地方
6
在标有“ V
9
在标有“ A
0
在标有“ X
逻辑是使用正则表达式执行运动。在上面的示例中,如果将重力设置为“ up”,我们可以用正则表达式将“ 9A”替换为“ A9”,以将球向右移动。这意味着通过尝试使用正则表达式,我们可以找到所有可能的运动。
该功能X
根据当前重力设置执行垂直球的运动,将结果存储在全局列表K
和中Y
,并检查是否找到了解决方案。
DXGHN ; Define the function X(G,H,N)
+3qG\^ Select the correct set of regular expressions based on the current gravity setting G (3 for "v" and 4 for "^")
=Ti H Apply i(...,H) and store the result in T
I!},GTK If [G,T] not in K
aK,GT Store [G,T] in K
aY+eKN Store [G,T,N] in Y
I&!g5T}\EjT) If J[5] not found in T and T contains "E" (all platforms visited and ball in cup)
N.q Print N and exit
该功能(
实现对4个方向/重力按钮的检查。仅当当前重力会发生变化并且球处于安全的位置以改变重力时,才能按下重力按钮。只有在可以安全地移动到相应位置的情况下,才能按下方向按钮。
D(GHNT) ; Define the function ( (G,H,N,T)
IN ) If N is not empty (contains either "<" or ">" representing directional buttons)
IqHhT ) If H (gravity setting for which this test is performed) is equal T[0] (the current gravity)
=ZrGhtT Apply r(G,T[1]) and store the result in Z (G is the appropriate regex index for the combination of gravity and directional button, T[1] is the current modified input)
InZhtT ) If Z != T[1] (the regex operation changed something, meaning we found a valid move)
XHZ+eTN Call X(H,Z,[T[2],N])
.? Else (gravity button pressed)
I If ...
nHhT H (new gravity setting) is not equal T[0] (current gravity setting)
& ... and ...
gGhtT J[G] found in T[1] (ball is in an appropriate place to switch gravity)
XHhtT+eTH Call X(H,T[1],[T[2],H])
最后是主循环。的第一个元素Y
被重复删除,并检查所有可能的移动。
# Loop until error (Y empty)
=N.(Y0 Pop first element of Y and store it in the variable N
(6\vkN) Call ( (6,"v","",N)
(7\^kN) Call ( (7,"^","",N)
(8\v\<N) Call ( (8,"v","<",N)
(9\v\>N) Call ( (9,"v",">",N)
(10\^\<N) Call ( (10,"^","<",N)
(11\^\>N Call ( (11,"^",">",N)