一道很裸的搜索题 我们可以一开始把所有的零赋值成2 然后从四条边界往里搜 所有与边界相邻的2(能搜到的2)都赋值成0即可 最后输出整个矩阵
代码如下
#include<iostream>
#include<cstdio>
#include<cctype>
using namespace std;
#define in = read();
typedef long long ll;
typedef unsigned int ui;
const ll size = 50 + 1;
int n;
int num;
int map[size][size];
bool judge[size][size];
int h[5] = {0 , 0 , 0 , 1 , -1} , s[5] = {0 , 1 , -1 , 0 , 0};
inline ll read(){
ll num = 0 , f = 1; char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();
}
while(isdigit(ch)){
num = num*10 + ch - '0';
ch = getchar();
}
return num*f;
}
void dfs(int x , int y){
for(register int i=1;i<=4;i++)
if(x + h[i] > 0 && y + s[i] > 0 && x + h[i] <= n && y + s[i] <= n && map[x + h[i]][y + s[i]] == 2){
map[x + h[i]][y + s[i]] = 0;
dfs(x + h[i] , y + s[i]);
}
}
int main(){
n in;
for(register int i=1;i<=n;i++)
for(register int j=1;j<=n;j++){
num in;
if(num == 1) map[i][j] = 1;
if(num == 0) map[i][j] = 2;
}
for(register int i=1;i<=n;i++){
if(map[i][1] == 2){
map[i][1] = 0;
dfs(i , 1);
}
if(map[i][n] == 2){
map[i][n] = 0;
dfs(i , n);
}
if(map[1][i] == 2){
map[1][i] = 0;
dfs(1 , i);
}
if(map[n][i] == 2){
map[n][i] = 0;
dfs(n , i);
}
}
for(register int i=1;i<=n;i++){
for(register int j=1;j<=n;j++)
printf("%d " , map[i][j]);
printf("\n");
}
return 0;
}
//COYG