H - Knight Moves

点击打开链接

bfs ,没啥好说的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#define N 1005
using namespace std;

int n,l;
int vis[305][305];
int dis[8][2]={2,1,1,2,-1,2,-2,1,2,-1,1,-2,-1,-2,-2,-1};
struct node
{
    int x,y;
    int dis;
};
void bfs(int x,int y)
{
    queue<node > q;
    node start,next;
    start.dis= 0;
    start.x= x;
    start.y= y;
    q.push(start);
    while(!q.empty())
    {
        start=q.front();
        q.pop();
        for(int i=0;i<8;i++)
        {
            next.x=start.x+dis[i][0];
            next.y=start.y+dis[i][1];
            if(next.x>=0 && next.x<l&& next.y>=0 && next.y<l && -1==vis[next.x][next.y])
            {
                vis[next.x][next.y]=start.dis+1;
                next.dis=start.dis+1;
                q.push(next);
            }
        }
    }
}
int main()
{
    cin>>n;
    int rx,ry,ex,ey;
    while(n--)
    {
        cin>>l;
        cin>>rx>>ry>>ex>>ey;
        memset(vis,-1,sizeof(vis));
        vis[rx][ry]=0;
        bfs(rx,ry);
        cout<<vis[ex][ey]<<endl;
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值