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");
};