发的地方
2024-08-08 14:27:43
发布于:广东
#include <bits/stdc++.h>
using namespace std;
int mp[110][110],n;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int vis[110][110];
struct node{
int x,y,val;
};
queue<node>q;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>mp[i][j];
}
}
node start={1,1,mp[1][1]};
vis[1][1]=1;
q.push(start);
while(!q.empty()){
node head =q.front();
q.pop();
cout<<head.val<<" ";
for(int i=0;i<4;i++){
int xx=head.x+dir[i][0];
int yy=head.y+dir[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&vis[xx][yy]==0){
node canGo={xx,yy,mp[xx][yy]};
vis[xx][yy]=1;
q.push(canGo);
}
}
}
return 0;
}
这里空空如也
有帮助,赞一个