求助,只得了20分
查看原帖
求助,只得了20分
542974
William_qwq楼主2021/10/9 21:17

请各位大神拔刀相助

#include<bits/stdc++.h>
using namespace std;
int n,m,a[410][410],vis[410][410];
int dx[]={-2,-1,1,2,-2,-1,1,2};
int dy[]={1,2,-2,-1,-1,-2,2,1};
struct gg
{
	int x,y,dist;
};
bool in(int x,int y)
{
	return x>=1&&x<=n&&y>=1&&y<=m;
}
void bfs(int x,int y)
{
	int i;
	a[x][y]=0;
	vis[x][y]=1;
	gg s;
    s.x=x; s.y=y; s.dist=0;
    queue<gg> q;
    q.push(s);
    while(!q.empty())
    {
    	gg pos=q.front();
    	a[pos.x][pos.y]=pos.dist;
    	q.pop();
    	for(i=0;i<8;i++)
    	{
    		gg npos;
    		npos.x=pos.x+dx[i];
    		npos.y=pos.y+dy[i];
    		npos.dist=pos.dist+1;
    		if(!in(npos.x,npos.y)||vis[npos.x][npos.y]) continue;
    		vis[npos.x][npos.y]=1;
    		q.push(npos);
    		
		}
	}
	
}
int main()
{
    int x,y,i,j;
    cin>>n>>m>>x>>y;
    memset(a,-1,sizeof(a));
    bfs(x,y);
    for(i=1;i<=n;i++)
    {
    	for(j=1;j<=m;j++)
    	{
    		cout<<a[i][j]<<"    ";
		}
		cout<<endl;
	}
	return 0;
}

2021/10/9 21:17
加载中...