POJ 题目1915 Knight Moves(双向BFS)

本文介绍了一种使用双向BFS算法解决骑士在棋盘上从一点到另一点的最短移动路径问题的方法。该算法适用于不同大小的棋盘,并提供了一个具体的C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Knight Moves
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 23530 Accepted: 11049

Description

Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.

Input

The input begins with the number n of scenarios on a single line by itself.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.

Output

For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.

Sample Input

3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1

Sample Output

5
28
0

Source

TUD Programming Contest 2001, Darmstadt, Germany

很水的BFS,练一下双向的。。

ac代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
int n;
int vis1[330][330],vis2[330][330];
int dirx[8]={2,1,-1,-2,-2,-1,1,2};
int diry[8]={1,2,2,1,-1,-2,-2,-1};
struct s
{
    int x,y;
    int step;
}a,b,temp;
int jud(struct s a)
{
    if(a.x<0||a.x>=n)
        return 0;
    if(a.y<0||a.y>=n)
        return 0;
    return 1;
}
void bfs()
{
    memset(vis1,-1,sizeof(vis1));
    memset(vis2,-1,sizeof(vis2));
    a.step=0;
    b.step=0;
    queue<struct s>q1,q2;
    vis1[a.x][a.y]=0;
    vis2[b.x][b.y]=0;
    int tmp1,tmp2;
    tmp1=tmp2=0;
    q1.push(a);
    q2.push(b);
    while(1)
    {
        while(!q1.empty()&&q1.front().step==tmp1)
        {
            a=q1.front();
            q1.pop();
            if(vis2[a.x][a.y]!=-1)
            {
                int k=vis2[a.x][a.y];
                printf("%d\n",k+a.step);
                return;
            }
            int i;
            for(i=0;i<8;i++)
            {
                temp.x=a.x+dirx[i];
                temp.y=a.y+diry[i];
                temp.step=a.step+1;
                if(!jud(temp))
                    continue;
                if(vis2[temp.x][temp.y]!=-1)
                {
                    int k=vis2[temp.x][temp.y];
                    printf("%d\n",k+temp.step);
                    return ;
                }
                if(vis1[temp.x][temp.y]==-1)
                {
                    vis1[temp.x][temp.y]=temp.step;
                    q1.push(temp);
                }
            }
        }
        tmp1=q1.front().step;
        while(!q2.empty()&&q2.front().step==tmp2)
        {
            b=q2.front();
            q2.pop();
            if(vis1[b.x][b.y]!=-1)
            {
                int k=vis1[b.x][b.y];
                printf("%d\n",b.step+k);
                return;
            }
            int i;
            for(i=0;i<8;i++)
            {
                temp.x=b.x+dirx[i];
                temp.y=b.y+diry[i];
                temp.step=b.step+1;
                if(!jud(temp))
                    continue;
                if(vis1[temp.x][temp.y]!=-1)
                {
                    int k=vis1[temp.x][temp.y];
                    printf("%d\n",k+temp.step);
                    return;
                }
                if(vis2[temp.x][temp.y]==-1)
                {
                    vis2[temp.x][temp.y]=temp.step;
                    q2.push(temp);
                }
            }
        }
        tmp2=q2.front().step;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        //int n;
        scanf("%d",&n);
        scanf("%d%d%d%d",&a.x,&a.y,&b.x,&b.y);
        if(a.x==b.x&&a.y==b.y)
        {
            printf("0\n");
            continue;
        }
        bfs();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值