虽然ac了,但感觉最后可以不用加1啊,为啥呢
查看原帖
虽然ac了,但感觉最后可以不用加1啊,为啥呢
885503
hhr1101楼主2023/7/11 16:41
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int x1,y1,x2,y2;
int dis[20][20];
int dx[]={1,1,2,2,-1,-1,-2,-2,2,2,-2,-2};
int dy[]={2,-2,1,-1,2,-2,1,-1,2,-2,2,-2};
struct node
{
  int x;
  int y;
};
queue<node>q;
void bfs(int m,int n)
{
    memset(dis,-1,sizeof dis);
    node now,next;
    now.x=m,now.y=n;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0;i<12;i++)
        {
            next.x=now.x+dx[i];
            next.y=now.y+dy[i];
            if(next.x<=0||next.x>20||next.y<=0||next.y>20) continue;
            if(dis[next.x][next.y]>=0) continue;
            dis[next.x][next.y]=dis[now.x][now.y]+1;
            q.push(next);
        }
    }
}
int main()
{
    cin>>x1>>y1>>x2>>y2;
    bfs(x1,y1);
    cout<<dis[1][1]+1<<endl;
    bfs(x2,y2);
    cout<<dis[1][1]+1<<endl;
    return 0;
}
2023/7/11 16:41
加载中...