109 lines
2.3 KiB
C++
109 lines
2.3 KiB
C++
#include <iostream>
|
||
#include <algorithm>
|
||
#include <cmath>
|
||
#include <vector>
|
||
using namespace std;
|
||
vector<int> result;
|
||
|
||
int _5to10(const vector<int>& 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 <class T>
|
||
struct Node{
|
||
T element;
|
||
bool isLeaf;
|
||
Node<T>* NW;//1
|
||
Node<T>* NE;//2
|
||
Node<T>* SW;//3
|
||
Node<T>* SE;//4
|
||
vector<int> path;
|
||
|
||
Node(const T& _element) : isLeaf(true), element(_element), NW(nullptr), NE(nullptr), SW(nullptr), SE(nullptr) {}
|
||
};
|
||
|
||
//这里不需要Tree,可以直接用递归做,更方便
|
||
//存对角线的(i, j)
|
||
template <class T>
|
||
void dfs(vector<vector<int>>& mat, Node<T>*& node, vector<int>& 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<T>(val);
|
||
node->path = path;
|
||
|
||
//转换10进制
|
||
result.push_back(_5to10(node->path));
|
||
return;
|
||
}
|
||
|
||
node = new Node<T>(-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<vector<int>> matrix(n, vector<int>(m));
|
||
for(int i = 0; i < n; i++)
|
||
for(int j = 0; j < m; j++)
|
||
cin >> matrix[i][j];
|
||
|
||
Node<int>* root = nullptr;
|
||
vector<int> 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
|
||
|
||
*/
|