POJ1915,双向宽度优先搜索

第一道双先宽度优先搜索,过了很开心~

/*************************************************************************
    > File Name: main.cpp
    > Author:Eagles 
    > Mail:None 
    > Created Time: 2018年08月30日 星期四 10时07分17秒
    > Description:POJ1915,双向宽度优先搜索
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
#define N 301
int n;
int ans;

bool vis[2][N][N];//标记是否到达该点

int steps[2][N][N];//记录到达每一个坐标所需的步数

struct node
{
    int x,y;
    int step;
    node()
    {
        x=y=step=0;
    }
}star,en;

int dir[8][2]={{-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}};


bool check(int x, int y)
{
    return (0<=x&&x<n&&0<=y&&y<n);
}


bool  bfs()
{
    memset(vis,false,sizeof(vis));

    queue<node>Q[2];

    if (star.x==en.x&&star.y==en.y)
    {
        ans=0;
        return true;
    }

    Q[0].push(star);
    Q[1].push(en);

    vis[0][star.x][star.y]=true;
    vis[1][en.x][en.y]=true;
    steps[0][star.x][star.y]=0;
    steps[1][en.x][en.y]=0;

    node cur,nex;

    int inx=0;

    while (!Q[0].empty()||!Q[1].empty())
    {
        inx=(inx^1);//每一次换一个队列进行扩展,亦可写作inx=(inx+1)%2;

        int the_size=Q[inx].size();

        for (int i=0; i<the_size; i++)
        {
            cur=Q[inx].front();
            Q[inx].pop();

            for (int j=0; j<8; j++)
            {
                nex.x=cur.x+dir[j][0];
                nex.y=cur.y+dir[j][1];
                nex.step=cur.step+1;

                if (check(nex.x,nex.y))
                {
                    if (vis[inx^1][nex.x][nex.y])//如果已经相遇
                    {
                        ans=nex.step+steps[inx^1][nex.x][nex.y];
                        return true;
                    }

                    if (!vis[inx][nex.x][nex.y])
                    {

                        vis[inx][nex.x][nex.y]=true;
                        steps[inx][nex.x][nex.y]=nex.step;

                        Q[inx].push(nex);
                    }
                }
            }    


        }
    }
return false;
}


int main()
{

   // freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int t;

    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d",&n);
            scanf("%d %d",&star.x,&star.y);
            scanf("%d %d",&en.x,&en.y);

            star.step=en.step=0;

            ans=-1;

            bfs();

            printf("%d\n",ans);
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值