資源描述:
《基于c語(yǔ)言的廣度優(yōu)先搜索》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在學(xué)術(shù)論文-天天文庫(kù)。
1、基于C語(yǔ)言的廣度優(yōu)先搜素算法的實(shí)現(xiàn)1.算法說(shuō)明廣度優(yōu)先搜索使用隊(duì)列(queue)來(lái)實(shí)現(xiàn),整個(gè)過(guò)程也可以看做一個(gè)倒立的樹(shù)形:(1)把根節(jié)點(diǎn)放到隊(duì)列的末尾。(2)每次從隊(duì)列的失部取出一個(gè)元素,查看這個(gè)元素所有的下一級(jí)元素,把它們放到隊(duì)列的末尾。并把這個(gè)元素記為它下一級(jí)元素的前驅(qū)。(3)找到所要找的元素吋結(jié)束程序。(4)如果遍歷整個(gè)樹(shù)還沒(méi)有找到,結(jié)束程序。本次算法的應(yīng)用中,我用這個(gè)隊(duì)列來(lái)保存最短路徑。首先我定義隊(duì)列為“真進(jìn)假出”,所謂“真進(jìn)”就是當(dāng)添加一個(gè)元素時(shí),把元素放置隊(duì)尾,然后rear++,而“假出”就是當(dāng)刪除一個(gè)元素吋,并沒(méi)有真的刪除隊(duì)首
2、元素,只是front++。通過(guò)比較搜索所得的所有可行路徑的長(zhǎng)度,這樣我們就可以從隊(duì)列屮獲取一條最短路徑!1.代碼及結(jié)果^includeftdefineNtypcdcfstruct{intx;inty;}Node;/*雙?親X的?序6號(hào)?*//*雙?親X的?序d號(hào)?*//*孩C子哩?的?坐?標(biāo)括?//*隊(duì)6元a素?類無(wú)?型i*/typcdcfstruct{intparent:intchild;Nodechi1dpos:}QType;intMaze[N][N]={1111111111111111111intvisitcd[N]
3、[N]={1,11111111,1,1,1,1,1,1,1,1,1,1,1,1,10,0,0,1,0,0,0,1,1,0,0,1,0,00,0,1,0,0,0,0,1,0,0,0,0,1,10,0,0,0,0,0,1,0,0,1,0,0,0,11,0,1,0,0,0,0,0,0,0,0,1,0,00,0,0,0,1,0,0,0,0,1,0,0,0,01,0,0,0,0,1,1,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