求调
查看原帖
求调
1098781
封禁用户楼主2024/1/21 11:39

rt,思路没问题啊?

#include<iostream>
#include<iomanip>
#include<queue>
using namespace std;

int n,m,x,y,vis[400+1][400+1]={-1};
const int dx[8+1]={0,-2,-2,-1,-1,2,2,1,1};
const int dy[8+1]={0,1,-1,2,-2,1,-1,2,-2};
struct point_and_step{
	int px,py,number;
};
queue<point_and_step>Q;

bool check(int fx,int fy){
	if(fx>=1&&fx<=n&&fy>=1&&fy<=m&&vis[fx][fy]==-1)
		return 1;
	return 0;
}
void bfs(int i,int j){
	vis[i][j]=0;
	Q.push({i,j,0});
	while(!Q.empty()){
		point_and_step Front=Q.front();
		Q.pop();
		for(int i=1;i<=8;i++){
			int fx=Front.px+dx[i];
			int fy=Front.py+dy[i];
			if(check(fx,fy)){
				vis[fx][fy]=Front.number+1;
				Q.push({fx,fy,vis[fx][fy]});
			}
		}
	}
}
int main(){
	cin>>n>>m>>x>>y;
	bfs(x,y);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++)
			cout<<left<<setw(5)<<vis[x][y];
		cout<<'\n';
	}
	return 0;
}
2024/1/21 11:39
加载中...