@FIVE1024
  • 板块学术版
  • 楼主kk0808
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/12/1 11:32
  • 上次更新2024/12/1 14:12:06
查看原帖
@FIVE1024
1477111
kk0808楼主2024/12/1 11:32
#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct node{
	ll x,y,step;
};
ll n,sx,sy,ex,ey;
bool vis[1005][1005];
ll dx[8] = {-1,1,-2,2,-2,2,-1,1};
ll dy[8] = {-2,-2,-1,-1,1,1,2,2};
void bfs(){
	queue<node> q;
	q.push({sx,sy,0});
	vis[sx][sy] = 1;
	while(!q.empty()){
		node now = q.front();
		q.pop();
		if(now.x == ex && now.y == ey){
			cout << now.step;
			return ;
		}
		for(ll i = 0;i <= 7;i++){
			ll xx = now.x + dx[i];
			ll yy = now.y + dy[i];
			if(xx >= 0 && xx < n && yy >= 0 && yy < n && !vis[xx][yy]){
				vis[xx][yy] = 1;
				q.push({xx,yy,now.step + 1});
			}
		}
	}
}  
int main(){
	cin >> n;
	cin >> sx >> sy >> ex >> ey;
	bfs();
	return 0;
}

2024/12/1 11:32
加载中...