2023/7/22(2)宽搜练习马走日

ltrh / 2023-07-22 / 原文

 

#include <bits/stdc++.h>
using namespace std;
int qwq[12][2]={{1,2},{1,-2},{-1,2},{-1,-2},{-2,-1},{-2,1},{2,1},{2,-1},{2,2},{-2,-2},{2,-2},{-2,2}};
int ax,ay,bx,by;
bool mp[105][105];
struct node{
    int x,y,step;
    node(){}
    node(const int x,const int y,const int step):x(x),y(y),step(step){}
};
int bfs(int u,int v)
{
    queue <node>Q;
    Q.push(node(u,v,0));
    mp[u][v]=1;
    while(!Q.empty())
    {
        node nw=Q.front();
        Q.pop();
        for(int i=0;i<12;i++)
        {
            int x=nw.x+qwq[i][0],y=nw.y+qwq[i][1];
            if(x>0&&x<=100&&y>0&&y<=100&&!mp[x][y])
            {
                Q.push(node(x,y,nw.step+1));
                mp[x][y]=1;
                if(x==1&&y==1)return nw.step+1;
            }
        }
    }
    return -1;
}
int main()
{
    cin>>ax>>ay>>bx>>by;
    cout<<bfs(ax,ay)<<endl;
    memset(mp,0,sizeof(mp));
    cout<<bfs(bx,by)<<endl;
}