[Poj 1915]Knight Moves

描述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.

输入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.输出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.样例输入

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

样例输出

5
28
0

翻译

题目描述

骑士的行走方式类似于象棋中的马,现在给你一个任务,计算骑士从一点到另一点所需的最少步数。

输入

第一行给出骑士的数量n。对于每一个骑士都有3行,第一行一个整数L,表示棋盘的大小(4<=L<=300),整个棋盘大小为LxL(坐标范围为0...L);第二行和第三行分别包含一对整数(xy),表示骑士的起始点和终点。假设,对于每一个骑士起始点和终点均合理。

输出

对每一个骑士输出一行,一个整数表示需要移动的最少步数。如果起始点和终点相同,则输出0。

样例输入

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

样例输出

5
28
0

看到是最少移动次数,想到搜索,看到数据范围300*300,dfs会炸,所以果断使用bfs,而单向bfs太慢,所以使用双向bfs
单项bfs:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#pragma GCC optimize(2)
using namespace std;
inline int read()
{
    int f=1,ans=0;char c;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
    return f*ans;
}
int dy[8]={1,2,2,1,   -1,-2,-2,-1};
int dx[8]={2,1,-1,-2, -2, 1,-1,2};
int book[301][301];
struct node{
    int x;
    int y;
    int minn;
};
int main()
{
    int k=read();
 
    while(k--)
    {
        int l=read();
        int ex=read(),ey=read(),fx=read(),fy=read();
        node s;
        s.x=ex,s.y=ey;
        s.minn=0;
        book[ex][ey]=1;
        queue<node> que;
        que.push(s);
        while(!que.empty())
        {
            node ss=que.front(); que.pop();
            int xx=ss.x,yy=ss.y;
            //cout<<xx<<" "<<yy<<" "<<ss.minn<<endl;
            if(xx==fx&&yy==fy){cout<<ss.minn<<endl;break;}
            for(int i=0;i<8;i++)
            {
                int xxx=dx[i]+xx,yyy=dy[i]+yy;
                if(xxx>=0&&xxx<=l&&yyy>=0&&yyy<=l)
                {
                    node sry=ss;
                    sry.x=xxx;
                    sry.y=yyy;
                    if(book[xxx][yyy]==0)
                    {
                        sry.minn=ss.minn+1;
                        book[xxx][yyy]=1;   
                        que.push(sry);
                         
                    }
                     
                }
             } 
        }
        memset(book,0,sizeof(book));
    }
}

双向bfs:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#pragma GCC optimize(3)
using namespace std;
inline void write(int x)
{
    int f=0;char ch[20];
    if(!x){puts("0");return;}
    if(x<0){putchar('-');x=-x;}
    while(x)ch[++f]=x%10+'0',x/=10;
    while(f)putchar(ch[f--]);
    putchar('\n');
}
 
inline int read() {
    int f=1,ans=0;
    char c;
    while(c<'0'||c>'9') {
        if(c=='-')f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9') {
        ans=ans*10+c-'0';
        c=getchar();
    }
    return f*ans;
}
int dy[8]= {1,2,2,1,   -1,-2,-2,-1},dx[8]= {2,1,-1,-2, -2, 1,-1,2},minn1[301][301],minn2[301][301];
bool book1[301][301],book2[301][301];
struct node {
    int x,y,minn;
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int k=read();
    while(k--) {
        int l=read();
        int ex=read(),ey=read(),fx=read(),fy=read();
        node s;
        s.x=ex,s.y=ey;
        s.minn=0;
        book1[ex][ey]=1;
        queue<node> que;
        queue<node> que1;
        que.push(s);
        book2[fx][fy]=1;
        s.x=fx,s.y=fy,s.minn=0;
        que1.push(s);
        while(1) {
            node ss=que.front(),ss1=que1.front();
            int xx=ss.x,yy=ss.y;
            if(book2[xx][yy]) {
                write(minn1[xx][yy]+minn2[xx][yy]);
                break;
            }
            que.pop(),que1.pop();
            bool f=false;
            for(int i=0; i<8; i++) {
                int xxx=dx[i]+xx,yyy=dy[i]+yy;
                if(xxx>=0&&xxx<l&&yyy>=0&&yyy<l) {
                    node sry=ss;
                    sry.x=xxx;
                    sry.y=yyy;
                    if(!book1[xxx][yyy]) {
                        if(book2[xxx][yyy]) {
                            write(minn2[xxx][yyy]+ss.minn+1);
                            f=true;
                            break;
                        }
                        sry.minn=ss.minn+1;
                        book1[xxx][yyy]=1;
                        minn1[xxx][yyy]=sry.minn;
                        que.push(sry);
                    }
                }
            }
            if(f) break;
            xx=ss1.x,yy=ss1.y;
            for(int i=0; i<8; i++) {
                int xxx=dx[i]+xx,yyy=dy[i]+yy;
                if(xxx>=0&&xxx<l&&yyy>=0&&yyy<l) {
                    node sry=ss1;
                    sry.x=xxx;
                    sry.y=yyy;
   
                    if(!book2[xxx][yyy]) {
                        if(book1[xxx][yyy]) {
                            write(minn1[xxx][yyy]+ss1.minn+1);
                            f=true;
                            break;
                        }
                        sry.minn=ss1.minn+1;
                        book2[xxx][yyy]=1;
                        minn2[xxx][yyy]=sry.minn;
                        que1.push(sry);
                    }
                }
            }
            if(f) break;
        }
        memset(book1,0,sizeof(book1));
        memset(book2,0,sizeof(book2));
        memset(minn1,0,sizeof(minn1));
        memset(minn2,0,sizeof(minn2));
    }
}
 

猜你喜欢

转载自www.cnblogs.com/si-rui-yang/p/9343844.html
今日推荐