예제 #1
0
    private ArrayList openset = new ArrayList(); //ýòî íàáîð âñåõ åùž íå ïðîéäåííûõ, íî óæå îöåíåííûõ òî÷åê

    #endregion Fields

    #region Methods

    //ýòî ðåàëèçàöèÿ àëãîðèòìà À*
    public ArrayList algAStar(/*ArrayList allset, */cell start, cell goal)
    {
        cell p = start;
            int tentative_g_score;//îöåíêà ñòîèìîñòè ïóòè ÷åðåç òî÷êó
            bool tentative_is_better;//èñòèííî, åñëè ïóòü ÷åðåç òî÷êó ëó÷øå óæå ïðîéäåííîãî ïóòè
            ArrayList nearcells  = new ArrayList();
            //closedset = null;
            openset.Add(start);
            start.g = 0;
            start.h = Convert.ToInt32(start.estimate_cost(goal));
            nearcells = allcellsNearX(openset,closedset,start);
            p = lowestF(nearcells);
            start.f = start.g + start.h;
            int k = 0;
            while(openset.Count>0)
            {
                k++;
                //Debug.Log ("P:"+Convert.ToString(p.x)+' '+Convert.ToString(p.y));
                nearcells = allcellsNearX(openset,closedset,p);
                p = lowestF(nearcells);
                if(k==100 || p == goal){
                    return reconstructPath(start,goal);
                }
                /*foreach(cell c in nearcells){
                    Debug.Log (Convert.ToString(c.x)+' '+Convert.ToString(c.y));
                }*/
                openset.Remove(p);
                closedset.Add(p);
                foreach(cell y in nearcells)//ïðîâåðÿåì âñå òî÷êè, ïðèìûêàþùèå ê õ
                {
                    if((closedset.IndexOf(y)!=-1)||(y.isPassable()==false))
                    {
                        continue;//åñëè òî÷êà y óæå ïðîéäåíà èëè íåïðîõîäèìà, ïðîïóñêàåì åž
                    }
                    tentative_g_score = p.g + 1;//îöåíèâàåì ðàññòîÿíèå äëÿ ïðîõîæäåíèÿ ÷åðåç òî÷êó
                    if(openset.IndexOf(y)==-1)
                    {
                        openset.Add(y);
                        tentative_is_better = true;
                    }
                    else
                    {
                        if(tentative_g_score < y.g)
                        {tentative_is_better = true;}
                        else{tentative_is_better = false;}
                    }
                    if(tentative_is_better)
                    {
                        y.parent = p;
                        y.g = tentative_g_score;
                        y.h = Convert.ToInt32(y.estimate_cost(goal));
                        y.f = y.h + y.g;
                        allset.Add(y);
                    }
                }
            }
            return null;
    }