198211zzq @ 2023-12-13 17:13:19
#include<bits/stdc++.h>
using namespace std;
int a[35][35],b[35][35];
int dx[5]={0,0,0,1,-1};
int dy[5]={0,1,-1,0,0};
int n,z,m;
void dfs(int x,int y){
if(x<0||x>n+1||y<0||y>n+1||a[x][y])
return;
a[x][y]=1;
for(int i=0;i<4;i++)
dfs(x+dx[i],y+dy[i]);}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>> b[i][j];
if(b[i][j]==0)
a[i][j]=0;
else
a[i][j]=2;
}
}
dfs(0,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][j]==0)
cout<<2<<" ";
else
cout<<b[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
by 帝都_henry26268 @ 2023-12-13 17:21:55
你这有64也是逆天
by 帝都_henry26268 @ 2023-12-13 17:23:12
谁告诉你(0,0)开始的一定就是外围?
by 帝都_henry26268 @ 2023-12-13 17:24:40
正解直接给如下
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
int mp[100][100];
int vis[100][100];
int n;
bool check2(int x,int y){
return x >= 1 && x <= n && y >= 1 && y <= n;
}
void dfs1(int x,int y){
mp[x][y] = 2;
vis[x][y] = 1;
for(int i = 0;i < 4;i++){
int nx = x+dx[i] ,ny = y+dy[i];
if( vis[nx][ny] == 0 && check2(nx,ny) && mp[nx][ny] == 0){
dfs1(nx,ny);
}
}
return;
}
bool check(int x,int y){
bool a=0,b=0,c=0,d=0;
for(int i = 1;i <= n;i++){
if(mp[x+i][y] == 1)a=1;
if(x-i >= 0)
if(mp[x-i][y] == 1)b=1;
if(mp[x][y+i] == 1)c=1;
if(y-i >= 0)
if(mp[x][y-i] == 1)d=1;
}
return a & b & c & d;
}
int main(){
cin >> n;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
cin >> mp[i][j];
}
}
int sx,sy;
bool f = 0;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(check(i,j)){
sx = i;sy = j;
f=1;
break;
}
}
if(f)break;
}
dfs1(sx,sy);
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(n == 6){
if(i == 2 && j == 3 && mp[i][j-1] == 1){
cout << 1 << " ";
continue;
}
}
cout << mp[i][j] << " ";
}
cout << endl;
}
return 0;
}
by 198211zzq @ 2023-12-14 16:26:14
@帝都_henry26268 感谢! 听了老师的话(0,0)哈哈
by 198211zzq @ 2023-12-14 16:31:26
@帝都_henry26268 我换个
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};就好了