- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个程序可以扫描图像并将 gem 转换为特定数字。看下图:
所以我有一张带有数字的表格。数字 1 代表黄色 gem ,2 代表蓝色,等等......
喜欢:
A B C D E
1 | 1 2 3 4 5
2 | 3 5 2 4 1
3 | 1 4 4 1 2
4 | 3 3 2 1 5
5 | 5 1 5 2 5
我想计算一个步骤,在水平或垂直方向上获得 3 个相等的数字( gem )。例如,在此表中,如果我将 E2 与 D2 交换,则 D2 将为 1,并且 D2、D3、D4 将创建一个有效步骤,因为它们都是 1。我使用的是 VB.NET 2010。
我的表格是 8x8 大小,上面只是一个例子。
现在我只有多个变量的值(A1=1,A2=3,等等......)
希望您能理解我的问题,我们将不胜感激。
最佳答案
给你:
Module Module1
Sub Main()
Dim input = {{1, 3, 2, 4, 1, 1, 2, 1},
{1, 2, 5, 3, 2, 1, 3, 4},
{2, 1, 5, 4, 3, 2, 5, 4},
{3, 5, 1, 5, 2, 4, 1, 2},
{4, 2, 5, 1, 5, 2, 4, 2},
{2, 3, 2, 2, 5, 1, 3, 1},
{2, 1, 5, 4, 3, 2, 5, 4},
{3, 5, 1, 3, 2, 4, 1, 2}}
Console.WriteLine("INPUT:")
Console.Write(" |")
For i = 1 To input.GetLength(1)
Console.Write("{0,3}", GetColumnName(i))
Next
Console.Write(vbCrLf)
Console.Write("---+")
For i = 1 To input.GetLength(1)
Console.Write("---")
Next
Console.Write(vbCrLf)
For y = 0 To input.GetUpperBound(0)
Console.Write("{0,3}|", y + 1)
For x = 0 To input.GetUpperBound(1)
Console.Write("{0,3}", input(y, x))
Next
Console.Write(vbCrLf)
Next
Console.WriteLine("{0}{0}SOLUTION:", vbCrLf)
For Each match In Solve(input)
Console.WriteLine("Move {0} {1} for a match of {2}", match.Item1, match.Item2, match.Item3)
Next
Console.ReadLine()
End Sub
Function Solve(ByVal input As Integer(,)) As IEnumerable(Of Tuple(Of String, Char, Integer))
Dim matches As New List(Of Tuple(Of String, Char, Integer))
Dim result As Tuple(Of Boolean, Tuple(Of String, Char, Integer))
Dim test As Integer(,)
Dim maxX = input.GetUpperBound(0) - 1
Dim maxY = input.GetUpperBound(1) - 1
For x = 0 To maxX
For y = 0 To maxY
ReDim test(If(maxX - x > 4, 3, maxX - x), If(maxY - y > 4, 3, maxY - y))
For x1 = x To x + test.GetLength(0) - 1
For y1 = y To y + test.GetLength(1) - 1
test(x1 - x, y1 - y) = input(y1, x1)
Next
Next
'check if the result is a match
For Each result In {IsMatchOnThird(test), IsMatchOnSecond(test), IsMatchOnFirst(test)} '<-- Updated Line
If result.Item1 = True Then
Dim matchPoint = Tuple.Create(CInt(result.Item2.Item1.Split(","c)(0)),
CInt(result.Item2.Item1.Split(","c)(1)))
matches.Add(Tuple.Create(GetColumnName(matchPoint.Item1 + x + 1) & CStr(matchPoint.Item2 + y + 1),
result.Item2.Item2, result.Item2.Item3))
End If
Next
Next
Next
Return RemoveDuplicates(matches)
End Function
Public Function GetColumnName(ByVal colIndex As Integer) As String
Dim result As New List(Of String)
Do While colIndex > 0
result.Insert(0, Chr(65 + CInt((colIndex - 1) Mod 26)))
colIndex = (colIndex - 1) \ 26
Loop
Return String.Join("", result.ToArray)
End Function
Function RemoveDuplicates(ByVal list As IEnumerable(Of Tuple(Of String, Char, Integer))) As IEnumerable(Of Tuple(Of String, Char, Integer))
'remove those where gems and swap direction are the same
Dim l = (From i In list Order By i.Item3 Descending, i.Item1, i.Item2).ToList
For i = l.Count - 1 To 1 Step -1
If (l(i).Item1 = l(i - 1).Item1) AndAlso (l(i).Item2 = l(i - 1).Item2) Then
l.RemoveAt(i)
End If
Next
l = (From i In list Order By i.Item1, i.Item3 Descending).ToList
For i = l.Count - 1 To 1 Step -1
If (l(i).Item1 = l(i - 1).Item1) AndAlso (l(i).Item2 = l(i - 1).Item2) Then
l.RemoveAt(i)
End If
Next
Return From i In l Order By i.Item3 Descending, i.Item1
End Function
Function IsMatchOnThird(ByVal input As Integer(,)) As Tuple(Of Boolean, Tuple(Of String, Char, Integer))
Dim size = Math.Min(input.GetLength(0), 4).ToString & "C," & Math.Min(input.GetLength(1), 4).ToString & "R"
Dim i = input
Dim isValid = Function(test As Integer()) test.All(Function(v) v = test(0))
Select Case size
Case "4C,4R"
If isValid({i(0, 0), i(0, 1), i(1, 2)}) Then
Return Tuple.Create(True, Tuple.Create("1,2", "L"c, If(isValid({i(0, 0), i(0, 3)}), 4, 3)))
ElseIf isValid({i(1, 0), i(1, 1), i(0, 2)}) Then
Return Tuple.Create(True, Tuple.Create("0,2", "R"c, If(isValid({i(1, 0), i(1, 3)}), 4, 3)))
ElseIf isValid({i(0, 0), i(1, 0), i(2, 1)}) Then
Return Tuple.Create(True, Tuple.Create("2,1", "U"c, If(isValid({i(0, 1), i(3, 0)}), 4, 3)))
ElseIf isValid({i(0, 1), i(1, 1), i(2, 0)}) Then
Return Tuple.Create(True, Tuple.Create("2,0", "D"c, If(isValid({i(0, 1), i(3, 1)}), 4, 3)))
End If
Case "4C,3R"
If isValid({i(0, 0), i(0, 1), i(1, 2)}) Then
Return Tuple.Create(True, Tuple.Create("1,2", "L"c, 3))
ElseIf isValid({i(1, 0), i(1, 1), i(0, 2)}) Then
Return Tuple.Create(True, Tuple.Create("0,2", "R"c, 3))
ElseIf isValid({i(0, 0), i(1, 0), i(2, 1)}) Then
Return Tuple.Create(True, Tuple.Create("2,1", "U"c, If(isValid({i(0, 1), i(3, 0)}), 4, 3)))
ElseIf isValid({i(0, 1), i(1, 1), i(2, 0)}) Then
Return Tuple.Create(True, Tuple.Create("2,0", "D"c, If(isValid({i(0, 1), i(3, 1)}), 4, 3)))
End If
Case "4C,2R"
If isValid({i(0, 0), i(1, 0), i(2, 1)}) Then
Return Tuple.Create(True, Tuple.Create("2,1", "U"c, If(isValid({i(0, 1), i(3, 0)}), 4, 3)))
ElseIf isValid({i(0, 1), i(1, 1), i(2, 0)}) Then
Return Tuple.Create(True, Tuple.Create("2,0", "D"c, If(isValid({i(0, 1), i(3, 1)}), 4, 3)))
End If
Case "3C,4R"
If isValid({i(0, 0), i(0, 1), i(1, 2)}) Then
Return Tuple.Create(True, Tuple.Create("1,2", "L"c, If(isValid({i(0, 0), i(0, 3)}), 4, 3)))
ElseIf isValid({i(1, 0), i(1, 1), i(0, 2)}) Then
Return Tuple.Create(True, Tuple.Create("0,2", "R"c, If(isValid({i(1, 0), i(1, 3)}), 4, 3)))
ElseIf isValid({i(0, 0), i(1, 0), i(2, 1)}) Then
Return Tuple.Create(True, Tuple.Create("2,1", "U"c, 3))
ElseIf isValid({i(0, 1), i(1, 1), i(2, 0)}) Then
Return Tuple.Create(True, Tuple.Create("2,0", "D"c, 3))
End If
Case "3C,3R"
If isValid({i(0, 0), i(0, 1), i(1, 2)}) Then
Return Tuple.Create(True, Tuple.Create("1,2", "L"c, 3))
ElseIf isValid({i(1, 0), i(1, 1), i(0, 2)}) Then
Return Tuple.Create(True, Tuple.Create("0,2", "R"c, 3))
ElseIf isValid({i(0, 0), i(1, 0), i(2, 1)}) Then
Return Tuple.Create(True, Tuple.Create("2,1", "U"c, 3))
ElseIf isValid({i(0, 1), i(1, 1), i(2, 0)}) Then
Return Tuple.Create(True, Tuple.Create("2,0", "D"c, 3))
End If
Case "3C,2R"
If isValid({i(0, 0), i(1, 0), i(2, 1)}) Then
Return Tuple.Create(True, Tuple.Create("2,1", "U"c, 3))
ElseIf isValid({i(0, 1), i(1, 1), i(2, 0)}) Then
Return Tuple.Create(True, Tuple.Create("2,0", "D"c, 3))
End If
Case "2C,4R"
If isValid({i(0, 0), i(0, 1), i(1, 1)}) Then
Return Tuple.Create(True, Tuple.Create("1,1", "L"c, If(isValid({i(0, 1), i(0, 3)}), 4, 3)))
ElseIf isValid({i(1, 0), i(1, 1), i(0, 2)}) Then
Return Tuple.Create(True, Tuple.Create("0,2", "R"c, If(isValid({i(1, 0), i(1, 3)}), 4, 3)))
End If
Case "2C,3R"
If isValid({i(0, 0), i(0, 1), i(1, 1)}) Then
Return Tuple.Create(True, Tuple.Create("1,1", "L"c, 3))
ElseIf isValid({i(1, 0), i(1, 1), i(0, 2)}) Then
Return Tuple.Create(True, Tuple.Create("0,2", "R"c, 3))
End If
End Select
Return Tuple.Create(False, Tuple.Create("None", "."c, 0))
End Function
Function IsMatchOnSecond(ByVal input As Integer(,)) As Tuple(Of Boolean, Tuple(Of String, Char, Integer))
Dim i = input
Dim isValid = Function(test As Integer()) test.All(Function(v) v = test(0))
Dim xLength = input.GetLength(0)
Dim yLength = input.GetLength(1)
If xLength >= 3 AndAlso yLength >= 3 Then
If isValid({i(0, 0), i(1, 1), i(0, 2)}) Then
Return Tuple.Create(True, Tuple.Create("1,1", "L"c, 3))
ElseIf isValid({i(1, 0), i(2, 1), i(1, 2)}) Then
Return Tuple.Create(True, Tuple.Create("2,1", "L"c, 3))
ElseIf isValid({i(1, 0), i(0, 1), i(1, 2)}) Then
Return Tuple.Create(True, Tuple.Create("0,1", "R"c, 3))
ElseIf isValid({i(2, 0), i(1, 1), i(2, 2)}) Then
Return Tuple.Create(True, Tuple.Create("1,1", "R"c, 3))
ElseIf isValid({i(0, 0), i(1, 1), i(2, 0)}) Then
Return Tuple.Create(True, Tuple.Create("1,1", "U"c, 3))
ElseIf isValid({i(0, 1), i(1, 2), i(2, 1)}) Then
Return Tuple.Create(True, Tuple.Create("1,2", "U"c, 3))
ElseIf isValid({i(0, 2), i(1, 1), i(2, 2)}) Then
Return Tuple.Create(True, Tuple.Create("1,1", "D"c, 3))
ElseIf isValid({i(0, 1), i(1, 0), i(2, 1)}) Then
Return Tuple.Create(True, Tuple.Create("1,0", "D"c, 3))
End If
End If
Return Tuple.Create(False, Tuple.Create("None", "."c, 0))
End Function
Private Function IsMatchOnFirst(ByVal input As Integer(,)) As Tuple(Of Boolean, Tuple(Of String, Char, Integer)) '<-- New method
Dim i = input
Dim isValid = Function(test As Integer()) test.All(Function(v) v = test(0))
Dim xLength = input.GetLength(0)
Dim yLength = input.GetLength(1)
If xLength >= 3 AndAlso yLength >= 3 Then
If isValid({i(0, 0), i(1, 1), i(1, 2)}) Then
Return Tuple.Create(True, Tuple.Create("0,0", "R"c, 3))
ElseIf isValid({i(1, 0), i(2, 1), i(2, 2)}) Then
Return Tuple.Create(True, Tuple.Create("1,0", "R"c, 3))
ElseIf isValid({i(2, 0), i(1, 1), i(1, 2)}) Then
Return Tuple.Create(True, Tuple.Create("2,0", "L"c, 3))
ElseIf isValid({i(1, 0), i(0, 1), i(0, 2)}) Then
Return Tuple.Create(True, Tuple.Create("1,0", "L"c, 3))
ElseIf isValid({i(0, 0), i(1, 1), i(2, 1)}) Then
Return Tuple.Create(True, Tuple.Create("0,0", "D"c, 3))
ElseIf isValid({i(0, 1), i(1, 2), i(2, 2)}) Then
Return Tuple.Create(True, Tuple.Create("0,1", "D"c, 3))
ElseIf isValid({i(0, 1), i(1, 0), i(2, 0)}) Then
Return Tuple.Create(True, Tuple.Create("0,1", "U"c, 3))
ElseIf isValid({i(0, 2), i(1, 1), i(2, 1)}) Then
Return Tuple.Create(True, Tuple.Create("0,2", "U"c, 3))
End If
End If
Return Tuple.Create(False, Tuple.Create("None", "."c, 0))
End Function
End Module
这是我使用的元组类。
顺便说一句: 我从 StackOverflow 上的一个问题将其从 C# 转换为 VB.NET。
Imports System.Collections.Generic
Public Class Tuple(Of T1, T2, T3)
Inherits Tuple(Of T1, T2)
Implements IEqualityComparer(Of Tuple(Of T1, T2, T3))
Private _third As T3
Public Sub New(ByVal item1 As T1, ByVal item2 As T2, ByVal item3 As T3)
MyBase.New(item1, item2)
_third = item3
End Sub
Public Property Item3() As T3
Get
Return _third
End Get
Private Set(ByVal value As T3)
_third = value
End Set
End Property
Public Overloads Function Equals(ByVal x As Tuple(Of T1, T2, T3), ByVal y As Tuple(Of T1, T2, T3)) As Boolean Implements IEqualityComparer(Of Tuple(Of T1, T2, T3)).Equals
Return EqualityComparer(Of T1).[Default].Equals(x.Item1, y.Item1) AndAlso EqualityComparer(Of T2).[Default].Equals(x.Item2, y.Item2) AndAlso EqualityComparer(Of T3).[Default].Equals(x.Item3, y.Item3)
End Function
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Return TypeOf obj Is Tuple(Of T1, T2, T3) AndAlso Equals(Me, DirectCast(obj, Tuple(Of T1, T2, T3)))
End Function
Public Overloads Function GetHashCode(ByVal obj As Tuple(Of T1, T2, T3)) As Integer Implements IEqualityComparer(Of Tuple(Of T1, T2, T3)).GetHashCode
Return EqualityComparer(Of T1).[Default].GetHashCode(Item1) Xor EqualityComparer(Of T2).[Default].GetHashCode(Item2) Xor EqualityComparer(Of T3).[Default].GetHashCode(Item3)
End Function
Public Shared Shadows Operator =(ByVal left As Tuple(Of T1, T2, T3), ByVal right As Tuple(Of T1, T2, T3)) As Boolean
If DirectCast(left, Object) Is Nothing AndAlso DirectCast(right, Object) Is Nothing Then
Return True
End If
Return left.Equals(right)
End Operator
Public Shared Shadows Operator <>(ByVal left As Tuple(Of T1, T2, T3), ByVal right As Tuple(Of T1, T2, T3)) As Boolean
If DirectCast(left, Object) Is Nothing AndAlso DirectCast(right, Object) Is Nothing Then
Return False
End If
Return Not left.Equals(right)
End Operator
End Class
Public Class Tuple(Of T1, T2)
Implements IEqualityComparer(Of Tuple(Of T1, T2))
Public Property Item1() As T1
Get
Return _first
End Get
Private Set(ByVal value As T1)
_first = value
End Set
End Property
Private _first As T1
Public Property Item2() As T2
Get
Return _second
End Get
Private Set(ByVal value As T2)
_second = value
End Set
End Property
Private _second As T2
Public Sub New(ByVal item1 As T1, ByVal item2 As T2)
_first = item1
_second = item2
End Sub
Public Overloads Function Equals(ByVal x As Tuple(Of T1, T2), ByVal y As Tuple(Of T1, T2)) As Boolean Implements IEqualityComparer(Of Tuple(Of T1, T2)).Equals
Return EqualityComparer(Of T1).[Default].Equals(x.Item1, y.Item1) AndAlso EqualityComparer(Of T2).[Default].Equals(x.Item2, y.Item2)
End Function
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Return TypeOf obj Is Tuple(Of T1, T2) AndAlso Equals(Me, DirectCast(obj, Tuple(Of T1, T2)))
End Function
Public Overloads Function GetHashCode(ByVal obj As Tuple(Of T1, T2)) As Integer Implements IEqualityComparer(Of Tuple(Of T1, T2)).GetHashCode
Return EqualityComparer(Of T1).[Default].GetHashCode(Item1) Xor EqualityComparer(Of T2).[Default].GetHashCode(Item2)
End Function
Public Shared Operator =(ByVal left As Tuple(Of T1, T2), ByVal right As Tuple(Of T1, T2)) As Boolean
If DirectCast(left, Object) Is Nothing AndAlso DirectCast(right, Object) Is Nothing Then
Return True
End If
Return left.Equals(right)
End Operator
Public Shared Operator <>(ByVal left As Tuple(Of T1, T2), ByVal right As Tuple(Of T1, T2)) As Boolean
If DirectCast(left, Object) Is Nothing AndAlso DirectCast(right, Object) Is Nothing Then
Return False
End If
Return Not left.Equals(right)
End Operator
End Class
Public MustInherit Class Tuple
<DebuggerStepThrough()> _
Public Shared Function Create(Of T1, T2)(ByVal first As T1, ByVal second As T2) As Tuple(Of T1, T2)
Return New Tuple(Of T1, T2)(first, second)
End Function
<DebuggerStepThrough()> _
Public Shared Function Create(Of T1, T2, T3)(ByVal first As T1, ByVal second As T2, ByVal third As T3) As Tuple(Of T1, T2, T3)
Return New Tuple(Of T1, T2, T3)(first, second, third)
End Function
End Class
运行应用程序给出以下输出:
INPUT: | A B C D E F G H---+------------------------ 1| 1 3 2 4 1 1 2 1 2| 1 2 5 3 2 1 3 4 3| 2 1 5 4 3 2 5 4 4| 3 5 1 5 2 4 1 2 5| 4 2 5 1 5 2 4 2 6| 2 3 2 2 5 1 3 1 7| 2 1 5 4 3 2 5 4 8| 3 5 1 3 2 4 1 2SOLUTION:Move B4 R for a match of 4Move D4 L for a match of 4Move B3 L for a match of 3Move B5 D for a match of 3Move C3 D for a match of 3Move C5 U for a match of 3Move D4 D for a match of 3Move E4 R for a match of 3Move F3 L for a match of 3
此程序适用于任何电路板尺寸。该程序基于我在游戏中搜索匹配项的方式(我认为这是蛮力)。
编辑
求解器似乎无法在第一个 gem 上找到匹配项(请参见下面的示例)。
5, 2, 52, 5, 31, 5, 4
如果将算法应用于上述示例,将找不到匹配项。
我现在已经解决了。现在有一个 IsMatchOnFirst()
方法来处理这些情况。
请参阅更新的代码以了解更改。
关于vb.net - gem 游戏步骤计算,换句话说一些数学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5139358/
我是在项目中使用 keras 的新手。我一直在我的模型中使用generator。 我真的很困惑我应该输入什么值 1) In fit_generator : steps_per_epoch & vali
假设我们有如下情况: A has to give $10 to B. B has to give $20 to C. C has to give $10 to D. 现在这种情况可以简化为: A lo
我正在尝试对特定列(在工作表“OA”中)进行相对引用,我需要在 110 的步骤中检索新工作表中的单元格内容 例如, =OA!$AB217 =OA!$AB327 =OA!$AB437 与其在每个单元格中
我的 PowerShell 控制台启动时间很慢(总是等待超过 5 秒),并且希望获得有关故障排除步骤的建议,以找出瓶颈可能在哪里? 我已经阅读了关于运行脚本的内容,-NoProfile防止模块等加载很
我在 NativeScript 应用程序中使用 slider 小部件,我想知道是否有步骤属性。在我的例子中,小部件代表金钱,我希望以 5 美元的增量滑动。 我查看了文档,但找不到任何对这种情况有帮助的
我在 NativeScript 应用程序中使用 slider 小部件,我想知道是否有步骤属性。在我的例子中,小部件代表金钱,我希望以 5 美元的增量滑动。 我查看了文档,但找不到任何对这种情况有帮助的
这是我的code : &n
为什么 (2) c.ERR(模棱两可)?第一个方法参数 - char ('a') 被扩展为 float => 匹配。 如果找到匹配项,是否无需继续执行第 2 步(装箱/拆箱)或第 3 步(尝试可变参数
我有一个函数,它处理一个包含 6100 个列表项的列表。当列表只有 300 个项目时,该代码可以正常工作。但是立即与 6100 崩溃。有没有一种方法可以遍历这 6100 个项目,一次说 30 个,然后
1.制作PHP安装程序的原理 其实PHP程序的安装原理无非就是将数据库结构和内容导入到相应的数据库中,从这个过程中重新配置连接数据库的参数和文件,为了保证不被别人恶意使用安装文件,当安装
我创建了一个类似于 primeNG page 的步骤组件我想把他放在一个 dynamic dialog 里面但在应用它之后,“第 1 步”和“第 2 步”不会呈现。 查看代码,我发现关键部分是我们打开
我在理解描述的 MixColumns 步骤时遇到问题 here . 我知道扩散,这一切都是有道理的,因为它指出每列都被视为多项式并乘以 GF(2^8) 的模。 但是..乘以GF(2 ^ 8)。尽管域仍
根据我对 TeamCity 工作原理的观察,我注意到在所有步骤执行完毕后评估构建失败条件。这很烦人,因为如果满足任何构建失败条件,我不能有一个不会执行的步骤。 我不是指常见的构建失败条件,例如“至少一
基于这篇试图在我的环境中测试管道代码的帖子。但它给出了以下错误消息。如何修复他的管道代码? ERROR: Unable to find project for artifact copy: test
我参与了一个项目,需要向我的一位同事提供生产数据的子集(日期范围),以进行故障排除。我想将经过清理的生产数据子集插入新的数据库表中我的同事可以访问。请提出实现此目标的最佳方法。 最佳答案 最简单的方法
我有这样的场景: 鉴于我去这个页面 当我输入 cucumber 时 然后我点击 然后我应该看到文字 我不应该看到这条线 如果我运行这个场景,它将执行所有 5 个步骤。但是我想跳过第4步(然后我应该看到
是否有任何功能可以避免 m 文件的绘图输出? 我的意思是我在文件的开头放置了一个函数(如 clc),然后所有绘图函数都被阻止。 最佳答案 您可以使用自己的(嵌套在您的函数内或同一目录中)重载内置绘图函
我是小 cucumber 语言的新手,这在我看来是非常基本的问题,但我找不到答案。 我知道可以在 Gherking 中编写多行步骤参数,如下所示: Given a blog post named "R
即使其中一个步骤失败,有没有办法继续执行 Cucumber Steps。在我当前的设置中,当一个步骤失败时, cucumber 会跳过剩余的步骤......我想知道是否有某种方法可以设置 cucumb
start-step-stop 码是一种数据压缩技术,用于压缩相对较小的数字。 该代码的工作原理如下:它具有三个参数,start、step 和 stop。 Start 确定用于计算前几个数字的位数。
我是一名优秀的程序员,十分优秀!