private int BestFSAlgoritm()
        {
            tablica = ConvertListsToArray(ListPol);
            TupleList <int, Pole> priorityQueue = new TupleList <int, Pole>();


            bool[,] visitedTable = new bool[50, 50];
            for (int i = 0; i < 50; i++)
            {
                for (int j = 0; j < 50; j++)
                {
                    visitedTable[i, j] = false;
                }
            }

            Pole start;
            Pole end;
            Pole firstStackNode = new Pole();
            Pole tmp            = new Pole();

            Koszt = 0;
            if ((start = GetStartNode(tablica)) == null)
            {
                MessageBox.Show("The START node doesn't exist");
                return(-1);
            }
            else if ((end = GetEndNode(tablica)) == null)
            {
                MessageBox.Show("The End node doesn't exist");
                return(-1);
            }



            NodesTree drzewo = new NodesTree(start);



            priorityQueue.Add(BestFirstFunction(start, end), start);
            visitedTable[start.XPosition, start.YPosition] = true;

            while (priorityQueue.Count != 0)
            {
                priorityQueue.Sort();
                firstStackNode = priorityQueue.First().Item2;
                priorityQueue.RemoveAt(0);
                if (firstStackNode.TypSciany != Block.Start)
                {
                    Koszt++;
                    firstStackNode.TypSciany = Block.Special;
                    Thread.Sleep(Delay);
                    firstStackNode.TypSciany = Block.Visited;
                }


                if (firstStackNode.YPosition != 0)
                {
                    tmp = tablica[firstStackNode.YPosition - 1, firstStackNode.XPosition];

                    if (visitedTable[tmp.XPosition, tmp.YPosition] == true)
                    {
                    }
                    else if (tmp.TypSciany == Block.Hall)
                    {
                        priorityQueue.Add(BestFirstFunction(tmp, end), tmp);
                        visitedTable[tmp.XPosition, tmp.YPosition] = true;
                        drzewo.addChild(firstStackNode, tmp);
                    }
                    else if (tmp.TypSciany == Block.End)
                    {
                        DlugoscSciezki = drzewo.ReturnWay(firstStackNode).Count;
                        drzewo.ReturnWay(firstStackNode).ForEach(x => x.TypSciany = Block.Special);
                        return(Koszt);
                    }
                }
                if (firstStackNode.XPosition != 0)
                {
                    tmp = tablica[firstStackNode.YPosition, firstStackNode.XPosition - 1];
                    if (visitedTable[tmp.XPosition, tmp.YPosition] == true)
                    {
                    }
                    else if (tmp.TypSciany == Block.Hall)
                    {
                        priorityQueue.Add(BestFirstFunction(tmp, end), tmp);
                        visitedTable[tmp.XPosition, tmp.YPosition] = true;
                        drzewo.addChild(firstStackNode, tmp);
                    }
                    else if (tmp.TypSciany == Block.End)
                    {
                        DlugoscSciezki = drzewo.ReturnWay(firstStackNode).Count;
                        drzewo.ReturnWay(firstStackNode).ForEach(x => x.TypSciany = Block.Special);
                        return(Koszt);
                    }
                }
                if (firstStackNode.YPosition != iloscKolumnY - 1)
                {
                    tmp = tablica[firstStackNode.YPosition + 1, firstStackNode.XPosition];
                    if (visitedTable[tmp.XPosition, tmp.YPosition] == true)
                    {
                    }
                    else if (tmp.TypSciany == Block.Hall)
                    {
                        priorityQueue.Add(BestFirstFunction(tmp, end), tmp);
                        visitedTable[tmp.XPosition, tmp.YPosition] = true;
                        drzewo.addChild(firstStackNode, tmp);
                    }
                    else if (tmp.TypSciany == Block.End)
                    {
                        DlugoscSciezki = drzewo.ReturnWay(firstStackNode).Count;
                        drzewo.ReturnWay(firstStackNode).ForEach(x => x.TypSciany = Block.Special);
                        return(Koszt);
                    }
                }
                if (firstStackNode.XPosition != iloscWierszyX - 1)
                {
                    tmp = tablica[firstStackNode.YPosition, firstStackNode.XPosition + 1];
                    if (visitedTable[tmp.XPosition, tmp.YPosition] == true)
                    {
                    }
                    else if (tmp.TypSciany == Block.Hall)
                    {
                        priorityQueue.Add(BestFirstFunction(tmp, end), tmp);
                        visitedTable[tmp.XPosition, tmp.YPosition] = true;
                        drzewo.addChild(firstStackNode, tmp);
                    }
                    else if (tmp.TypSciany == Block.End)
                    {
                        DlugoscSciezki = drzewo.ReturnWay(firstStackNode).Count;
                        drzewo.ReturnWay(firstStackNode).ForEach(x => x.TypSciany = Block.Special);
                        return(Koszt);
                    }
                }
            }
            return(-1);
        }
        public int DFSRecirsive(Pole obecny)
        {
            Pole tmp;
            int  wynik = 0;

            if (FIND == true)
            {
                return(wynik);
            }


            if (obecny.TypSciany != Block.Start)
            {
                obecny.TypSciany = Block.Special;
                Thread.Sleep(Delay);
                obecny.TypSciany = Block.Visited;
                Koszt++;
            }


            if (obecny.YPosition != 0)
            {
                tmp = tablica[obecny.YPosition - 1, obecny.XPosition];
                if (tmp.TypSciany == Block.Hall)
                {
                    wynik += DFSRecirsive(tmp);
                }

                if (tmp.TypSciany == Block.End)
                {
                    DlugoscSciezki++;
                    obecny.TypSciany = Block.Special;
                    FIND             = true;
                    return(1);
                }
            }


            if (obecny.XPosition != 0)
            {
                tmp = tablica[obecny.YPosition, obecny.XPosition - 1];
                if (tmp.TypSciany == Block.Hall)
                {
                    wynik += DFSRecirsive(tmp);
                }
                if (tmp.TypSciany == Block.End)
                {
                    DlugoscSciezki++;
                    obecny.TypSciany = Block.Special;
                    FIND             = true;
                    return(1);
                }
            }



            if (obecny.YPosition != iloscKolumnY - 1)
            {
                tmp = tablica[obecny.YPosition + 1, obecny.XPosition];
                if (tmp.TypSciany == Block.Hall)
                {
                    wynik += DFSRecirsive(tmp);
                }
                if (tmp.TypSciany == Block.End)
                {
                    DlugoscSciezki++;
                    obecny.TypSciany = Block.Special;
                    FIND             = true;
                    return(1);
                }
            }
            if (obecny.XPosition != iloscWierszyX - 1)
            {
                tmp = tablica[obecny.YPosition, obecny.XPosition + 1];
                if (tmp.TypSciany == Block.Hall)
                {
                    wynik += DFSRecirsive(tmp);
                }
                if (tmp.TypSciany == Block.End)
                {
                    DlugoscSciezki++;
                    obecny.TypSciany = Block.Special;
                    FIND             = true;
                    return(1);
                }
            }

            if (wynik != 0 && obecny.TypSciany != Block.Start)
            {
                DlugoscSciezki++;
                obecny.TypSciany = Block.Special;
            }


            return(wynik);
        }