yr是yr @ 2024-01-09 15:47:14
#include <stdio.h>
int n;
int map[35][35];
void ranse()
{
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(map[i][j] == 0)
{
map[i][j] = 2;
}
else if(map[i][j] == -1)
{
map[i][j] = 0;
}
printf("%d ", map[i][j]);
}
printf("\n");
}
}
void solu(int x, int y)
{
if(y > n)
{
solu(x + 1, 1);
return;
}
else if(x > n)
{
return;
}
if(map[x][y] == 1)
{
solu(x, y + 1);
return;
}
else if(map[x][y] == -1)
{
solu(x, y + 1);
return;
}
else if(map[x + 1][y] == -1 || map[x - 1][y] == -1 || map[x][y + 1] == -1 || map[x][y - 1] == -1)
{
map[x][y] = -1;
solu(x, y + 1);
return;
}
else
{
solu(x, y + 1);
return;
}
return;
}
int main()
{
scanf("%d", &n);
for(int i = 0;i <= 32;i++)
{
for(int j = 0;j <= 32;j++)
{
map[i][j] = -1;
}
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
scanf("%d", &map[i][j]);
}
}
solu(1, 1);
ranse();
//思路大概是从边界检查,-1向内渗透,遇到0将0转化为-1,直到没有符合条件的0为止,然后将剩下的0转为2,将染成的-1变回0输出
return 0;
}
by mooktian @ 2024-01-23 10:57:56
@yr是yr 自己去看题解啊。