i'm making an algorithm called Quadtree, so i've tried this code that i made but the output is incorrect
input
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
output
0
the code i've tried
#include <iostream>
#include <string>
using namespace std;
int m[128][128];
int nJawaban;
string jawaban[128*128];
bool homogen(int r, int c, int k) {
int val = m[r][c];
for (int i = r; i < r+k; i++) {
for (int j = c; j < c+k; j++) {
if (m[i][j] != val) {
return false;
}
}
}
return true;
}
// i think it's here
void quadtree(int r, int c, int k, string s){
if (m[r][c] == m[r-1][c-1]) {
if (m[r][c] == 1) {
jawaban[nJawaban] = "1" + s;
nJawaban++;
}
} else {
quadtree(r, c, k/2, s+"0");
quadtree(r, c+(k/2), k/2, s+"1");
quadtree(r, c, r+k-1, s+"0");
quadtree(r, c, r+k-1, s+"1");
}
}
int main(){
int r, c;
scanf("%d %d", &r, &c);
int maxRc = max(r, c);
int pow2 = 1;
while (pow2 < maxRc){
pow2 *= 2;
}
for (int i = 0; i < pow2; i++) {
for (int j = 0; j < pow2; j++) {
m[i][j] = 0;
}
}
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
scanf("%d", &m[i][j]);
}
}
nJawaban = 0;
quadtree(0, 0, pow2, "");
printf("%d\n", nJawaban);
for (int i = 0; i < nJawaban; i++) {
printf("%s\n", jawaban[i].c_str());
}
return 0;
}
input:
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
expected output:
11
112
113
1211
1212
1213
123
130
131
1320
1321
1322
note: some of the var name is written in indonesian language and i'm trying to make this code without (using namespace std) and also fix the code
i've watched youtube video and reading some stackoverflow answer
https://www.youtube.com/watch?v=xFcQaig5Z2A
Efficient (and well explained) implementation of a Quadtree for 2D collision detection
nevertheless i can't correct my mistake but i can point out where the mistake is. My question here is how to correct this mistake or even provide better algorithm
edit: it's still didn't work