思路:
实现代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n, m;
int Index, count1, count2;
char str[150][150];
int MAP[150][150];
class quadtree
{
public:
int q[4];
quadtree() {}
quadtree(quadtree& a)
{
for (int i = 0; i < 4; i++)
q[i] = a.q[i];
}
bool operator==(quadtree a)
{
for (int i = 0; i < 4; i++)
if (!(q[i] == a.q[i]))return false;
return true;
}
}search[1800];
void init()
{
memset(search, 0, sizeof(search));
memset(MAP, 0, sizeof(MAP));
for (int i = 0; i < n; i++)
{
cin >> str[i];
for (int j = 0; j < m; j++)
MAP[i][j] = str[i][j] - '0';
}
count1 = count2 = 0;
Index = 2;
}
int DFS(int x, int y, int s)
{
int count2_2 = count2;
count1++;
count2++;
if (s == 1)return MAP[x][y];
quadtree*temp = new quadtree;
s /= 2;
temp->q[0] = DFS(x, y, s);
temp->q[1] = DFS(x + s, y, s);
temp->q[2] = DFS(x, y + s, s);
temp->q[3] = DFS(x + s, y + s, s);
int flag = temp->q[0];
for (int i = 1; i < 4; i++)
if (!(temp->q[i] == temp->q[0])) {
flag = 2; break;
}
if (flag == 0 || flag == 1) { count1 -= 4; count2 -= 4; return flag; }//合并删除
for (int i = 2; i < Index; i++)
if (search[i]==*temp)
{
count2 = count2_2;
return i;
}
search[Index++] = quadtree(*temp);
return Index - 1;
}
int main()
{
while (scanf_s("%d %d", &n, &m) && n&&m)
{
init();
if (n < m)swap(n, m);
int k = 1;
while (k < n)k = k << 1;
DFS(0, 0, k);
cout << count1 << " " << count2 << endl;
}
//system("pause");
return 0;
}