int mark = 1;
int ans = n * n+3;
while (ans--)
{
while(arr[x][y + 1] == 0 && y + 1 <= n)
{
arr[x][y] = mark;
mark++;
y++;
ans--;
}
while (arr[x + 1][y] == 0 && x + 1 <= n)
{
arr[x][y] = mark;
mark++;
x++;
ans--;
}
while(arr[x][y-1] == 0 && y- 1 >= 1)
{
arr[x][y] = mark;
mark++;
y--;
ans--;
}
while(arr[x-1][y] == 0&& x-1>=0)
{
arr[x][y] = mark;
mark++;
x--;
ans--;
}
if (arr[x - 1][y] != 0 && arr[x][y - 1]!= 0 && arr[x + 1][y] != 0 && arr[x][y + 1] != 0)
{
arr[x][y] = mark;
break;
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)printf("%3d", arr[i][j]);
printf("\n");
}
return 0;
}
1.29的
最新推荐文章于 2024-07-15 22:10:26 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)