Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' <summary>
- ''' Структура, использующаяся для вывода пути.
- ''' </summary>
- Public Structure Point
- Public Sub New(x As Int32, y As Int32, value As Int32)
- Me.X = x
- Me.Y = y
- Me.Value = value
- End Sub
- Public ReadOnly X As Int32, Y As Int32, Value As Int32
- Public Overrides Function ToString() As String
- Return String.Format("{0}, {1} : {2}", X, Y, Value)
- End Function
- End Structure
- Public Module Module1
- Public Sub Main(args As [String]())
- arr = New Int32(,) {{2, 5, 1, 0}, {3, 3, 1, 9}, {4, 4, 7, 8}}
- 'Dim size1 As Int32 = 4000, size2 As Int32 = 4000
- 'arr = New Int32(size1 - 1, size2 - 1) {}
- 'Dim random As New Random()
- 'For x As Int32 = 0 To size1 - 1
- ' For y As Int32 = 0 To size2 - 1
- ' arr(x, y) = random.[Next]()
- ' Next
- 'Next
- Dim timer = New Stopwatch()
- timer.Start()
- InitializeMap()
- Dim path = New List(Of Point)()
- BacktracePath(path, longestPathEnd.X, longestPathEnd.Y)
- path.Reverse()
- timer.Stop()
- Console.WriteLine("Длина пути: {0}", path.Count)
- Console.WriteLine("Память: {0} MiB", Process.GetCurrentProcess().WorkingSet64 / (1024 * 1024))
- Console.WriteLine("Временя: {0} ms", timer.ElapsedMilliseconds)
- path.ForEach(Sub(p) Console.WriteLine(p.ToString))
- End Sub
- Private arr As Int32(,), map As Int32(,)
- Private longestPathEnd As Point
- ''' <summary>
- ''' Инициализирует карту для волнового алгоритма и выполняет его.
- ''' </summary>
- Private Sub InitializeMap()
- map = New Int32(arr.GetLength(0) - 1, arr.GetLength(1) - 1) {}
- For x As Int32 = 0 To map.GetLength(0) - 1
- For y As Int32 = 0 To map.GetLength(1) - 1
- map(x, y) = -1
- Next
- Next
- For x As Int32 = 0 To map.GetLength(0) - 1
- For y As Int32 = 0 To map.GetLength(1) - 1
- WaveExpansion(x, y)
- Next
- Next
- End Sub
- ''' <summary>
- ''' Распространяет волну от указанного элемента. Если соседние клетки не заполнены содержат меньшее значение (т.е. через них прошла более "слабая" волна), они будут перезаписаны.
- ''' </summary>
- Private Sub WaveExpansion(x As Int32, y As Int32, Optional range As Int32 = 0)
- If map(x, y) < range Then
- map(x, y) = range
- If range > longestPathEnd.Value Then
- longestPathEnd = New Point(x, y, map(x, y))
- End If
- Dim thisCellValue As Int32 = arr(x, y)
- If x > 0 Then
- If arr(x - 1, y) > thisCellValue Then
- WaveExpansion(x - 1, y, range + 1)
- End If
- End If
- If x < map.GetLength(0) - 1 Then
- If arr(x + 1, y) > thisCellValue Then
- WaveExpansion(x + 1, y, range + 1)
- End If
- End If
- If y > 0 Then
- If arr(x, y - 1) > thisCellValue Then
- WaveExpansion(x, y - 1, range + 1)
- End If
- End If
- If y < map.GetLength(1) - 1 Then
- If arr(x, y + 1) > thisCellValue Then
- WaveExpansion(x, y + 1, range + 1)
- End If
- End If
- End If
- End Sub
- ''' <summary>
- ''' Восстанавливает путь волны от указанного элемента.
- ''' </summary>
- Private Sub BacktracePath(path As List(Of Point), x As Int32, y As Int32)
- path.Add(New Point(x, y, arr(x, y)))
- Dim soughtCellValue As Int32 = map(x, y) - 1
- If x > 0 Then
- If map(x - 1, y) = soughtCellValue Then BacktracePath(path, x - 1, y)
- End If
- If x < map.GetLength(0) - 1 Then
- If map(x + 1, y) = soughtCellValue Then BacktracePath(path, x + 1, y)
- End If
- If y > 0 Then
- If map(x, y - 1) = soughtCellValue Then BacktracePath(path, x, y - 1)
- End If
- If y < map.GetLength(1) - 1 Then
- If map(x, y + 1) = soughtCellValue Then BacktracePath(path, x, y + 1)
- End If
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement