#include #include #include #include using namespace std; vector result; int _5to10(const vector& x){ int num = 0; for(int i = x.size() - 1; i >= 0; i--){ num = num * 5 + x[i]; } return num; } //T取bool或者int[0, 1],这里考虑写一个完整的4叉树的类 template struct Node{ T element; bool isLeaf; Node* NW;//1 Node* NE;//2 Node* SW;//3 Node* SE;//4 vector path; Node(const T& _element) : isLeaf(true), element(_element), NW(nullptr), NE(nullptr), SW(nullptr), SE(nullptr) {} }; //这里不需要Tree,可以直接用递归做,更方便 //存对角线的(i, j) template void dfs(vector>& mat, Node*& node, vector& path, int li, int lj, int ri, int rj) { int val = mat[li][lj]; bool same = true; for (int i = li; i <= ri && same; i++) { for (int j = lj; j <= rj && same; j++) { if (mat[i][j] != val) same = false; } } if (same) { // 叶子 node = new Node(val); node->path = path; //转换10进制 result.push_back(_5to10(node->path)); return; } node = new Node(-1); // 内部节点 node->isLeaf = false; int midRow = (li + ri) / 2; int midCol = (lj + rj) / 2; // NW=1 path.push_back(1); dfs(mat, node->NW, path, li, lj, midRow, midCol); path.pop_back(); // NE=2 path.push_back(2); dfs(mat, node->NE, path, li, midCol+1, midRow, rj); path.pop_back(); // SW=3 path.push_back(3); dfs(mat, node->SW, path, midRow+1, lj, ri, midCol); path.pop_back(); // SE=4 path.push_back(4); dfs(mat, node->SE, path, midRow+1, midCol+1, ri, rj); path.pop_back(); } int main(){ int n, m; cin >> n >> m; vector> matrix(n, vector(m)); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) cin >> matrix[i][j]; Node* root = nullptr; vector path; dfs(matrix, root, path, 0, 0, n-1, m-1); sort(result.begin(), result.end()); for (int v : result) cout << v << " "; // TODO: 遍历叶子节点,把 path 转换为十进制数输出 return 0; } /* 8 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 1 1 1 1 1 0 0 1 1 1 1 1 1 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 0 1 7 8 9 12 14 17 18 22 23 24 38 44 63 69 88 94 113 119 */