【CCF CSP-20141202】Z字形扫描

【CCF CSP-20141202】Z字形扫描

C++代码

方法一:模拟

#include <bits/stdc++.h>
using namespace std;
using gg = long long;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    gg ni;
    cin >> ni;
    vector<vector<gg>> ai(ni, vector<gg>(ni));
    for (auto& i : ai) {
        for (auto& j : i) {
            cin >> j;
        }
    }
    vector<array<gg, 2>> d1 = {{-1, 1}, {1, -1}},  //右上、左下
        d2 = {{0, 1}, {1, 0}};  //右、下
    gg index = 0, x = 0, y = 0;  // index指示目前移动方向的索引,x、y为横纵坐标
    auto outOfBorder = [ni](gg x, gg y) {  //坐标是否越界
        return x < 0 or x >= ni or y < 0 or y >= ni;
    };
    for (gg i = 0; i < ni * ni; ++i) {  //遍历n*n个数字
        cout << ai[x][y] << " ";  //输出当前数字
        //在当前方向上已无法移动
        if (outOfBorder(x + d1[index][0], y + d1[index][1])) {
            auto d = d2[index];  //确定向右还是向下移动一格
            if (outOfBorder(x + d[0], y + d[1])) {
                d = d2[index xor 1];
            }
            x += d[0], y += d[1], index ^= 1;  //更新坐标和移动方向
        } else {  //在当前方向上可以继续移动,那就继续移动
            x += d1[index][0], y += d1[index][1];
        }
    }
    return 0;
}

方法二:扫描线

#include <bits/stdc++.h>
using namespace std;
using gg = long long;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    gg ni;
    cin >> ni;
    vector<vector<gg>> ai(ni, vector<gg>(ni));
    for (auto& i : ai) {
        for (auto& j : i) {
            cin >> j;
        }
    }
    for (gg i = 0; i < 2 * ni - 1; ++i) {  // 2n-1条扫描线
        array<gg, 2> c =  //初始位置
            i < ni ? array<gg, 2>{i, 0} : array<gg, 2>{ni - 1, i - ni + 1};
        vector<gg> v;  //存储扫描线上的所有数字
        while (c[0] >= 0 and c[0] < ni and c[1] >= 0 and c[1] < ni) {
            v.push_back(ai[c[0]][c[1]]);
            --c[0], ++c[1];
        }
        if (i % 2 == 0) {
            for (gg j : v) {
                cout << j << " ";
            }
        } else {
            for (auto j = v.rbegin(); j != v.rend(); ++j) {
                cout << *j << " ";
            }
        }
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我真的不是cjc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值