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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

constexpr int px[19] = {1, 1, 2, 2, 2, 3, 3, 0, 0, 0,
                        1, 1, 2, 2, 3, 3, 4, 4, 4};
constexpr int py[19] = {1, 2, 1, 2, 3, 2, 3, 0, 1, 2,
                        0, 3, 0, 4, 1, 4, 2, 3, 4};
constexpr int dx[7] = {-1, -1, 0, 0, 0, 1, 1};
constexpr int dy[7] = {-1, 0, -1, 0, 1, 0, 1};

string check(const string c) {
  char field[3][5][5];

  for (int j = 0; j < (1 << 19); ++j) {

    for (int k = 0; k < 19; ++k) {
      field[0][px[k]][py[k]] = (j & (1 << k)) != 0 ? 1 : 0;
    }

    for (int k = 0; k < 7; ++k) {
      int x = px[k], y = py[k];
      int pattern = 0;
      for (int l = 0; l < 7; ++l) {
        if (field[0][x + dx[l]][y + dy[l]] > 0) {
          pattern += (1 << l);
        }
      }
      field[1][x][y] = c[pattern] - '0';
    }

    {
      int pattern = 0;
      for (int l = 0; l < 7; ++l) {
        if (field[1][2 + dx[l]][2 + dy[l]] > 0) {
          pattern += (1 << l);
        }
      }
      field[2][2][2] = c[pattern] - '0';
    }

    if (field[1][2][2] != field[2][2][2]) {
      return "no";
    }
  }

  return "yes";
}

bool solve() {
  string c;
  cin >> c;
  if (c == "#") {
    return false;
  }
  cout << check(c) << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (solve())
    ;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

string check(const string c) {
  static const int N = 5;
  static const int px[19] = {1, 1, 2, 2, 2, 3, 3, 0, 0, 0,
                             1, 1, 2, 2, 3, 3, 4, 4, 4};
  static const int py[19] = {1, 2, 1, 2, 3, 2, 3, 0, 1, 2,
                             0, 3, 0, 4, 1, 4, 2, 3, 4};
  static const int dx[7] = {-1, -1, 0, 0, 0, 1, 1};
  static const int dy[7] = {-1, 0, -1, 0, 1, 0, 1};
  static int field[3][N][N];

  for (int j = 0; j < (1 << 19); ++j) {

    for (int k = 0; k < 19; ++k) {
      if ((j & (1 << k)) != 0) {
        field[0][px[k]][py[k]] = 1;
      }
    }

    for (int z = 0; z <= 1; ++z) {
      for (int k = 0; k < 7; ++k) {
        int x = px[k], y = py[k];
        int pattern = 0;
        for (int l = 0; l < 7; ++l) {
          if (field[z][x + dx[l]][y + dy[l]] > 0) {
            pattern += (1 << l);
          }
        }
        field[z + 1][x][y] = c[pattern] - '0';
      }
    }

    if (field[1][2][2] != field[2][2][2]) {
      return "no";
    }
  }

  return "yes";
}

bool solve() {
  string c;
  cin >> c;
  if (c == "#") {
    return false;
  }
  cout << check(c) << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (solve())
    ;
  return 0;
}