91xrz @ 2023-04-05 22:50:59
#include <bits/stdc++.h>
using namespace std;
const int N = 40;
//typedef pair<int, int> PII;
//#define x first
//#define y second
int map1[N][N]; //迷宫
int dx[4]{ 0,-1,0,1 };
int dy[4]{ -1,0,1,0 };
int n, m;
void bfs(int x, int y)
{
map1[x][y] = 2;
for (int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(xx > 0 && xx <= n && yy > 0 && yy <= n&&map1[xx][yy]==1)
continue;
if (xx > 0 && xx <= n && yy > 0 && yy <= n && map1[xx][yy] == 0)
{
bfs(xx, yy);
}
}
return;
}
int main()
{
cin >> n ;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> map1[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (map1[i][j] == 0)//判断该点是否在闭合圈外面
{
int l1 = i - 1;
int l2 = n - i;
int r1 = j - 1;
int r2 = n - j;
int c = 0;
for (int k=1;k<=l1;k++)
if (map1[i-k][j] == 1)
{
c++;
break;
}
for (int k=1;k<=l2;k++)
if (map1[i+k][j] == 1)
{
c++;
break;
}
for (int k=1; k <=r1; k++)
if (map1[i][j-k] == 1)
{
c++;
break;
}
for (int k=1;k<=r2;k++)
if (map1[i][j+k] == 1)
{
c++;
break;
}
if (c == 4)
{
bfs(i, j);
break;
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
cout << map1[i][j] << " ";
if (j == n)cout << endl;
}
}
by LEZ2012 @ 2023-04-09 21:00:49
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int f[33][33];
int x,y,i,j,n;
bool flag[33][33];
int dx[5]={0,1,0,-1,0},dy[5]={0,0,1,0,-1};
void work(int i,int j){
if(f[i][j]==1||flag[i][j]==1||i==n+1||i==-1||j==-1||j==n+1)return;
int a;
flag[i][j]=1;
for(a=1;a<=4;a++)work(i+dx[a],j+dy[a]);
return;
}
int main(){
memset(flag,0,sizeof flag);
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%d",&f[i][j]);
if(f[i][j])flag[i][j]=1;
}
}
for(i=1;i<=n;i++){
work(i,1);
work(1,i);
work(n,i);
work(i,n);
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(flag[i][j])
printf("%d ",f[i][j]);
else
printf("2 ");
}
printf("\n");
}
return 0;
}
AC代码(求关注)