A 演算法的理解和使用 C 版本

2021-09-01 22:35:39 字數 3453 閱讀 6387

專案中使用到了自動尋路這些功能,正好總結一下a*的各種概念和個人的理解

目錄

1、a*演算法的基本概念

2、a*演算法的尋路過程

3、**

4、**

啟發式搜尋就是在狀態空間中的搜尋對每乙個搜尋的位置進行評估,得到最好的位置,再從這個位置進行搜尋直到目標。這樣可以省略大量無畏的搜尋路徑,提到了效率。在啟發式搜尋中,對位置的估價是十分重要的。採用了不同的估價可以有不同的效果。

圖中的搜尋區域被劃分為了簡單的二維陣列,陣列每個元素對應乙個小方格,當然我們也可以將區域等分成是五角星,矩形等,通常將乙個單位的中心點稱之為搜尋區域節點(node)。

我們將路徑規劃過程中待檢測的節點存放於open list中,而已檢測過的格仔則存放於close list中。

我們將路徑規劃過程中已經檢查過的節點放在close list。

在路徑規劃中用於回溯的節點。

h為啟發函式,也被認為是一種試探,由於在找到唯一路徑前,我們不確定在前面會出現什麼障礙物,因此用了一種計算h的演算法,具體根據實際場景決定。在我們簡化的模型中,h採用的是傳統的曼哈頓距離(manhattan distance)估價函式,也就是橫縱向走的距離之和。

h(n) = d * (abs ( n.x – goal.x ) + abs ( n.y – goal.y ) )

f(n) = g + h。

f代表當前檢查點的總花費,g代表起點到當前檢查點的花費,h代表當前檢查點到終點的預估花費。

當然,h的函式可以是其他的估價函式, 例如 : 對角線距離和歐幾里得距離,比較麻煩 ,可以自己查查

1.將起點a新增到open列表中(a沒有計算花費f是因為當前open列表只有這乙個節點)。 2.檢查open列表,選取花費f最小的節點m(檢查m如果為終點是則結束尋路,如果     open列表    沒有則尋路失敗,直接結束)。 3.對於與m相鄰的每一節點n:     1>如果n是阻擋障礙,那麼不管它。     2>如果n在closed列表中,那麼不管它。     3>如果n不在open列表中:新增它然後計算出它的花費    f(n)=g+h。     4>如果n已在open列表中:當我們使用當前生成的路徑時,    檢查f花費是否    更小。如果是,更新它的花費f和它的    父節點。 4.重複2,3步。

(**是之前網上的 ,但是有錯誤,修改了一下 ,可以使用)

using system;

using system.collections.generic;

using system.linq;

listcloselist;

listopenlist;

public maze(int[,] maze)

public point findpath(point start, point end, bool isignorecorner)

if (openlist.get(end) != null)

return openlist.get(end);

if (count>3000) // 迴圈了3000次跳出 防止卡死

return null;

}return openlist.get(end);

}private void foundpoint(point tempstart, point point)

}private void notfoundpoint(point tempstart, point end, point point)

private int calcg(point start, point point)

private int calch(point end, point point)

//獲取某個點周圍可以到達的點

public listsurrroundpoints(point point, bool isignorecorner)

return surroundpoints;

}//在二維陣列對應的位置不為障礙物

private bool canreach(int x, int y)

public bool canreach(point start, int x, int y, bool isignorecorner)}}

}//point 型別

public class point

public int f //f=g+h

public int g

public int h

public int x

public int y

public point(int x, int y)

public void calcf()

}//對 list的一些擴充套件方法

public static class listhelper

public static bool exists(this listpoints, int x, int y)

public static point minpoint(this listpoints)

public static void add(this listpoints, int x, int y)

public static point get(this listpoints, point point)

public static void remove(this listpoints, int x, int y)}}

}using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

, ,,,

,,,,

};maze maze = new maze(array);

point start = new point(1, 1);

point end = new point(7, 7);

console.writeline(system.datetime.now.ticks / 10000);

var parent = maze.findpath(start, end, false);

listpath = new list();

while (parent != null)

console.writeline(system.datetime.now.ticks / 10000);

path.reverse();

for (int i = 0; i < array.getlength(0); i++)

else

}console.writeline();

console.writeline();}}

}

理解和使用c 中的反射

什麼是反射?reflection,中文翻譯為反射。這是.net中獲取執行時型別資訊的方式,net的應用程式由幾個部分 程式集 assembly 模組 module 型別 class 組成,而反射提供一種程式設計的方式,讓程式設計師可以在程式執行期獲得這幾個組成部分的相關資訊,例如 assembly類...

C 之 陣列的理解和使用

宣告乙個變數就是在記憶體中間劃分一塊合適的記憶體空間,宣告乙個陣列就是劃分出一組合適的記憶體空間。例如我們宣告乙個name變數 string name 小白 console.writeline name console.read 輸出的結果為 但是當我們需要很多名字的時候,比如乙個班級的所有同學的名...

排序演算法 C 版本

排序演算法應該是 資料結構與演算法 必須要掌握的,作為乙個合格的程式猿,不管是平時吹逼或者面試,理論來說應該是信手拈來且nobug,noerror,可是捫心自問是不是有這樣的效果?其實不然,不管是在面試還是平時做題,每次遇到還是心裡會有咯噔一下的感覺,一旦有膽怯的感覺就說明還是不夠紮實,反反覆覆練習...