30分,求好心大佬调!!
查看原帖
30分,求好心大佬调!!
776751
W0718楼主2024/7/23 13:36
#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int maxn=10000+111;
int x2,y2,x3,y3;
bool vis[maxn][maxn];
int dr[8][2]={{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};
int dt[4][2]={{2,2},{2,-2},{-2,-2},{-2,2}};
int ans=0x7f7f7f;
struct node{
	int x;int y;int t;
    node(int xx,int yy,int tt)
    {
    	x=xx;y=yy;t=tt;
	}
};
queue<node> q;
void bfs(int sx,int sy)
{
	q.push(node(sx,sy,0));vis[sx][sy]=true;
	while(!q.empty())
	{
		int dx;
		int dy;
		node nd=q.front();q.pop();
		for(int i=0;i<8;i++)//走日字 
		{
			dx=nd.x+dr[i][0];
			dy=nd.y+dr[i][1];
			if(dx==1&&dy==1)
			{
				ans=min(ans,nd.t+1);
			}
			if(dx>0&&dy>0&&dx<=sx&&dy<=sy&&!vis[dx][dy])
			{
				q.push(node(dx,dy,nd.t+1));
				vis[dx][dy]=true;
			}
		}
		
		for(int i=0;i<4;i++)//走田字 
		{
			dx=nd.x+dt[i][0];
			dy=nd.y+dt[i][1];
			if(dx==1&&dy==1)
			{
				ans=min(ans,nd.t+1);
			}
			if(dx>0&&dy>0&&dx<=sx&&dy<=sy&&!vis[dx][dy])
			{
				q.push(node(dx,dy,nd.t+1));
				vis[dx][dy]=true;
			}
		}
	}
	return ;
}
signed main()
{
	scanf("%d %d %d %d",&x2,&y2,&x3,&y3);
	bfs(x2,y2);
	cout<<ans<<endl;
	
	memset(vis,false,sizeof(vis));
	ans=0x7f7f7f;//为第二批马初始化 
	
	bfs(x3,y3);
	cout<<ans;
	return 0;
}
2024/7/23 13:36
加载中...