zhenghuangzhe @ 2024-08-09 12:00:48
#include<bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, -1, 0, 0};
int dy[] = {0, 0, 0, 1, -1};
int n, a[31][31], used[31][31];
void dfs(int vx, int vy)
{
if(vx < 1 || vx > n || vy < 1 || vy > n || used[vx][vy] == 1 || a[vx][vy] == 1) return ;
used[vx][vy] = 1;
for(int i = 1; i <= 4; i++) dfs(vx + dx[i], vy + dy[i]);
return ;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
scanf("%d", &a[i][j]);
}
}
dfs(1, 1);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(used[i][j] == 0 && a[i][j] == 0) printf("2 ");
else if(a[i][j] == 1) printf("1 ");
else printf("0 ");
}
printf("\n");
}
return 0;
}
像是遇到了特殊情况,想不出来,卡在32分。
by zhenghuangzhe @ 2024-08-09 12:01:24
样例能过
by haimingbei @ 2024-08-09 12:08:59
@zhenghuangzhe 看看吧
/*
1、P1162 填涂颜色
*/
#include<bits/stdc++.h>
using namespace std;
int a[40][40],n,b[40][40];
int xp[5]={0,1,0,-1,0};
int yp[5]={0,0,1,0,-1};
void dfs(int x,int y){
if(x<0 || x>n+1 || y<0 || y>n+1 || b[x][y]!=0)return ;
b[x][y]=1;
for(int i=1;i<=4;i++)dfs(x+xp[i],y+yp[i]);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
if(a[i][j]==0)b[i][j]=0;
else b[i][j]=2;
}
}
dfs(0,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(b[i][j]==0)cout<<2<<" ";
else cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
by akksc04 @ 2024-08-09 22:06:24
@zhenghuangzhe
输出想复杂了 如果used[i][j]没被标记过,输出2 否则,输出a[i][j]; 在这之前的在输入时加上
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
if (a[i][j]==0){
used[i][j]=0;
}else{
used[i][j]=2;
}
```cpp
望观(逃