POJ 1915

#include<iostream>
#include<stdio.h>
#define MAXN 350
#include"queue"
using namespace std;
bool mark[MAXN][MAXN];
struct point
{
    int x;
    int y;
    int step;
    point()
    {
        step = 0;
    }
};
bool bfs(point p);
point b;
int num;
point e;
point tem;
queue<point> coll;
int main()
{
    //freopen("acm.acm","r",stdin);
    int test;
    int ans;
    cin>>test;
    while(test --)
    {
        //memset(road,0,sizeof(road));
        memset(mark,false,sizeof(mark));
        cin>>num;
        cin>>b.x>>b.y;
        cin>>e.x>>e.y;
        if(b.x == e.x && b.y == e.y)
        {
            cout<<0<<endl;
            continue;
        }
        coll.push(b);
        mark[b.x][b.y] = true;
    //    cout<<"----------"<<endl;
        while(!coll.empty() && !bfs(coll.front()) )
        {
            //cout<<"-================"<<endl;
        //    cout<<coll.front().x<<endl;
        //    cout<<coll.front().y<<endl;
            //break;
            coll.pop();
        }
        while(!coll.empty())
        {
            coll.pop();
        }
    }
}

bool bfs(point p)
{
    if(p.x - 2 >= 0)
    {
        if(p.y - 1 >= 0 && !mark[p.x - 2][p.y - 1])
        {
            tem.x = p.x - 2;
            tem.y = p.y - 1;
            tem.step = p.step + 1;
            mark[tem.x][tem.y] = true;
            if(tem.x == e.x && tem.y == e.y)
            {
                cout<<tem.step<<endl;
                return true;
            }
            else
                coll.push(tem);
        }
        if(p.y + 1 < num && !mark[p.x - 2][p.y + 1])
        {
            tem.x = p.x - 2;
            tem.y = p.y + 1;
            tem.step = p.step + 1;
            mark[tem.x][tem.y] = true;
            if(tem.x == e.x && tem.y == e.y)
            {
                cout<<tem.step<<endl;
                return true;
            }
            else 
                coll.push(tem);
        }
    }
    ////////////////
    if(p.x + 2 < num)
    {
        if(p.y - 1 >= 0 && !mark[p.x + 2][p.y - 1])
        {
            tem.x = p.x + 2;
            tem.y = p.y - 1;
            tem.step = p.step + 1;
            mark[tem.x][tem.y] = true;
            if(tem.x == e.x && tem.y == e.y)
            {
                cout<<tem.step<<endl;
                return true;
            }
            else
                coll.push(tem);
        }
        if(p.y + 1 < num && !mark[p.x + 2][p.y + 1])
        {
            tem.x = p.x + 2;
            tem.y = p.y + 1;
            tem.step = p.step + 1;
            mark[tem.x][tem.y] = true;
            if(tem.x == e.x && tem.y == e.y)
            {
                cout<<tem.step<<endl;
                return true;
            }
            else 
                coll.push(tem);
        }
    }

    ///////////////////
    if(p.y - 2 >= 0)
    {
        if(p.x - 1 >= 0 && !mark[p.x - 1][p.y - 2])
        {
            tem.x = p.x - 1;
            tem.y = p.y - 2;
            tem.step = p.step + 1;
            mark[tem.x][tem.y] = true;
            if(tem.x == e.x && tem.y == e.y)
            {
                cout<<tem.step<<endl;
                return true;
            }
            else
                coll.push(tem);
        }
        if(p.x + 1 < num && !mark[p.x + 1][p.y - 2])
        {
            tem.x = p.x + 1;
            tem.y = p.y - 2;
            tem.step = p.step + 1;
            mark[tem.x][tem.y] = true;
            if(tem.x == e.x && tem.y == e.y)
            {
                cout<<tem.step<<endl;
                return true;
            }
            else 
                coll.push(tem);
        }
    }
    //////////////////////
    if(p.y + 2 < num)
    {
        if(p.x - 1 >= 0 && !mark[p.x - 1][p.y + 2])
        {
            tem.x = p.x - 1;
            tem.y = p.y + 2;
            tem.step = p.step + 1;
            mark[tem.x][tem.y] = true;
            if(tem.x == e.x && tem.y == e.y)
            {
                cout<<tem.step<<endl;
                return true;
            }
            else
                coll.push(tem);
        }
        if(p.x + 1 < num && !mark[p.x + 1][p.y + 2])
        {
            tem.x = p.x + 1;
            tem.y = p.y + 2;
            tem.step = p.step + 1;
            mark[tem.x][tem.y] = true;
            if(tem.x == e.x && tem.y == e.y)
            {
                cout<<tem.step<<endl;
                return true;
            }
            else 
                coll.push(tem);
        }
    }
    return false;
}

关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。 

技术网站地址: vmfor.com

原文地址:https://www.cnblogs.com/gavinsp/p/4566574.html