题解:P5731 【深基5.习6】蛇形方阵
思路
因为大小为
- 向右遍历
while (y + 1 <= n && a[x][y + 1] == 0) {
y++;
a[x][y] = now;
now++;
}
只要当前位置不是空的,就可以填入,否则换向。
- 向下遍历
while (x + 1 <= n && a[x + 1][y] == 0) {
x++;
a[x][y] = now;
now++;
}
- 向左遍历
while (y - 1 >= 1 && a[x][y - 1] == 0) {
y--;
a[x][y] = now;
now++;
}
- 向上遍历
while (x - 1 >= 1 && a[x - 1][y] == 0) {
x--;
a[x][y] = now;
now++;
}
最后输出,注意右对齐 printf("%3d", a[i][j]); 实现。
代码
#include<iostream>
#include<cstdio>
using namespace std;
int a[20][20];
int main()
{
int n;
int now = 2, x = 1, y = 1;
a[1][1] = 1;
cin >> n;
while (now <= n * n) {
while (y + 1 <= n && a[x][y + 1] == 0) {
y++;
a[x][y] = now;
now++;
}
while (x + 1 <= n && a[x + 1][y] == 0) {
x++;
a[x][y] = now;
now++;
}
while (y - 1 >= 1 && a[x][y - 1] == 0) {
y--;
a[x][y] = now;
now++;
}
while (x - 1 >= 1 && a[x - 1][y] == 0) {
x--;
a[x][y] = now;
now++;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%3d", a[i][j]);
}
cout << endl;
}
return 0;
}