huangxinyou @ 2023-11-02 19:15:36
思路: 循环每一格,判断是否上下左右都有为1的格子,如果满足,则变为2```
using namespace std;
int n,a[31][31],nx[31],nn[31],sx[31],sn[31]; bool ok(int a,int b){return a>=sn[b]&&a<=sx[b]&&b>=nn[a]&&b<=nx[a];} int main(){ memset(nn,0x3f,sizeof(nn)); memset(sn,0x3f,sizeof(sn)); cin>>n; FO(i,1,n){ FO(j,1,n){ cin>>a[i][j]; if(a[i][j]==0)continue; nx[i]=max(nx[i],j); sx[j]=max(sx[j],i); nn[i]=min(nn[i],j); sn[j]=min(sn[j],i); } } // FO(i,1,n){ // cout<<nn[i]<<" "<<nx[i]<<"\n"; // } // cout<<"\n"; // FO(i,1,n){ // cout<<sn[i]<<" "<<sx[i]<<"\n"; // } FO(i,1,n){ FO(j,1,n){ if(a[i][j]==0&&ok(i,j)==1)a[i][j]=2; cout<<a[i][j]<<" "; } cout<<"\n"; } return 0; }
by huangxinyou @ 2023-11-02 20:41:24
求大神回复!
by ____TAT____ @ 2023-11-07 17:12:23
#include <bits/stdc++.h>
using namespace std;
int maze[32][32];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n;
struct Node{
int x;
int y;
Node(int xx,int yy){
x=xx,y=yy;
}
};
void bfs(Node f){
queue<Node> q;
q.push(f);
while(!q.empty()){
Node fir=q.front();
q.pop();
for(int i=0;i<4;i++){
int dx=fir.x+dir[i][0];
int dy=fir.y+dir[i][1];
if(dx<=n+1&&dy<=n+1&&dx>=0&&dy>=0&&maze[dx][dy]==2){
maze[dx][dy]=0;
q.push(Node(dx,dy));
}
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>maze[i][j];
}
}
for(int i=0;i<=n+1;i++){
for(int j=0;j<=n+1;j++){
if(maze[i][j]==0) maze[i][j]=2;
}
}
bfs(Node(0,0));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<maze[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}