其他分享
首页 > 其他分享> > P2615 [NOIP2015 提高组] 神奇的幻方

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

标签:NOIP2015,vector,int,res,幻方,P2615,Limit
来源: https://www.cnblogs.com/pannnn/p/15851220.html