• 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

  • 相关阅读:
    经管-7
    均衡价格和均衡产量以及偏分求导
    点弹性系数计算
    洛谷-P5703 【深基2.例5】苹果采购
    洛谷-P1616 疯狂的采药
    洛谷-P1049 装箱问题
    洛谷-P1048 采药
    洛谷-P1064 金明的预算方案
    操作系统启动
    mybatis中使用注解查询和使用xml配置文件查询相互对应关系
  • 原文地址:https://www.cnblogs.com/gavinsp/p/4566574.html
Copyright © 2020-2023  润新知