Imports System.IO Public Class Worker Public vychozi(1) As Integer Public cil(1) As Integer Public vysledek As Boolean Public jedubezzdi As Boolean Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim ctecka As New StreamReader(soubor) Dim intro As String = ctecka.ReadLine sirka = intro.Split(" ")(0) - 1 vyska = intro.Split(" ")(1) - 1 max = (sirka + 1) * (vyska + 1) + 1 Dim pole(sirka, vyska) As Char For i As Integer = 0 To vyska Dim radek As String = ctecka.ReadLine For j As Integer = 0 To sirka If radek(j) = "&" Then vychozi = {i, j} ElseIf radek(j) = "#" Then cil = {i, j} ElseIf radek(j) = "X" Then max -= 1 End If pole(i, j) = radek(j) Next Next ctecka.Close() mapa = pole vysledek = Hybej(vychozi, 1) End Sub Function Hybej(PraveStojim() As Integer, vrstva As Integer) As Boolean Dim vraceni As Boolean = False Dim moznosti As New List(Of Integer()) If vrstva > max Then Return False If KamJsemSlapl(PraveStojim) = 1 Then If vrstva < max Then tahy.Clear() tahy.Add(PraveStojim) max = vrstva vraceni = True End If ElseIf KamJsemSlapl(PraveStojim) = 0 Then moznosti.Add({PraveStojim(0) + 1, PraveStojim(1)}) moznosti.Add({PraveStojim(0), PraveStojim(1) + 1}) moznosti.Add({PraveStojim(0) - 1, PraveStojim(1)}) moznosti.Add({PraveStojim(0), PraveStojim(1) - 1}) moznosti.Sort(AddressOf PorovnejVzdalenosti) For i As Integer = 0 To 3 If Hybej(moznosti.Item(i), vrstva + 1) Then tahy.Add(PraveStojim) vraceni = True End If Next End If Return vraceni End Function Function KamJsemSlapl(jdina() As Integer) As Integer If (jdina(0) < 0) Or (jdina(0) > sirka) Or (jdina(1) < 0) Or (jdina(1) > vyska) Then 'mimo mapu Return -1 ElseIf mapa(jdina(0), jdina(1)) = "X" Then 've zdi Return -2 ElseIf mapa(jdina(0), jdina(1)) = "#" Then 'v cili Return 1 Else 'pohoda Return 0 End If End Function Private Sub Worker_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load BackgroundWorker1.RunWorkerAsync() End Sub Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted If vysledek = False Then MsgBox("Cesta neexistuje!") Application.Exit() End If Dim neco As New Vykresleni neco.Show() Me.Close() End Sub Function PorovnejVzdalenosti(ByVal zajem1() As Integer, ByVal zajem2() As Integer) As Integer Dim prvni, druha As Integer prvni = Math.Abs(zajem1(0) - cil(0)) + Math.Abs(zajem1(1) - cil(1)) druha = Math.Abs(zajem2(0) - cil(0)) + Math.Abs(zajem2(1) - cil(1)) If prvni = druha Then Return 0 Else Return prvni - druha End If End Function End Class