Files
python/TangDou/XiTi/SnakeFill.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

55 lines
1.4 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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