Skip to main content

Solution to UVa297 - Quadtrees using C++


This is the solution to this UVa problem based on Quad Tree.

Code

#include <iostream>
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

int gi;

void filler(vector<vector<int>>& a,
            string s,
            int l = 0,
            int r = 31,
            int t = 0,
            int d = 31) {
  if (s[gi] == 'e') {
    gi++;
    return;
  }
  if (s[gi] == 'f') {
    for (int i = t; i <= d; i++)
      for (int j = l; j <= r; j++)
        a[i][j] = 1;
    gi++;
    return;
  }
  gi++;

  filler(a, s, (l + r) / 2 + 1, r, t, (t + d) / 2);
  filler(a, s, l, (l + r) / 2, t, (t + d) / 2);
  filler(a, s, l, (l + r) / 2, (t + d) / 2 + 1, d);
  filler(a, s, (l + r) / 2 + 1, r, (t + d) / 2 + 1, d);
}

int main() {
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  while (n--) {
    string s1, s2;
    cin >> s1 >> s2;

    vector<vector<int>> a(32, vector<int>(32, 0));
    vector<vector<int>> b(32, vector<int>(32, 0));
    gi = 0;
    filler(a, s1);
    gi = 0;
    filler(b, s2);
    int counter = 0;
    for (int i = 0; i < 32; i++)
      for (int j = 0; j < 32; j++)
        if (a[i][j] || b[i][j])
          counter++;
    cout << "There are " << counter << " black pixels.\n";
  }

  return 0;
}



Comments

Popular posts from this blog