씨#
편집 : 나누기와 서식을 수정했습니다. 그리고 도우미 클래스를 추가했습니다.
골프 코드 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)