移动平台!


9

挑战

给定一个字符串(可能有换行符)或一个二维数组,以及一个正整数n,输出平台的位置n在初始位置之后旋转。


U, D, R, L 是平台。

^, v, >, < 是更改平台方向的箭头。

U, D, R, L分别向上,向下,向右和向左移动。当箭头在平台前面时,它会改变方向。

影响平台:

R<

D
^

v
U

>L

>L
 <

(顶部箭头会影响顶部L,但底部箭头不会影响顶部L

不会影响:

 <
R

>
 L

v
 U

D
 ^

<R

R操作正确,因此<不会影响R


例如,如果这是字符串:

>R   <

平台R将向右移动,直到接近箭头为止:

>   R<

之后,它将改变方向并开始向左移动:

>  R <

(即使现在已经走了,字母也不会改变。)

在某些情况下平台无法移动,例如

>R<

要么

v
U
^

最后一个例子:

v   >
D    Rv
   ^U
^    <

一转后

v   >
    U v
D  ^ R
^    <

一转后

v   >
D    Uv
   ^R
^    <

再转一圈:

v   >
    R v
D  ^ U
^    <

您可以假设这些平台在n转弯后不会重叠,这些平台不会超出范围,并且该平台不会碰到指向与该平台相同方向的箭头。


测试用例

Input:
">R   <", 4
Output:
">  R <"

Input:
">R   <", 6
Output:
">R   <"

Input:
">R<", 29
Output:
">R<"

Input:
"v
 U
 ^", 5
Output:
"v
 U
 ^"

Input:
"v

 D
 ^", 1
Output:
"v
 D

 ^"

Input:
"v

 D
 ^", 4
Output:
"v

 D
 ^"

Input:
"v   >
 D    Rv
    ^U
 ^    < ", 2
Output:
"v   >
 D    Uv
    ^R
 ^    <

Input:
">RL<", 3
Output:
">LR<"

Input:
">L  R<", 4
Output:
"> RL <"

Input:
"> RR<
 >L  R <", 6
Ouput:
">RR <
 > RL  <"

Input:
"R   <", 4
Output:
"  R <"

Input:
"R   <", 6
Ouput:
"R   <"

规则

  • 这是 ,因此以字节为单位的最短答案会获胜!
  • 不允许出现标准漏洞。

3
@closevoters:这个挑战尚不清楚吗?
Leaky Nun

是时候基于此制作深奥的编程语言了。
DanTheMan'8

另外,如果平台脱离网格会发生什么?
Quelklef '16

@Quelklef,您可以假设平台在n转弯后不会脱离网格。
acrolith '16

Answers:


2

C#,1245字节

(i,n)=>{string q="RlherLHEfquDFQUd",D="><v^",H="Rlhe",E="LrHE",X="Dufq",Y="UdFQ",S="#v<>";Func<string,char,int>I=(v,L)=>v.IndexOf(L);Func<char,int,char>z=(y,m)=>q[I(q,y)+m*4];Func<string,char,bool>_=(s,F)=>s.Contains(F);var g=((i as char[][])??((string)i).Split('\n').Select(f=>f.ToCharArray())).ToList();int w=g[0].Length,h=g.Count,u;g=g.Select((r,o)=>r.Select((t,p)=>'R'==t&&w>p+1&&0<=(u=I(D,r[p+1]))?z(t,u):'L'==t&&0<=p-1&&0<=(u=I(D,r[p-1]))?z(t,-1+u):'D'==t&&h>o+1&&0<=(u=I(D,g[o+1][p]))?z(t,-2+u):'U'==t&&0<=o-1&&0<=(u=I(S,g[o-1][p]))?z(t,-u):t).ToArray()).ToList();for(var j=0;j<n;j++){bool L,R,T,B;g=g.Select((r,o)=>r.Select((t,p)=>_(D,t)?t:(R=0<=p-1)&&_(H,r[p-1])?w>p+1&&0<=(u=I(D,r[p+1]))?z(r[p-1],u):r[p-1]:(L=w>p+1)&&_(E,r[p+1])?0<=p-1&&0<=(u=I(D,r[p-1]))?z(r[p+1],-1+u):r[p+1]:(B=0<=o-1)&&_(X,g[o-1][p])?h>o+1&&0<=(u=I(D,g[o+1][p]))?z(g[o-1][p],-2+u):g[o-1][p]:(T=h>o+1)&&_(Y,g[o+1][p])?0<=o-1&&0<=(u=I(S,g[o-1][p]))?z(g[o+1][p],-u):g[o+1][p]:(L&&_(H,t)&&!_(D,r[p+1]))||(R&&_(E,t)&&!_(D,r[p-1]))||(B&&_(Y,t)&&!_(D,g[o-1][p]))||(T&&_(X,t)&&!_(D,g[o+1][p]))?' ':t).ToArray()).ToList();}return string.Join("\n",g.Select(s=>new string(s))).ToUpper().Replace("H","U").Replace("E","D").Replace("F","R").Replace("Q","L").Replace("V","v");};

乍一看似乎比较简单,但是后来我继续编写更多代码。:D

LINQ枚举和更新板,更改字符以指示其移动方向。字符返回之前返回。还假设电路板是正方形的(因此必须修改一些多线测试用例以适应此限制)。

展开:

// Casts to Func<object, int, string> so as to accept both string and char[][] input
(i, n) =>{
    // Shorten constants/functions
    string q = "RlherLHEfquDFQUd", D = "><v^", H = "Rlhe", E = "LrHE", X = "Dufq", Y = "UdFQ",S="#v<>";
    Func<string, char, int> I = (v, L) => v.IndexOf(L);
    Func<char, int, char> z = (y, m) => q[I(q,y) + m * 4]; // Updates the direction of the platform
    Func<string, char, bool> _ = (s, F) => s.Contains(F);

    // Convert either string or char[][] input into common format
    var g = ((i as char[][]) ?? ((string)i).Split('\n').Select(f => f.ToCharArray())).ToList();

    // Get board dimensions
    int w = g[0].Length,h = g.Count,u;

    // Update platforms to reflect the direction they're initially moving
    g = g.Select((r, o) => r.Select((t, p) =>
        'R' == t &&w>p+1&&0<=(u=I(D,r[p+1]))?z(t,u):
        'L' == t &&0<=p-1&&0<=(u= I(D, r[p-1]))?z(t,-1+u):
        'D' == t &&h>o+1&&0<=(u= I(D, g[o+1][p]))?z(t,-2+u):
        'U' == t &&0<=o-1&&0<=(u= I(S,g[o-1][p]))?z(t,-u):t
    ).ToArray()).ToList();

    // Go through each timestep
    for (var j=0;j<n;j++)
    {
        bool L,R,T,B;
        g = g.Select((r, o) => r.Select((t, p) => 
            // Don't change <>^v characters
            _(D,t) ? t :

            // Move platforms going right
            (R=0 <= p - 1) && _(H,r[p-1]) ? w > p+1 && 0<=(u= I(D, r[p+1])) ? z(r[p-1],u) : r[p - 1] :

            // Move platforms going left
            (L=w > p + 1) && _(E,r[p+1]) ? 0 <= p-1 && 0<=(u= I(D, r[p-1])) ? z(r[p+1],-1+u) : r[p + 1] :

            // Move platforms going down
            (B=0 <= o - 1) && _(X,g[o-1][p]) ? h > o+1 && 0<=(u= I(D, g[o+1][p])) ? z(g[o - 1][p],-2+u) : g[o-1][p] :

            // Move platforms going up
            (T=h > o + 1) && _(Y,g[o+1][p]) ? 0<=o-1&&0<=(u= I(S, g[o-1][p]))?z(g[o + 1][p],-u) :g[o+1][p]:

            // Erase platforms that moved
            (L&&_(H,t)&&!_(D,r[p+1]))||
            (R&&_(E,t)&&!_(D,r[p-1]))||
            (B&&_(Y,t)&&!_(D,g[o-1][p]))||
            (T&&_(X,t)&&!_(D,g[o+1][p]))
            ? ' ':

            // Maintain whatever character this was
            t
            ).ToArray()).ToList();
    }

    // Replace direction characters with platform label and join into string return value.
    return string.Join("\n",g.Select(s=>new string(s))).ToUpper().Replace("H", "U").Replace("E", "D").Replace("F", "R").Replace("Q", "L").Replace("V", "v");
};
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.