1 条题解

  • 0
    @ 2024-9-22 16:48:48

    模拟

    根据题意,每次左旋90°,那么按照顺序就是下右上左,依次循环执行直到取完全部数据即可。同时使用标记数组,对已经取过的数据进行标记,以防止重复取数。

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 1e3 + 10, INF = 0x3f3f3f3f, MOD = 1E9 + 7;
    int n, m, a[N][N], p[N];
    bool st[N][N];
    
    int main(int argc, char* argv[]) {
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                cin >> a[i][j];
    
        int x = 0, y = 1, cnt = 0, sum = n * m;
        while (cnt < sum) {
            while (x < n && !st[x + 1][y])
                p[++cnt] = a[++x][y], st[x][y] = 1;// down
            while (y < m && !st[x][y + 1])
                p[++cnt] = a[x][++y], st[x][y] = 1;// right
            while (x > 1 && !st[x - 1][y])
                p[++cnt] = a[--x][y], st[x][y] = 1;// up
            while (y > 1 && !st[x][y - 1])
                p[++cnt] = a[x][--y], st[x][y] = 1;// left
        }
        for (int i = 1; i <= sum; i++)
            cout << p[i] << " ";
        return 0;
    }
    
    • 1

    信息

    ID
    148
    时间
    1000ms
    内存
    512MiB
    难度
    5
    标签
    递交数
    260
    已通过
    98
    上传者