201412-2

//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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值