Problem p01214 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int h1, w1, h2, w2, dis;
string block[4][65];
string board[65];
string cp[65];
int H[4], W[4];

bool check(int &y, int &x) { return (0 <= y && y < h2 && 0 <= x && x < w2); }

int y, x, idx;

int sim() {

  for (int i = 0; i < h1; i++) {
    for (int j = 0; j < w1; j++) {

      int py = y + i, px = x + j;

      if (block[idx][i][j] == '.')
        continue;
      if (!check(py, px) && block[idx][i][j] == '#')
        return 0;
      if (!check(py, px))
        continue;
      if (board[py][px] == '#')
        return 0;

      board[py][px] = '#';
    }
  }

  dis = 1;

  int res = 0;

  for (int i = 0; i < h2; i++) {

    int flag = 1;

    for (int j = 0; j < w2; j++)
      if (board[i][j] == '.') {
        flag = 0;
        break;
      }

    res += flag;
  }

  return res;
}

void solve() {

  int ans = 0;

  dis = 0;

  for (int k = 0; k < 4; k++) {

    h1 = H[k], w1 = W[k];

    for (int i = -h1 + 1; i < h2; i++)
      for (int j = -w1 + 1; j < w2; j++) {
        y = i, x = j, idx = k;
        ans = max(ans, sim());
        for (int l = 0; l < h2; l++)
          board[l] = cp[l];
      }
  }

  if (!dis)
    ans = -1;

  cout << ans << endl;
}

void init() {

  H[0] = H[2] = h1;
  H[1] = H[3] = w1;
  W[0] = W[2] = w1;
  W[1] = W[3] = h1;

  for (int j = 0; j < w1; j++)
    for (int i = h1 - 1; i >= 0; i--)
      block[1][j] += block[0][i][j];

  for (int i = h1 - 1; i >= 0; i--)
    for (int j = w1 - 1; j >= 0; j--)
      block[2][h1 - i - 1] += block[0][i][j];

  for (int j = w1 - 1; j >= 0; j--)
    for (int i = 0; i < h1; i++)
      block[3][w1 - j - 1] += block[0][i][j];
}

int main() {

  int t;
  cin >> t;

  while (t--) {

    cin >> h1 >> w1;
    for (int i = 0; i < h1; i++)
      cin >> block[0][i];

    init();

    cin >> h2 >> w2;
    for (int i = 0; i < h2; i++) {
      cin >> board[i];
      cp[i] = board[i];
    }

    solve();

    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 65; j++)
        block[i][j] = "";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int h1, w1, h2, w2, dis;
string block[4][65];
string board[65];
string cp[65];
int H[4], W[4];

bool check(int &y, int &x) { return (0 <= y && y < h2 && 0 <= x && x < w2); }

int y, x, idx;

int sim() {

  for (int i = 0; i < h1; i++) {
    for (int j = 0; j < w1; j++) {

      int py = y + i, px = x + j;

      if (!check(py, px) && block[idx][i][j] == '#')
        return 0;
      if (!check(py, px))
        continue;
      if (block[idx][i][j] == '.')
        continue;
      if (board[py][px] == '#')
        return 0;

      board[py][px] = '#';
    }
  }

  dis = 1;

  int res = 0;

  for (int i = 0; i < h2; i++) {

    int flag = 1;

    for (int j = 0; j < w2; j++)
      if (board[i][j] == '.')
        flag = 0;

    res += flag;
  }

  return res;
}

void solve() {

  int ans = 0;

  dis = 0;

  for (int k = 0; k < 4; k++) {

    h1 = H[k], w1 = W[k];

    for (int i = -h1 + 1; i < h2; i++)
      for (int j = -w1 + 1; j < w2; j++) {
        y = i, x = j, idx = k;
        ans = max(ans, sim());
        for (int l = 0; l < h2; l++)
          board[l] = cp[l];
      }
  }

  if (!dis)
    ans = -1;

  cout << ans << endl;
}

void init() {

  H[0] = H[2] = h1;
  H[1] = H[3] = w1;
  W[0] = W[2] = w1;
  W[1] = W[3] = h1;

  for (int j = 0; j < w1; j++)
    for (int i = h1 - 1; i >= 0; i--)
      block[1][j] += block[0][i][j];

  for (int i = h1 - 1; i >= 0; i--)
    for (int j = w1 - 1; j >= 0; j--)
      block[2][h1 - i - 1] += block[0][i][j];

  for (int j = w1 - 1; j >= 0; j--)
    for (int i = 0; i < h1; i++)
      block[3][w1 - j - 1] += block[0][i][j];
}

int main() {

  int t;
  cin >> t;

  while (t--) {

    cin >> h1 >> w1;
    for (int i = 0; i < h1; i++)
      cin >> block[0][i];

    init();

    cin >> h2 >> w2;
    for (int i = 0; i < h2; i++) {
      cin >> board[i];
      cp[i] = board[i];
    }

    solve();

    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 65; j++)
        block[i][j] = "";
  }

  return 0;
}