1 条题解

  • 1
    @ 2025-3-30 19:33:09

    偏移量

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int N = 20;
    
    int Map[N][N], n;
    int dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1}, idx;
    
    int main()
    {
        cin >> n;
        
        int x = 0, y = n - 1;
        int nex = x, ney = y;
        for (int i = 1; i <= n * n; i ++) {
            Map[x][y] = i;
            
            nex = x + dx[idx];
            ney = y + dy[idx];
            
            if (nex >= n || ney >= n || nex < 0 || ney < 0 || Map[nex][ney]) {
                idx = (idx + 1) % 4;
                nex = x + dx[idx];
                ney = y + dy[idx];
            }
            
            x = nex, y = ney;
            
            // cout << x << ' ' << y << endl;
        }
        
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < n; j ++) {
                cout << Map[i][j] << ' ';
            }
            cout << endl;
        }
        
        return 0;
    }
    
    • 1

    信息

    ID
    230
    时间
    1000ms
    内存
    64MiB
    难度
    10
    标签
    递交数
    4
    已通过
    1
    上传者