11.16

單向BFS需要搜索N層才能到達(dá)終點(diǎn)柑营,在每個(gè)層需要進(jìn)行的判斷量(即通常的那個(gè)for循環(huán))為X。那么猎荠,單BFS的運(yùn)算量為:X^N。
如果換成雙BFS蜀备,那么前后各搜索 N/2層关摇,那么總的運(yùn)算量為:2 * ( X ^ ( N/2 ) )。顯然當(dāng)X比較大時(shí)碾阁,在運(yùn)算量上不僅僅不僅僅是減半那么簡單输虱。特別的,如果X=1脂凶,那么雙BFS也就退化成了單向BFS了宪睹,實(shí)際上,此時(shí)也就是可以用DFS來進(jìn)行深搜了蚕钦,而且代碼相對來說更加簡潔亭病。

poj 2243

Knight Moves
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14679 Accepted: 8226
Description

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input

The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
Output

For each test case, print one line saying "To get from xx to yy takes n knight moves.".
Sample Input

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
Sample Output

To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.

雙向BFS:

#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <queue>  
using namespace std;  
  
struct knight  
{  
    int x,y,step;  
};  
  
int dir[8][2]={{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{-1,2},{1,-2},{1,2}};  
int sx, sy, ex, ey;  
int visit[8][8];  
int color[8][8];  
int bfs();  
  
int main()  
{  
    int x1, x2;  
    char y1, y2;  
    while(scanf("%c%d %c%d", &y1, &x1, &y2, &x2) != EOF)  
    {  
        getchar();  
        sx = x1 - 1;  
        sy = y1 - 'a';  
        ex = x2 - 1;  
        ey = y2 - 'a';  
        memset(visit, -1, sizeof(visit));  
        memset(color, 0, sizeof(color));  
        int cost = bfs();  
        printf("To get from %c%d to %c%d takes %d knight moves.\n", y1, x1, y2, x2, cost);  
    }  
    return 0;  
}  
  
int bfs()  
{  
    if(sx == ex && sy == ey)  
        return 0;  
    queue<knight> que_front;  
    queue<knight> que_back;  
    knight front, back;  
    front.x = sx; front.y = sy; front.step = 0;  
    back.x = ex; back.y = ey; back.step = 1;  
    que_front.push(front);  
    que_back.push(back);  
    visit[sx][sy] = 0;  
    visit[ex][ey] = 1;  
    color[sx][sy] = 1;  
    color[ex][ey] = 2;  
    int ans1 = 0, ans2 = 0;  
    while(!que_front.empty() || !que_back.empty())  
    {  
        if(!que_front.empty())  
        {  
            front = que_front.front();  
            que_front.pop();  
            for(int i = 0; i < 8; i++)  
            {  
                int dx = front.x + dir[i][0];  
                int dy = front.y + dir[i][1];  
                if(dx >= 0 && dx < 8 && dy >= 0 && dy < 8 && color[dx][dy] != 1)  
                {  
                    if(color[dx][dy] == 0)  
                    {  
                        knight tmp;  
                        tmp.x = dx; tmp.y = dy;  
                        tmp.step = front.step + 1;  
                        visit[dx][dy] = tmp.step;  
                        color[dx][dy] = 1;  
                        que_front.push(tmp);  
                    }  
                    else  
                        return front.step + visit[dx][dy];  
                }  
            }  
        }  
        if(!que_back.empty())  
        {  
            back = que_back.front();  
            que_back.pop();  
            for(int i = 0; i < 8; i++)  
            {  
                int dx = back.x + dir[i][0];  
                int dy = back.y + dir[i][1];  
                if(dx >= 0 && dx < 8 && dy >= 0 && dy < 8 && color[dx][dy] != 2)  
                {  
                    if(color[dx][dy] == 0)  
                    {  
                        knight tmp;  
                        tmp.x = dx; tmp.y = dy;  
                        tmp.step = back.step + 1;  
                        visit[dx][dy] = tmp.step;  
                        color[dx][dy] = 2;  
                        que_back.push(tmp);  
                    }  
                    else  
                        return back.step + visit[dx][dy];  
                }  
                  
            }  
        }  
    }  
    return -1;  
}  
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市嘶居,隨后出現(xiàn)的幾起案子罪帖,更是在濱河造成了極大的恐慌,老刑警劉巖邮屁,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件整袁,死亡現(xiàn)場離奇詭異,居然都是意外死亡樱报,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門泞当,熙熙樓的掌柜王于貴愁眉苦臉地迎上來迹蛤,“玉大人,你說我怎么就攤上這事〉领” “怎么了嚷量?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長逆趣。 經(jīng)常有香客問我蝶溶,道長,這世上最難降的妖魔是什么宣渗? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任抖所,我火速辦了婚禮,結(jié)果婚禮上痕囱,老公的妹妹穿的比我還像新娘田轧。我一直安慰自己,他們只是感情好鞍恢,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布傻粘。 她就那樣靜靜地躺著,像睡著了一般帮掉。 火紅的嫁衣襯著肌膚如雪弦悉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天蟆炊,我揣著相機(jī)與錄音稽莉,去河邊找鬼。 笑死盅称,一個(gè)胖子當(dāng)著我的面吹牛肩祥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播缩膝,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼混狠,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了疾层?” 一聲冷哼從身側(cè)響起将饺,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎痛黎,沒想到半個(gè)月后予弧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡湖饱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年掖蛤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片井厌。...
    茶點(diǎn)故事閱讀 39,785評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蚓庭,死狀恐怖致讥,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情器赞,我是刑警寧澤垢袱,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站港柜,受9級特大地震影響请契,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜夏醉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一爽锥、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧授舟,春花似錦救恨、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至奢啥,卻和暖如春秸仙,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背桩盲。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工寂纪, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人赌结。 一個(gè)月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓捞蛋,卻偏偏與公主長得像,于是被迫代替她去往敵國和親柬姚。 傳聞我的和親對象是個(gè)殘疾皇子拟杉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,491評論 0 23
  • 最近的文章和朋友圈難到是要開跑步專輯嘛搬设?哈哈! 還真是研究了一下跑步該怎么跑的問題撕捍。說長跑應(yīng)該循序漸進(jìn)拿穴,初跑者要想...
    吳佟閱讀 251評論 0 0
  • 一直都覺得自己的內(nèi)心世界挺強(qiáng)大的,無論是面對挫折還是孤單的生活都可以從容面對忧风,結(jié)果那只是我的我以為默色。 大學(xué)恍恍惚惚...
    趙鐘毓閱讀 150評論 0 0
  • 歲月是一杯空自等待的茶 漫不經(jīng)心的疏忽 恍惚的黃昏明滅的燈火 光和影傾斜的角度 像站不穩(wěn)的 命運(yùn) 相遇或者離開 都...
    楊昊田閱讀 530評論 12 21
  • 雙調(diào)·得勝令·釣歸所見(去年舊作) 自序:八月二十五日,與林兄各成至岐下釣魚狮腿。傍晚歸航之際腿宰,夕陽漸下弟蚀,紅光映海,秋...
    沙湖小景閱讀 274評論 0 1