路径与浪费时间


22

前提

因此,最近我约有半个小时的时间去预约,并决定在外面等。我还确定,如果我一动不动地站在房子前面,那看起来会很奇怪。因此,我决定在有限的区域内快速散步。我还得出结论,如果我开始绕圈子行走,很明显我在游荡。因此,我受到启发,创造了我的第一个高尔夫挑战赛。

规格

您将得到一张列表,一张区域地图,其中将包含" ""#",它们代表自由空间和某种障碍。自由空间只能越过一次,越过它需要1分钟。您的初始位置将以"@"无赖的传统来表示,目标将以来表示,"$"因为这就是您将要失去的位置。您还将获得一个整数,该整数表示在看上去好像没有被入侵之前必须浪费多少分钟。当您降落在"$",它必须是确切的分钟数(因此,如果您要递减计数,则在相邻图块上必须为1,在图块上必须为0)。始终可以到达目的地。您的程序或函数将必须返回一个列表,该列表显示带有<,>,^和v的最短路径,以表示四个可能的方向。

例子

输入:

[[" ", " ", " ", " "],
 ["@", " ", " ", "$"],
 [" ", " ", " ", " "],
 [" ", " ", " ", " "]]

5

输出:

[[">", ">", ">", "v"],
 ["^", " ", " ", "$"],
 [" ", " ", " ", " "],
 [" ", " ", " ", " "]]

输入:

[[" ", "#", " ", " ", " "],
 [" ", "#", " ", " ", " "],
 ["@", "#", " ", "$", " "],
 [" ", " ", " ", " ", " "],
 [" ", "#", " ", " ", " "],
 [" ", "#", " ", " ", " "]]

7

输出:

[[" ", "#", " ", " ", " "],
 [" ", "#", ">", "v", " "],
 ["v", "#", "^", "$", " "],
 [">", ">", "^", " ", " "],
 [" ", "#", " ", " ", " "],
 [" ", "#", " ", " ", " "]]

输入:

[[" ", "#", " ", " ", " "],
 [" ", "#", " ", " ", " "],
 ["@", "#", " ", "$", " "],
 [" ", " ", " ", " ", " "],
 [" ", "#", " ", " ", " "],
 [" ", "#", " ", " ", " "]]

17

输出:

[[" ", "#", " ", "v", "<"],
 [" ", "#", " ", "v", "^"],
 ["v", "#", " ", "$", "^"],
 [">", ">", "v", ">", "^"],
 [" ", "#", "v", "^", "<"],
 [" ", "#", ">", ">", "^"]]

规则

  • 适用标准漏洞
  • 每个磁贴只能移动一次
  • 必须在板上花费确切的时间
  • 如果是多条路径,则仅需要显示一条路径
  • 这是一个打高尔夫球的代码问题,所以最短的答案会胜出
  • 根据评论中user202729的问题,您可以假定输入有效。

如果需要进一步说明,请添加评论


1
是否保证“将始终有可能在指定的时间内到达目的地”?
user202729 '18

是的,它将永远存在,即使是令人费解的方法
LForchini

5
欢迎来到PPCG!:)不错的第一个挑战。
朱塞佩

半分钟结束了怎么办?(如果不明显,则无需更改任何内容)
Jonathan Allan

Answers:


6

JavaScript(ES6),171字节

以currying语法接受输入(a)(n)通过修改输入矩阵进行输出

a=>g=(n,y=a[F='findIndex'](r=>~(i=r[F](v=>v>'?'))),x=i,R=a[y])=>!n--|[-1,0,1,2].every(d=>(R[x]='<^>v'[d+1],(c=(a[Y=y+~-d%2]||0)[X=x+d%2])<1?g(n,Y,X):n|c!='$')&&(R[x]=' '))

在线尝试!

已评论

a =>                           // a[] = input matrix
g = (                          // g = recursive function taking:
  n,                           //   n = number of remaining moves
                               //   (x, y) = current coordinates, initialized as follows:
  y = a[F = 'findIndex'](r =>  //     y = index of the row containing the starting point,
    ~(i = r[F](v => v > '?'))  //         found by iterating over all rows r until we
  ),                           //         find some i such that r[i] > '?'
  x = i,                       //     x = index of the column of the starting point
  R = a[y]                     //   R[] = current row
) =>                           //
  !n-- |                       // decrement n; force failure if we're out of moves
  [-1, 0, 1, 2].every(d =>     // for each direction d, where -1 = left, 0 = up,
    (                          // 1 = right and 2 = down:
      R[x] = '<^>v'[d + 1], (  //   update the current cell with the direction symbol
        c = (                  //   c = content of the new cell at (X, Y) with:
          a[Y = y + ~-d % 2]   //     Y = y + dy
          || 0                 //     (use a dummy value if this row does not exist)
        )[X = x + d % 2]       //     X = x + dx
      ) < 1 ?                  //   if c is a space:
        g(n, Y, X)             //     we can go on with a recursive call
      :                        //   else:
        n | c != '$'           //     return false if n = 0 and we've reached the target
    ) &&                       //   unless the above result is falsy,
    (R[x] = ' ')               //   restore the current cell to a space
  )                            // end of every()

5

Python 2中310个 256字节

感谢@cairdcoinheringaahing except:0-3字节
感谢@Mnemonic -8字节
感谢@JonathanAllan -3字节
感谢@ovs -5字节

G,L=input()
R=[]
def S(x,y,G,c,R):
 try:
	if x>-1<y<G[y][x]in' @':i=0;exec"T=[r[:]for r in G];T[y][x]='<v^>'[i];S(x+~-i/2,y+~-(i^2)/2,T,c-1,R);i+=1;"*4
	R+=[G]*(0==c<'$'==G[y][x])
 except:0
for i,y in enumerate(G):"@"in y>S(y.index("@"),i,G,L,R)
print R[0]

在线尝试!

一些解释:

try-except用于确保xy坐标都在边界内。访问时将引发异常G[y][x]。Python太好了,可以接受负索引,因此x>-1<y添加了check 。

T=[r[:]for r in G]用于创建G按值的副本

~-i/2~-(i^2)/2用于生成pair (-1, 0), (0, 1), (0, -1), (1, 0),该pairs 用于在网格中移动(还有更短的方法!)

R+=[G]*(0==c<'$'==G[y][x])检查,'$'达到要求的步骤数。R用于从递归函数调用中获得此结果。

for i,y in enumerate(G):"@"in y>S(y.index("@"),i,G,L,R)在输入和调用函数中找到xy的。'@'S

print R[0] R 可能包含一个以上的解决方案,因此仅输出第一



1
您可以将替换if G[y][x]=='$':为来保存一个字节if'$'==G[y][x]:

1
实际上,整个条件可以替换R+=(G[y][x]=='$')*(c==0)*[G]为另一个字节。

1
not,不确定我在看什么。您可以在第一种情况下使用if(x>-1<y)*(G[y][x]in' @'):

1
一种更快捷的方式y+cmp(i%2,i/2)y+~-(i^2)/2; 可能还有更短的时间。
乔纳森·艾伦

2

Python 2中264个 261 251 249字节

def f(a,n,r=-1,s=0):
 j=len(a[0]);x=1;z=y=0
 if r<0:s,r=divmod(sum(a,[]).index('@'),j)
 for c in'>v<^':
	u=r+x;v=s+y;x,y=-y,x
	if j>u>-1<v<len(a):b=[e[:]for e in a];b[s][r]=c;w=a[v][u];z=n*(w<'!')and f(b,n-1,u,v)or n==1and w=='$'and b
	if z:return z

在线尝试!


1
@ Arnauld:糟糕!有点太花哨了。固定。
Chas Brown
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.