You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.4 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
// C++中,在函数(包括main函数)中定义超大数组内存为栈所分配的最大空间为4M
// 因此在子函数或者main函数中定义超大数组的方式是万万行不通的。
// 解决方式:为超大数组创建为一个全局数组。
// 为什么数组在全局变量不容易炸?
// https://www.zhihu.com/question/297001880
int a[10000][10000] = {0};
int main() {
//列数
int w = 6;
//从m到n
int m = 8, n = 2;
//行数
int row = (max(m, n) % w == 0) ? max(m, n) / w : max(m, n) / w + 1;
//开始排列
for (int i = 0; i < row; ++i) {
//奇数行024...
if (i % 2 == 0) {
for (int j = 0; j < w; ++j) {
a[i][j] = i * w + j + 1;
}
}//偶数行
else {
for (int j = w - 1; j >= 0; j--) {
a[i][j] = i * w + (w - j);
}
}
}
//找到m和n的矩阵坐标
int x1, y1, x2, y2;
//输出结果
for (int i = 0; i < row; ++i) {
for (int j = 0; j < w; ++j) {
cout << setw(4) << a[i][j] << " ";
if (a[i][j] == m) {
x1 = i, y1 = j;
}
if (a[i][j] == n) {
x2 = i, y2 = j;
}
}
cout << endl;
}
cout << abs(x1 - x2) + abs(y1 - y2) << endl;
return 0;
}