problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
luogu::P2615类 参考

洛谷 P2615 [NOIP2015 提高组] 神奇的幻方 更多...

#include <luogu.h>

静态 Public 成员函数

static int main (istream &cin, ostream &cout)
 

详细描述

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

在文件 luogu.h413 行定义.

成员函数说明

◆ main()

int luogu::P2615::main ( istream &  cin,
ostream &  cout 
)
static

在文件 luogu.cpp1274 行定义.

1274 {
1275 int n;
1276 int square[40][40];
1277 memset(square, 0, sizeof square);
1278 auto um = unordered_map<int, pair<int, int>>();
1279 cin >> n;
1280 square[0][n / 2] = 1;
1281 um[1] = make_pair(0, n / 2);
1282 for(int k = 2; k <= n * n; k++) {
1283 auto [prev_x, prev_y] = um[k - 1];
1284 if(prev_x == 0 && prev_y != n - 1) {
1285 //若 (K−1) 在第一行但不在最后一列,则将 K 填在最后一行, (K−1) 所在列的右一列;
1286 square[n - 1][prev_y + 1] = k;
1287 um[k] = make_pair(n - 1, prev_y + 1);
1288 } else if(prev_y == n - 1 && prev_x != 0) {
1289 //若 (K−1) 在最后一列但不在第一行,则将 K 填在第一列, (K−1) 所在行的上一行;
1290 square[prev_x - 1][0] = k;
1291 um[k] = make_pair(prev_x - 1, 0);
1292 } else if(prev_x == 0 && prev_y == n - 1) {
1293 //若 (K−1) 在第一行最后一列,则将 K 填在 (K−1) 的正下方;
1294 square[1][prev_y] = k;
1295 um[k] = make_pair(1, prev_y);
1296 } else if(prev_x != 0 && prev_y != n - 1) {
1297 //若 (K−1) 既不在第一行,也不在最后一列,如果 (K−1) 的右上方还未填数,则将 K 填在 (K−1) 的右上方,否则将 K 填在 (K−1) 的正下方。
1298 if(square[prev_x - 1][prev_y + 1] == 0) {
1299 square[prev_x - 1][prev_y + 1] = k;
1300 um[k] = make_pair(prev_x - 1, prev_y + 1);
1301 } else {
1302 square[prev_x + 1][prev_y] = k;
1303 um[k] = make_pair(prev_x + 1, prev_y);
1304 }
1305 }
1306 }
1307 for(int i = 0; i < n; i++) {
1308 for(int j = 0; j < n; j++) {
1309 cout << square[i][j] << " ";
1310 }
1311 cout << endl;
1312 }
1313 return 0;
1314 }

被这些函数引用 luogu::TEST().


该类的文档由以下文件生成: