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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

char memo[1 << 20];
int n;

int main() {
  {
    queue<int> q;
    int cost = 0;
    q.push(-1);
    q.push(0);
    memo[0] = 1;

    while (q.size() != 1) {
      int state = q.front();
      q.pop();

      if (state == -1) {
        cost++;
        q.push(-1);
        // printf("cost = %d, size = %d\\n",cost,q.size());
        continue;
      }

      // printf("state: %x\\n",state);

      for (int xs = 0; xs < 5; xs++) {
        for (int xe = xs + 1; xe <= 5; xe++) {
          int mask = ((1 << xe) - 1) ^ ((1 << xs) - 1);
          for (int ys = 0; ys < 4; ys++) {
            int next = state;
            for (int ye = ys; ye < 4; ye++) {
              next ^= mask << (ye * 5);
              if (!memo[next]) {
                q.push(next);
                memo[next] = cost + 1;
              }
            }
          }
        }
      }
    }
  }

  while (n = getInt()) {
    int ss = 0;
    REP(i, n) REP(j, n) {
      int t = getInt();
      ss |= (t << (j + i * 5));
    }

    queue<int> q;
    int cost = 0;
    const int mm = ((1 << 25) - 1) ^ ((1 << 20) - 1);
    set<int> f;
    q.push(ss);
    f.insert(ss);
    q.push(-1);

    int ans = INT_MAX;
    while (q.size() != 1) {
      int state = q.front();
      q.pop();

      if (state == -1) {
        cost++;
        q.push(-1);
        // printf("cost = %d, size = %d\\n",cost,q.size());
        if (cost >= ans - 1)
          break;
        continue;
      }

      if ((state & mm) == 0) {
        ans = min(ans, cost + memo[state] - 1);
      }

      for (int xs = 0; xs < 5; xs++) {
        for (int xe = xs + 1; xe <= 5; xe++) {
          int mask = ((1 << xe) - 1) ^ ((1 << xs) - 1);
          for (int ys = 0; ys < 5; ys++) {
            int next = state;
            for (int ye = ys; ye < 5; ye++) {
              next ^= mask << (ye * 5);
              if (ye == 4 && !f.count(next)) {
                if (cost + 1 + memo[next & ((1 << 20) - 1)] < ans) {
                  f.insert(next);
                  q.push(next);
                }
              }
            }
          }
        }
      }
    }
    while (ans-- > 0)
      printf("myon");
    puts("");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

char memo[1 << 20];
int n;

int main() {
  {
    queue<int> q;
    int cost = 0;
    q.push(-1);
    q.push(0);
    memo[0] = 1;

    while (q.size() != 1) {
      int state = q.front();
      q.pop();

      if (state == -1) {
        cost++;
        q.push(-1);
        // printf("cost = %d, size = %d\\n",cost,q.size());
        continue;
      }

      // printf("state: %x\\n",state);

      for (int xs = 0; xs < 5; xs++) {
        for (int xe = xs + 1; xe <= 5; xe++) {
          int mask = ((1 << xe) - 1) ^ ((1 << xs) - 1);
          for (int ys = 0; ys < 4; ys++) {
            int next = state;
            for (int ye = ys; ye < 4; ye++) {
              next ^= mask << (ye * 5);
              if (!memo[next]) {
                q.push(next);
                memo[next] = cost + 1;
              }
            }
          }
        }
      }
    }
  }

  while (n = getInt()) {
    int ss = 0;
    REP(i, n) REP(j, n) {
      int t = getInt();
      ss |= (t << (j + i * 5));
    }

    queue<int> q;
    int cost = 0;
    const int mm = ((1 << 25) - 1) ^ ((1 << 20) - 1);
    set<int> f;
    q.push(ss);
    f.insert(ss);
    q.push(-1);

    int ans = INT_MAX;
    while (q.size() != 1) {
      int state = q.front();
      q.pop();

      if (state == -1) {
        cost++;
        q.push(-1);
        // printf("cost = %d, size = %d\\n",cost,q.size());
        if (cost > ans)
          break;
        continue;
      }

      if ((state & mm) == 0) {
        ans = min(ans, cost + memo[state] - 1);
      }

      for (int xs = 0; xs < 5; xs++) {
        for (int xe = xs + 1; xe <= 5; xe++) {
          int mask = ((1 << xe) - 1) ^ ((1 << xs) - 1);
          for (int ys = 0; ys < 5; ys++) {
            int next = state;
            for (int ye = ys; ye < 5; ye++) {
              next ^= mask << (ye * 5);
              if (ye == 4 && !f.count(next)) {
                f.insert(next);
                q.push(next);
              }
            }
          }
        }
      }
    }
    while (ans-- > 0)
      printf("myon");
    puts("");
  }

  return 0;
}