POJ 题目1915 Knight Moves(双向BFS)

来源:互联网 发布:淘宝直通车开通怎么用 编辑:程序博客网 时间:2024/06/09 23:56
Knight Moves
Time Limit: 1000MS Memory Limit: 30000KTotal 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

380 07 01000 030 50101 11 1

Sample Output

5280

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


0 0