Kaaras @ 2024-01-27 08:56:51
#include<iostream>
using namespace std;
long long n,num[100][100],newcolor[100][100];
int main(){
cin>>n;
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
cin>>num[i][j];
newcolor[i][j] = num[i][j];
}
}
for(int i = 0;i <= n;i++){
for(int j = 0;j < n;j++){
if(num[i][j]) break;
else num[i][j] = 1;
}
for(int j = n - 1;j >= 0;j--){
if(num[i][j]) break;
else num[i][j] = 1;
}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
if(!num[i][j]) newcolor[i][j] = 2;
}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
cout<<newcolor[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
by Youth_Glory @ 2024-01-27 09:18:51
你这个搜索方法会有遗漏,比如下面这个形状:
00000000
00111000
00101111
00100001
11100001
10000001
10111001
11101111
如果按照你的方法来做,最下面一排中间的那个0也会变成2
by yangjunrui @ 2024-01-27 09:39:43
可以用深搜做
by qx_wangziyan @ 2024-01-27 10:05:55
试试这个:
#include <bits/stdc++.h>
using namespace std;
struct st{
int x,y;
};
int a[35][35];
int n;
int dx[8]= {1,-1, 0, 0};
int dy[8]= {0,0,1,-1};
int fun(int x2,int y2)
{
int x3,y3;
if (x2<0||y2<0||x2>n+1||y2>n+1) return 0;
if (a[x2][y2] !=0) return 0;
a[x2][y2]=3;
for (int i=0;i<4;i++)
{
x3=x2+dx[i];y3=y2+dy[i];
if (a[x3][y3] ==0)
{
fun(x3,y3);
}
}
}
int main()
{
st b0;
cin>>n;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
fun(0,0);
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 Kaaras @ 2024-01-29 10:19:31
全部re
by mouyulin @ 2024-02-01 09:36:36
#include<bits/stdc++.h>
using namespace std;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,1,-1};
int n,a[10086][10086],b[10086][10086];
void dfs(int x,int y){
if(x<1 || x>n || y<1 || y>n || a[x][y]!=0) return;
a[x][y]=1;
for(int i=0;i<4;i++)
dfs(x+dx[i],y+dy[i]);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&a[i][j]);
if(a[i][j]==1) b[i][j]=-1;
}
}
for(int i=1;i<=n;i++){
if(a[i][1]!=1) dfs(i,1);
if(a[i][n]!=1) dfs(i,n);
}
for(int i=1;i<=n;i++){
if(a[1][i]!=1) dfs(1,i);
if(a[n][i]!=1) dfs(n,i);
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(b[i][j]==-1) printf("1 ");
else if(a[i][j]==0) printf("2 ");
else printf("0 ");
}
printf("\n");
}
return 0;
}
by Kaaras @ 2024-02-02 09:11:53
感谢大佬
by nannangua @ 2024-06-19 16:44:11
@qx_wangziyan 你这个用void函数,有时候没有返回值也会RE