如何通过不同的行和列获得矩阵中最大的值总和。
例如: -
我有这些矩阵值
2 4 5
3 7 9
8 6 1
如果我选择2然后我不能选择在同一行和列中的其他值,我只能选择是否7,9,6,1。那么如果选择7,我只能选择1
这里的计算样本。
2+7+4= 13
2+9+6= 17
3+4+1= 8
3+6+5= 14
8+7+5= 20
8+4+9= 21
所以最大的是21。任何人都可以帮助我
这是我的代码
Function getrandom() As Byte
Dim Buffer(0) As Byte
Dim RNG As New RNGCryptoServiceProvider
RNG.GetBytes(Buffer)
Return Buffer(0) And 7
End Function
Private Sub btnCreateArr_Click(sender As Object, e As EventArgs) Handles btnCreateArr.Click
Dim totalAllNumber As Integer = 0
Dim numbers(3, 3) As Integer
Dim appPath As String = Application.StartupPath()
Using writer As StreamWriter = File.CreateText("C:\Users\pc\Desktop\matrix.txt")
For x As Integer = 0 To 3
For y As Integer = 0 To 3
numbers(x, y) = getrandom()
totalAllNumber = totalAllNumber + numbers(x, y)
'MsgBox(x & ":" & y & " -->" & numbers(x,y) & vbCrLf & totalAllNumber)
writer.Write(numbers(x, y).ToString + " ")
Next
writer.Write(vbCrLf)
Next
Dim totLargest = 0
Dim usedX As ArrayList = New ArrayList()
Dim usedY As ArrayList = New ArrayList()
Dim indexLargestX As Integer
Dim indexLargestY As Integer
For x As Integer = 0 To 3
Dim largest = 0
For y As Integer = 0 To 3
If largest < numbers(x, y) Then
'if index array not empty
If usedX.Count <> 0 Then
'to check whether index of largest is already used or not
If usedX.Contains(x) Then
largest = largest
ElseIf usedY.Contains(y) Then
largest = largest
End If
Else
largest = numbers(x, y)
indexLargestX = x
indexLargestY = y
'to stop
'here iwant to store the largest index of x and y so that i can use that to check with next largest value must be not in same index whether x or y,
'but look like here the value is not add inside 'usedX' and 'usedY'
If y = 3 Then
usedX.Add(indexLargestX)
usedY.Add(indexLargestY)
End If
End If
End If
Next
writer.Write(vbCrLf + "Largest number in row")
writer.Write(vbCrLf + largest.ToString + " ")
totLargest = totLargest + largest
Next
writer.Write(vbCrLf + "total : " + totLargest.ToString + " ")
End Using
End Sub