tengyuxuan @ 2024-12-23 20:33:25
0pts:
#include <bits/stdc++.h>
using namespace std;
struct node{
int x,y;
};
int fx[]={1,0,-1,0};
int fy[]={0,1,0,-1};
queue<node> q;
int n;
int a[32][32];
void bfs()
{
q.push({0,0});
while(!q.empty())
{
node fir=q.front();
for(int i=0;i<4;i++)
{
int dx=fir.x+fx[i];
int dy=fir.y+fy[i];
if(dx>=0&&dx<=n+1&&dy>=0&&dy<=n+1&&a[dx][dy]==0)
{
q.push({dx,dy});
a[dx][dy]=3;
}
}
q.pop();
}
}
int main()
{
ios::sync_with_stdio(0);
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
bfs();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]==3)
{
cout<<"0 ";
}else if(a[i][j]==0){
cout<<"2 ";
}else{
cout<<"1 ";
}
}
cout<<endl;
}
return 0;
}
100pts:
#include <bits/stdc++.h>
using namespace std;
struct node{
int x,y;
};
int fx[]={1,0,-1,0};
int fy[]={0,1,0,-1};
queue<node> q;
int n;
int a[32][32];
void bfs()
{
q.push({0,0});
while(!q.empty())
{
node fir=q.front();
for(int i=0;i<4;i++)
{
int dx=fir.x+fx[i];
int dy=fir.y+fy[i];
if(dx>=0&&dx<=n+1&&dy>=0&&dy<=n+1)
{
if(a[dx][dy]==0)
{
q.push({dx,dy});
a[dx][dy]=3;
}
}
}
q.pop();
}
}
int main()
{
ios::sync_with_stdio(0);
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
bfs();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]==3)
{
cout<<"0 ";
}else if(a[i][j]==0){
cout<<"2 ";
}else{
cout<<"1 ";
}
}
cout<<endl;
}
return 0;
}
by tengyuxuan @ 2024-12-23 20:49:36
0分时死活都不知道哪错了现在也不知道