Problem p01214 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  int N;
  cin >> N;
  while (N--) {
    int H1, W1, H2, W2;
    cin >> H1 >> W1;
    vector<string> block(H1);
    REP(i, H1) cin >> block[i];
    cin >> H2 >> W2;
    vector<string> grid(H2);
    REP(i, H2) cin >> grid[i];
    int ans = -1;
    for (int iter = 0; iter < 4; iter++) {
      for (int by = -H1 + 1; by < H2; by++) {
        int y1 = max(by, 0), y2 = min(H2, by + H1);
        if (y2 - y1 <= ans)
          continue;
        for (int bx = -W1 + 1; bx < W2; bx++) {
          bool exist = false;
          REP(y, H1)
          REP(x,
              W1) if (block[y][x] == '#' && (!valid(bx + x, by + y, W2, H2) ||
                                             grid[by + y][bx + x] == '#')) {
            exist = true;
            break;
          }
          if (exist)
            continue; // dame
          vector<string> grid_es = grid;
          REP(y, H1)
          REP(x, W1) if (block[y][x] == '#') grid_es[by + y][bx + x] = '#';
          int cnt = 0;
          for (int y = y1; y < y2; y++) {
            bool ok = true;
            for (int x = 0; x < W2; x++) {
              if (grid_es[y][x] != '#') {
                ok = false;
                break;
              }
            }
            if (ok)
              cnt++;
          }
          ans = max(ans, cnt);
        }
      }
      swap(H1, W1);
      vector<string> new_block(H1, string(W1, ' '));
      for (int y = 0; y < H1; y++) {
        for (int x = 0; x < W1; x++) {
          new_block[y][x] = block[x][H1 - y - 1];
        }
      }
      block = new_block;
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  int N;
  cin >> N;
  while (N--) {
    int H1, W1, H2, W2;
    cin >> H1 >> W1;
    vector<string> block(H1);
    REP(i, H1) cin >> block[i];
    cin >> H2 >> W2;
    vector<string> grid(H2);
    REP(i, H2) cin >> grid[i];
    int ans = -1;
    for (int iter = 0; iter < 4; iter++) {
      int my1 = INF, my2 = -INF;
      REP(y, H1)
      REP(x, W1) if (block[y][x] == '#') my1 = min(my1, y),
                                         my2 = max(my2, y);
      for (int by = -my1; by + my2 < H2; by++) {
        int y1 = max(by, 0), y2 = min(H2, by + H1);
        if (y2 - y1 <= ans)
          continue;
        for (int bx = -W1 + 1; bx < W2; bx++) {
          bool exist = false;
          REP(y, H1)
          REP(x,
              W1) if (block[y][x] == '#' && (!valid(bx + x, by + y, W2, H2) ||
                                             grid[by + y][bx + x] == '#')) {
            exist = true;
            break;
          }
          if (exist)
            continue; // dame
          vector<string> grid_es = grid;
          REP(y, H1)
          REP(x, W1) if (block[y][x] == '#') grid_es[by + y][bx + x] = '#';
          int cnt = 0;
          for (int y = y1; y < y2; y++) {
            bool ok = true;
            for (int x = 0; x < W2; x++) {
              if (grid_es[y][x] != '#') {
                ok = false;
                break;
              }
            }
            if (ok)
              cnt++;
          }
          ans = max(ans, cnt);
        }
      }
      swap(H1, W1);
      vector<string> new_block(H1, string(W1, ' '));
      for (int y = 0; y < H1; y++) {
        for (int x = 0; x < W1; x++) {
          new_block[y][x] = block[x][H1 - y - 1];
        }
      }
      block = new_block;
    }
    cout << ans << endl;
  }
  return 0;
}