有人帮我看看代码吗?
查看原帖
有人帮我看看代码吗?
1269423
huge_wave楼主2024/6/16 08:54
#include<bits/stdc++.h>
using namespace std;
const int N=410;
typedef long long ll;
struct noid{
	int x,y,step;
};
int n,m,x,y;
int cc[N][N];
int dx[]={2,1,-1,-2,-2,-1, 1, 2};
int dy[]={1,2, 2, 1,-1,-2,-2,-1};
void bfs(int ii,int jj){
	queue<noid> q;
	bool v[N][N];
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			v[i][j]=false;
		}
	} 
	q.push({x,y,0});
	while(!q.empty()){
		noid p=q.front();
		if(p.x==ii&&p.y==jj){
			cc[ii][jj]=p.step;
			return;
		}
		q.pop();
		for(int i=0;i<8;i++){
			int xx=p.x+dx[i];
			int yy=p.y+dy[i];
			if(xx<=n&&xx>=1&&y<=m&&y>=1&&!v[xx][yy]){
				q.push({xx,yy,p.step+1});
				v[xx][yy]=true;
			}
		}
	}
	cc[ii][jj]=-1;
}
int main(){
	/*ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);*/
	
	scanf("%d%d%d%d",&n,&m,&x,&y);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(i==x&&j==y){
				cc[i][j]=0;
			}
			bfs(i,j);
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			printf("%3d ",cc[i][j]);
		}
		printf("\n");
	} 
	return 0;
}
2024/6/16 08:54
加载中...