huangxinyou @ 2023-11-02 20:40:28
思路: 循环每一格,判断是否上下左右都有为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; } / 6 1 1 1 1 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1 /
by huangxinyou @ 2023-11-02 20:41:50
求大神回复!
by heyx0201 @ 2023-11-02 20:56:01
望丰展?使MD
by A1234567890QwQ @ 2023-11-11 14:42:53
markdown版
#include<bits/stdc++.h>
using namespace std;
#define FO(i,a,b) for(int i=a;i<=b;i++)
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; }
/* 6 1 1 1 1 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1 */
by A1234567890QwQ @ 2023-11-11 14:44:37
但是看不懂,求大佬