- js24012 的博客
O1477 【入门】快乐的马里奥
- @ 2026-3-15 16:06:21
O1477 【入门】快乐的马里奥
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
int dx[5]={0,0,0,1,-1};
int dy[5]={0,1,-1,0,0};
bool vis[101][101];
int a[101][101];
queue<pair<int,int>>q;
int cnt=0;
bool check(int X,int Y){
if(vis[X][Y] == 1)return 0;
if(X>n||Y>m||X<1||Y<1)return 0;
return 1;
}
void bfs(int x,int y){
q.push({x,y});
vis[x][y]=1;
cnt++;
a[x][y]=cnt;
while(!q.empty()){
pair<int,int>t=q.front();
q.pop();
int tx=t.first,ty=t.second;
for(int i = 1;i <= 4;i++){
int X=tx+dx[i];
int Y=ty+dy[i];
if(check(X,Y)){
q.push({X,Y});
vis[X][Y]=1;
cnt++;
a[X][Y] = cnt;
}
}
}
}
signed main(){
cin >> n >> m;
bfs(1,1);
for(int i=1;i <= n;i++){
for(int j = 1;j <= m;j++){
cout << a[i][j]<<" ";
}
cout << endl;
}
return 0;
}