bfs學習

今天作到了bfs的練習,順便寫下心得。。。git

bfs能解決搜索和最短路徑的問題。學習

下面是學習心得:ui

typedef struct point                //定義點
{
    int x;
    int y;
}P;
bfs()
{
    int level[N];                   //記錄隊列中元素層數,即從起點到該點最短路徑距離
    P father[NX][NY],queue[N];      //father用來記錄父節點,queue用來記錄隊列
    int top=0;                      //top用來記錄隊列長度,並指向最後節點
    for(int i=0;i<nx;i++)
        for(int j=0;j<ny;j++)
        {
            father[i][j].x=-1;      //初始化father
            father[i][j].y=-1;
        }
    queue[top].x=x0;                //對起點進行賦值
    queue[top].y=y0;
    father[x0][y0].x=x0;
    father[x0][y0].y=y0;
    top++;
    for(int i=0;i<top;i++)
    {
        for(遍歷第i個元素的鄰接點)
        {
            if(鄰接點沒標記)          //若鄰接點father爲-1
            {
                父節點記爲第i個元素;
                queue[top]賦值爲該鄰接點;      //把元素接到隊列上
                level[top]=level[i]+1;          //元素層次爲父節點層次+1
                top++;                          //隊列長度+1
            }
            if(鄰接點知足條件)
            {
                return ...;
            }
        }
    }
}
上一道訓練題:

Descriptionthis

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.

Inputspa

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.

Outputcode

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 Outputip

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.

個人題解(很水,有改進空間):

#include<stdio.h>
#include<string.h>
int dx[8]={-1,-1,-2,-2,1,1,2,2};        //對應的xy變化表
int dy[8]={2,-2,1,-1,2,-2,1,-1};
typedef struct point
{
    int x;
    int y;
}P;
int bfs(char* s1,char* s2)
{
    int x1=(int)(s1[0]-'a'+1);          //轉化爲數字
    int x2=(int)(s2[0]-'a'+1);
    int y1=(int)(s1[1]-'0');
    int y2=(int)(s2[1]-'0');
    int level[80],top=0,flag;           //top用來記錄隊列長度,並可用於指向隊尾
    P father[10][10],queue[80];         //father記錄父節點,queue記錄隊列
    for(int i=0;i<10;i++)
        for(int j=0;j<10;j++)
        {
            father[i][j].x=-1;          //父節點初始化爲-1
            father[i][j].y=-1;
        }
    level[top]=0;
    father[x1][y1].x=x1;
    father[x1][y1].y=y1;
    queue[top].x=x1;
    queue[top++].y=y1;
    if(x1==x2&&y1==y2)
        return 0;
    for(int i=0;i<top;i++)
    {
        flag=0;
        for(int j=0;j<8;j++)            //標記全部鄰接點
        {
            int tempx,tempy;
            tempx=queue[i].x+dx[j];
            tempy=queue[i].y+dy[j];
            if(tempx<=8&&tempx>0&&tempy<=8&&tempy>0&&father[tempx][tempy].x==-1)
            {
                father[tempx][tempy].x=queue[i].x;
                father[tempx][tempy].y=queue[i].y;
                queue[top].x=tempx;
                queue[top].y=tempy;
                level[top++]=level[i]+1;      //level用來記錄當前元素層次
            }
            if(tempx==x2&&tempy==y2)
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
            return level[top-1];
    }
}
int main()
{
    char s1[3],s2[3];
    int num;
    while(scanf("%s%s",s1,s2)!=EOF)
    {
        num=bfs(s1,s2);         //輸入
        printf("To get from %s to %s takes %d knight moves.\n",s1,s2,num);
    }
    return 0;
}
相關文章
相關標籤/搜索