VB.Net:290 个字符(320 字节)
需要 Option Strict Off,Option Explicit Off
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
通过删除字符方面昂贵的 Dictionary 以及不必要|
的 's 并经过更多的“缩小”,我们最终得到:
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 字节)相比,收益并不大,但也许这种方法对使用不同编程语言的其他人有用。