fghjk
2025-07-30 19:07:48
发布于:广东
#include <bits/stdc++.h>
using namespace std;
struct node{
int x,y,step;
};
queue<node>q;
int mp[110][110],xx,yy;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
bool vis[110][110];
int n,m;
void bfs(int xx,int yy){
q.push({xx,yy,0});
vis[xx][yy]=true;
while(!q.empty()){
node t= q.front();
q.pop();
cout<<mp[t.x][t.y]<<" ";
for(int i=0;i<4;i++){
int nx=t.x+dx[i];
int ny=t.y+dy[i];
if(1<=nx && nx<=n && 1<=ny && ny<=m&&vis[nx][ny]==false){
q.push({nx,ny,t.step+1});
vis[nx][ny]=true;
}
}
}
}
int main(){
freopen("spring.in", "r", stdin);
freopen("spring.out","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>mp[i][j];
}
}
cin>>xx>>yy;
bfs(xx,yy);
fclose(stdin);
fclose(stdout);
return 0;
}
这里空空如也
有帮助,赞一个