使用Ghost Leg方法,在给定数字和有效字符串模式的情况下,将按字符数排序的最短代码。
Input: 3, "| | | | | | | | |-| |=| | | | | |-| | |-| |=| | | |-| |-| | |-|" Output: 2 Input: 2, "| | |=| | |-| |-| | | |-| | |" Output: 1
|
,-
,=
(和[空格],[LF] )。此外,两个相邻的列不能同时包含短划线(在同一行中)。|-|-|
和|=|=|
会产生歧义。给定的输入字符串永远不会包含那些。Given pattern: "|-| |=|-|=|LF| |-| | |-|LF|=| |-| | |LF| | |-|=|-|" |-| |=|-|=| | |-| | |-| |=| |-| | | | | |-|=|-| Given value : Expected result 1 : 6 2 : 1 3 : 3 4 : 5 5 : 4 6 : 2
修改:更正预期结果
答案 0 :(得分:13)
缩小和高尔夫球:
function g(n,s){for(l=s.split('\n'),n*=2;k=l.shift();)for(j=3;j;)n+=k[n-3]==(c=--j-1?'=':'-')?-2:k[n-1]==c?2:0;return n/2}
可读版本:
function g(n, str) {
var c, i, j;
var lines = str.split('\n');
n = (n * 2) - 2;
for (i = 0; i < lines.length; i++) {
for (j = 0; j < 3; j++) {
c = (j == 1) ? '-' : '=';
if (lines[i].charAt(n-1) == c) n-=2; // Move left
else if (lines[i].charAt(n+1) == c) n+=2; // Move right
}
}
return 1+n/2;
}
的说明:强>
str
分为一系列行。n
以覆盖每行中的字符数,从0开始。=
标志的腿附着的地方。中间层用于-
标志的腿部。n
。每行的每一层只有一个可能的移动。因此,n
最多可以连续移动3次。n
,标准化为从1开始到垂直线的数量。
测试用例:
var ghostLegs = [];
ghostLegs[0] = "|-| |=|-|=|\n" +
"| |-| | |-|\n" +
"|=| |-| | |\n" +
"| | |-|=|-|";
ghostLegs[1] = "| | | | | | | |\n" +
"|-| |=| | | | |\n" +
"|-| | |-| |=| |\n" +
"| |-| |-| | |-|";
ghostLegs[2] = "| | |=| |\n" +
"|-| |-| |\n" +
"| |-| | |";
ghostLegs[3] = "|=|-|";
for (var m = 0; m < ghostLegs.length; m++) {
console.log('\nTest: ' + (m + 1) + '\n');
for (var n = 1; n <= (ghostLegs[m].split('\n')[0].length / 2) + 1; n++) {
console.log(n + ':' + g(n, ghostLegs[m]));
}
}
的结果:强>
Test: 1
1:6
2:1
3:3
4:5
5:4
6:2
Test: 2
1:1
2:3
3:2
4:4
5:5
6:6
7:8
8:7
Test: 3
1:3
2:1
3:4
4:2
5:5
Test: 4
1:3
2:2
3:1
答案 1 :(得分:9)
几乎是丹尼尔解决方案的翻译(我们爱你的人;)
{for(i=0;i<3;){s=++i-2?"=":"-";if(s==$x)x--;else if(s!=$++x)x--}}END{print x}
但我们可以取消if/else
并将其替换为?:
{for(i=0;i<3;){s=++i-2?"=":"-";s==$x?x--:s!=$++x?x--:x}}END{print x}
使用定义为x变量的起始位置运行它:
$ for x in `seq 6`; do echo $x\ ;awk -F\| -vx=$x -f ghost.awk<<__EOF__
|-| |=|-|=|
| |-| | |-|
|=| |-| | |
| | |-|=|-|
__EOF__
done
1 6
2 1
3 3
4 5
5 4
6 2
答案 2 :(得分:6)
def f s,m
m.each_line{|r|%w{= - =}.map{|i|s+=i==r[2*s-3]?-1:i==r[2*s-1]?1:0}}
s
end
92个字符
def f s,m
s=s*2-2
m.each_line{|r|%w{= - =}.each{|i|s+=i==r[s-1]?-2:i==r[s+1]?2:0}}
1+s/2
end
用法
map="|-| |=|-|=|
| |-| | |-|
|=| |-| | |
| | |-|=|-|"
1.upto(6) do |i|
p f(i, map)
end
输出
6
1
3
5
4
2
答案 3 :(得分:4)
sub g{for$s(pop=~/.+/g){map$_[0]-=1-abs(index substr(" $s",$_[0]*2-2,3),$_),qw[= - =]}pop}
sub g{map{for$s(qw[= - =]){pos=$_[0]*2-2;$_[0]+=/\G((?<=$s)|.$s)/&&$&cmp$"}}pop=~/.+/g;pop}
$s=<<'__PATTERN__';
|-| |=|-|=|
| |-| | |-|
|=| |-| | |
| | |-|=|-|
__PATTERN__
for $n (1..6) {
print g($n,$s);
}
答案 4 :(得分:3)
VB.Net:290个字符(320字节)
需要选项严格关闭,选项显式关闭
Function G(i,P)
i=i*2-1
F=0
M="-"
Q="="
Z=P.Split(Chr(10))
While E<Z.Length
L=(" "& Z(E))(i-1)
R=(Z(E)&" ")(i)
J=L & R=" "&" "
E-=(F=2Or J)
i+=If(F=1,2*((L=M)-(R=M)),If(F=2,2*((L=Q)-(R=Q)),If(J,0,2+4*(L=Q Or(L=M And R<>Q)))))
F=If(F=1,2,If(F=2,0,If(J,F,2+(L=Q Or R=Q))))
End While
G=(i-1)\2+1
End Function
可读形式:
Function G(ByVal i As Integer, ByVal P As String) As Integer
i = i * 2 - 1
Dim F As Integer = 0
Const M As String = "-"
Const Q As String = "="
Dim Z As String() = P.Split(Chr(10))
Dim E As Integer = 0
While E < Z.Length
Dim L As Char = (" " & Z(E))(i - 1)
Dim R As Char = (Z(E) & " ")(i)
Dim J As Boolean = L & R = " " & " "
E -= (F = 2 Or J)
i += If(F = 1, 2 * ((L = M) - (R = M)), _
If(F = 2, 2 * ((L = Q) - (R = Q)), _
If(J, 0, 2 + 4 * (L = Q Or (L = M And R <> Q)))))
F = If(F = 1, 2, If(F = 2, 0, If(J, F, 2 + (L = Q Or R = Q))))
End While
G = (i - 1) \ 2 + 1
End Function
测试用例
Sub Main()
Dim sb As New StringBuilder
Dim LF As Char = ControlChars.Lf
sb.Append("|-| |=|-|=|")
sb.Append(LF)
sb.Append("| |-| | |-|")
sb.Append(LF)
sb.Append("|=| |-| | |")
sb.Append(LF)
sb.Append("| | |-|=|-|")
Dim pattern As String = sb.ToString
For w As Integer = 1 To pattern.Split(LF)(0).Length \ 2 + 1
Console.WriteLine(w.ToString & " : " & G(w, pattern).ToString)
Next
Console.ReadKey()
End Sub
修改强>
(对于那些还在读这篇文章的人)
我尝试了不同的方法。我的想法是绘制预期的不同模式并相应地采取行动。我们首先要确定我们是向左还是向右转,然后确定我们的小Amidar猴子将移动的列数(如果需要,可以反转字符串)。
首先提出完整的解决方案:
Function GhostLeg(ByVal i As Integer, ByVal p As String) As Integer
i = i * 2 - 2
Dim LeftOrRight As New Dictionary(Of String, Integer)
LeftOrRight(" | ") = 0
LeftOrRight("-| ") = -1
LeftOrRight("=| ") = -1
LeftOrRight("=|-") = -1
LeftOrRight(" |-") = 1
LeftOrRight(" |=") = 1
LeftOrRight("-|=") = 1
Dim ColumnAdd As New Dictionary(Of String, Integer)
ColumnAdd("| | | ") = 0
ColumnAdd("| | |-") = 0
ColumnAdd("| |-| ") = 0
ColumnAdd("| | |=") = 0
ColumnAdd("| |=| ") = 0
ColumnAdd("| |-|=") = 0
ColumnAdd("| |=|-") = 0
ColumnAdd("|=| | ") = 0
ColumnAdd("|=| |-") = 0
ColumnAdd("|=| |=") = 0
ColumnAdd("|-| |-") = 1
ColumnAdd("|-| | ") = 1
ColumnAdd("|-| |=") = 1
ColumnAdd("|-|=|-") = 2
ColumnAdd("|-|=| ") = 2
ColumnAdd("|=|-| ") = 2
ColumnAdd("|=|-|=") = 3
Const TRIPLESPACE As String = " | | "
Dim direction As Integer
For Each line As String In p.Split(Chr(10))
line = TRIPLESPACE & line & TRIPLESPACE
direction = LeftOrRight(line.Substring(i + 4, 3))
If direction = 1 Then
line = line.Substring(i + 5, 6)
i += 2 * direction * ColumnAdd(line)
ElseIf direction = -1 Then
line = StrReverse(line.Substring(i, 6))
i += 2 * direction * ColumnAdd(line)
End If
Next
Return 1 + i \ 2
End Function
通过删除性格昂贵的字典,以及不必要的|
,并在经过一些更“缩小”后,我们最终得到:
Function G(i,p)
D="- 0= 0=-0 -2 =2-=2"
A="- -1- 1- =1-=-2-= 2=- 2=-=3"
For Each l In p.Replace("|","").Split(Chr(10))
l=" "& l &" "
w=InStr(D,Mid(l,i+2,2))
If w Then
w=Val(D(w+1))-1
s=InStr(A,If(w=1,Mid(l,i+3,3),StrReverse(Mid(l,i,3))))
i+=If(s,w*Val(A(s+2)),0)
End If
Next
G=i
End Function
与我之前的努力(282个字符,308个字节)相比,没有太大的收获,但是这种方法对于使用不同编程语言的其他人来说可能会有用。
答案 5 :(得分:3)
在看到Daniel Vassallo的解决方案之后,我太羞于发布了它。但是这里的丹尼尔的回答是移植到C#的。 C#的一个主要缺点是必须进行边界检查,这需要花费20个字符。
int G(string s,int n){var l=s.Split('\n');n*=2;for(int i=0,j,c;i<l.Length;i++)
for(j=0;j<3;n+=n>2&&l[i][n-3]==c?-2:n<l[i].Length&&l[i][n-1]==c?2:0)c=j++==1?45
:61;return n/2;}
格式化:
int G(string s, int n)
{
var l = s.Split('\n');
n *= 2;
for (int i = 0, j, c; i < l.Length; i++)
for (j = 0; j < 3; n += n > 2 && l[i][n - 3] == c ? -2 : n < l[i].Length && l[i][n - 1] == c ? 2 : 0)
c = j++ == 1 ? 45 : 61;
return n / 2;
}