11.16

作者: 翘尾巴 | 来源:发表于2017-11-16 15:08 被阅读0次

单向BFS需要搜索N层才能到达终点,在每个层需要进行的判断量(即通常的那个for循环)为X。那么,单BFS的运算量为:X^N。
如果换成双BFS,那么前后各搜索 N/2层,那么总的运算量为:2 * ( X ^ ( N/2 ) )。显然当X比较大时,在运算量上不仅仅不仅仅是减半那么简单。特别的,如果X=1,那么双BFS也就退化成了单向BFS了,实际上,此时也就是可以用DFS来进行深搜了,而且代码相对来说更加简洁。

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;  
}  

相关文章

  • 每日一画43

    11.16

  • 【物理】天体物理学笔记

    2013.11.06—11.16

  • 83

    11.16晴礼拜六

  • 11.16

    做过一个关于进入大学一个月的调查问卷 里面有几题问道 你对你的大学满意吗 对大学生活还习惯吗 可想而知 我的答案 ...

  • 11.16

    梦里有人翩翩起舞 平行交织久了的道路 偶尔堵塞拥挤... 人们选择隐藏过往 我却隐瞒动容... 后来我们鼎力相遇 ...

  • 11.16

    大梦初醒荒唐了一生。 想逃离,这一切的一切, 让我压抑,让我窒息。 我只想过一段平平静静的日子。 不想荒唐了我的青...

  • 11.16

  • 11.16

    认识李老师这么多天了,有时候觉得他跟原来好像不一样了,可是想想又觉得还是一样的,一种很微妙的感觉——他还是原来那个...

  • 11.16

    只是坚持而坚持,让自己更简单,更纯粹,更高效! 针对每个学生的不同情况针对性的解决方法这是一对一,既然是班课,那么...

  • 11.16

    初一年上学期期中拔高类题型应该是在绝对值和应用题,接着开始学习整体带入思想和方程思想 初二年上学期期中拔高类题型应...

网友评论

      本文标题:11.16

      本文链接:https://www.haomeiwen.com/subject/hcilvxtx.html