//201412-2
#include <bits/stdc++.h>
using namespace std;
struct Node {
int row, col;
} node[4] = /*东,,南,西南,东北*/{{0, 1},
{1, -1},
{1, 0},
{-1, 1}};
int main() {
int n;
scanf("%d", &n);
int arr[n][n];
//录入数据
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &arr[i][j]);
//0东,1西南,2南,3东北
int row = 0, col = 0;
//打印起点
printf("%d", arr[row][col]);
int nowDir = 0;
//记录剩余需要打印的数据
int count = n * n - 1;
//打印完全部数据
while (count) {
row += node[nowDir].row;
col += node[nowDir].col;
if (row >= 0 && row < n && col >= 0 && col < n) {
printf(" %d", arr[row][col]);
count--;
}
if (nowDir == 0 && row == 0)
nowDir = 1;
else if (nowDir == 0 && row == n - 1)
nowDir = 3;
else if (nowDir == 1 && col == 0)
nowDir = 2;
else if (nowDir == 1 && row == n - 1)
nowDir = 0;
else if (nowDir == 2 && col == 0)
nowDir = 3;
else if (nowDir == 2 && col == n - 1)
nowDir = 1;
else if (nowDir == 3 && row == 0)
nowDir = 0;
else if (nowDir == 3 && col == n - 1)
nowDir = 2;
}
return 0;
}
201412-2
最新推荐文章于 2024-03-05 11:15:16 发布