C#
编辑:修复了除法和格式。并添加了助手类。
这是高尔夫球代码,807个字符
class M{public int c,x,y,u;}
void G(string[] z){
M K;int[]x={0,0,-1,1},y={-1,1,0,0},I={0,0,0,0};
string[]T={"Up","Down","Left","Right"};
int X,Y,R,n=0,H=z.Length,W=z[0].Length;W-=W/2;var D= string.Join(" ", z).Where(c=>c!=' ').Select(c=>new M(){c=c,x=n%W,y=n++/W}).ToList();n=0;var S=D.GroupBy(k=>k.c).ToDictionary(k=>k.Key,k =>n++);
for(;I[0]<4;I[0]++)for(I[1]=0;I[1]<4;I[1]++)for(I[2]=0;I[2]<4;I[2]++)for(I[3]=0;I[3]<4;I[3]++){
if ((1<<I[0]|1<<I[1]|1<<I[2]|1<<I[3])!=15)continue;
foreach (var Q in D){D.ForEach(p=>p.u=-1);R=1;K=Q;j:if((X=K.x+x[n=I[S[K.c]]])>=0&&X<W&&(Y=K.y+y[n])>=0&&Y<H&&(K=D[X+Y*W]).u<0){
K.u=1;if(++R==D.Count){Console.WriteLine("{4} Start({0}:{1}) End({2}:{3})",Q.x,Q.y,K.x,K.y,string.Join(", ",S.Select(k=>string.Format("{1}: '{0}'",(char)k.Key,T[I[k.Value]])).ToArray()));return;}goto j;}}}
}
三个测试用例的结果:
下:'1',右:'2',上:'3',左:'4'开始(0:0)结束(5:2)
上:'@',左:'。',下:' e',右:'H'开始(1:1)结束(0:0)
右:'X',下:'V',上:'I',左:'C'开始(0:2)结束(2:4)
这是不含“ golf”的原始代码,将近4,000个字符:
class Program
{
static string[] input1 = { "1 2 2 2 2 1",
"1 3 4 4 1 4",
"2 3 1 3 4 2",
"1 4 4 2 1 3",
"2 2 2 3 2 3"};
static string[] input2 = { "@ . .",
"e . @",
"H H @",
};
static string[] input3 = { "0 0 1",
"0 0 1",
"3 2 2",
};
static void Main(string[] args)
{
Resolve(input1);
Resolve(input2);
Resolve(input3);
Console.ReadLine();
}
class N { public int c; public int x, y, i, u; }
static void Resolve(string[] input)
{
int[] ox = { -1, 1, 0, 0 }, oy = { 0, 0, -1, 1 }, I = { 0, 0, 0, 0 };
string[] TXT = { "Left", "Right", "Up", "Down" };
int X, Y, R, n = 0, H = input.Length, W = input[0].Length;
W -= W / 2;
N K = null;
var data = string.Join(" ", input).Where(c => c != ' ').Select(c => new N() { c = c, x = (n % W), y = (n / W), i = n++, u = -1 }).ToList();
n = 0;
var S = data.GroupBy(k => k.c).ToDictionary(k => k.Key, k => n++);
for (; I[0] < 4; I[0]++)
for (I[1] = 0; I[1] < 4; I[1]++)
for (I[2] = 0; I[2] < 4; I[2]++)
for (I[3] = 0; I[3] < 4; I[3]++)
{
if (((1 << I[0]) | (1 << I[1]) | (1 << I[2]) | (1 << I[3])) != 15) continue;
foreach(var Q in data)
{
data.ForEach(p => p.u = -1);
R = 0;
K = Q;
while (K != null)
{
n = I[S[K.c]];
X = K.x + ox[n];
Y = K.y + oy[n];
if (X >= 0 && X < W && Y >= 0 && Y < H)
{
n = X + Y * W;
if (data[n].u < 0)
{
data[n].u = K.i;
K = data[n];
R++;
if (R == data.Count - 1)
{
Console.WriteLine();
Console.WriteLine("Start({0}:{1}) End({2}:{3})", Q.x, Q.y, K.x, K.y);
Console.WriteLine(string.Join(", ", S.Select(k => string.Format("'{0}': {1}", (char)k.Key, TXT[I[k.Value]])).ToArray()));
Action<N> Write = null;
Write = (k) =>
{
if (k.u != -1)
{
Write(data[k.u]);
}
Console.Write(string.Format("({0}:{1}){2}", k.x, k.y, k == K ? "\n" : " => "));
};
Write(K);
return;
}
continue;
}
}
K = null;
}
}
}
Console.WriteLine("Solution not found");
}
}
}
这些是三个示例的结果:
找不到解决方案
开始(1:1)结束(0:0)'@':上,'。':左,'e':下,'H':右
(1:1)=>(0:1)=>(0:2)=>(1:2)=>(2:2)=>(2:1)=>(2:0)=>( 1:0)=>(0:0)
开始(0:0)结束(1:1)'0':右,'1':向下,'3':上,'2':左
(0:0] =>(1:0)=>(2:0)=>(2:1)=>(2:2)=>(1:2)=>(0:2)=>( 0:1)=>(1:1)