P2615 [NOIP2015 提高组] 神奇的幻方

// Problem: P2615 [NOIP2015 提高组] 神奇的幻方
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2615
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// User: Pannnn

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> res(n + 1, vector<int>(n + 1));
    
    res[1][(n + 1) / 2] = 1;
    int prex = 1;
    int prey = (n + 1) / 2;
    for (int i = 2; i <= n * n; ++i) {
        int x, y;
        if (prex == 1 && prey != n) {
            x = n, y = prey + 1;
        } else if (prex != 1 && prey == n) {
            x = prex - 1, y = 1;
        } else if (prex == 1 && prey == n) {
            x = prex + 1, y = prey;
        } else if (res[prex - 1][prey + 1] == 0) {
            x = prex - 1, y = prey + 1;
        } else {
            x = prex + 1, y = prey;
        }
        res[x][y] = i;
        prex = x;
        prey = y;
    }
    
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            cout << res[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}
上一篇:Mybatis分页查询limit


下一篇:FastAPI-5-查询参数