資源簡介
今天進行程序的測試,發(fā)現(xiàn)運算速度相當緩慢,當使用876,543,210這個矩陣變換,運算了130k+步驟,耗時有半個小時多。經(jīng)過簡單
計算,九個格子放入九個數(shù),就有A99種排列組合,結(jié)果是360K+,所以當程序運行到100K+的時候,我還是在耐心等待,不過幫我測試的同學(xué)可沒
有我的耐心,早早得都關(guān)了。-_-|| 所以在昨天晚上抱著嘗試的心態(tài),寫了A*算法。該算法就是有序搜索,與盲目搜索的不同之處就是多了一個跟
據(jù)一定的策略,從open表中找一個最容易產(chǎn)生結(jié)果的結(jié)點進行擴展。在這個程序中,該策略就是找到與目標狀態(tài)數(shù)字的排列最接近的結(jié)點進行擴展。
結(jié)果再輸入 876,543,210 只有經(jīng)過700+,改進速度提升了幾個數(shù)量級,結(jié)果還是另人滿意的。

代碼片段和文件信息
//this?is?main.cpp
//?九宮格?數(shù)字移動就是?將一個九個格子中初始填充1~8?然后經(jīng)過移動?使之變成目標狀態(tài)
/*
?????初始狀態(tài)??????????1?2?3?????????????1?2?3?
???????????????????4???5??????->?????4?5?6?
???????7?8?6?????????????7?8?
*/
//~
//#include“data.h“
#include“search.h“
void?main()
{
cout?<“********************************************************************************“?<????cout?<“this?program?is?about?the?movement?of?number.?In?details?the?matrix‘length?is?3“?<????cout?<“and?the?width?is?3.Now?input?8?numbers?from?1?to?8?(0?means?blank).?The?program“??<????cout?<“will?move?these?8?numbers?from?initial?state?to?final?state?automatically???????“?<????cout?<“For?example:“?<????cout?<“1?2?3?????????????1?2?3??“?<????cout?<“4?0?5??????->?????4?5?6??“?<????cout?<“7?8?6?????????????7?8?0??“?< cout?<“Now?please?the?8?numbers?(0~8)?and?blank?(the?blank?is?zero)?“?<
initial(head);
int?i?j;
for(i?=?0;?i?3;?i++)
for(j?=?0;?j?3;?j++)
{
cout?<“(“?< cin?>>?head.grid[i][j];
}????
findzore(head);
????findweight(head);
pnode?p?=?&head;
pnode?r?=?search(p);
if(?r?!=?NULL)
????{
cout?<“^_^?found!!“?<????????output(r);
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????6706??2008-11-26?22:35??data.h
?????文件???????1360??2008-11-26?22:38??main.cpp
?????文件???????1874??2008-11-25?11:05??readme.txt
?????文件???????5067??2008-11-26?22:34??search.h
-----------?---------??----------?-----??----
????????????????15007????????????????????4
評論
共有 條評論