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


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <bitset>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c) __typeof__((c).begin())
#define FOREACH(it, c) for (ITER(c) it = (c).begin(); it != (c).end(); ++it)
#define RITER(c) __typeof__((c).rbegin())
#define RFOREACH(it, c) for (RITER(c) it = (c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n) REPEAT(i, 0, n)
#define RREP(i, n) RREPEAT(i, 0, n);
#define REPEAT(i, k, n) for (int i = (k); i < (k + n); ++i)
#define RREPEAT(i, k, n) for (int i = (k) + (n)-1; i >= (k); ++i)
#define ALL(c) (c).begin(), (c).end()
#define LLPOW(p1, p2) ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A) (sizeof(A) / sizeof((A)[0]))
#define CIN_NO_SYNC                                                            \\
  do {                                                                         \\
    cin.tie(0);                                                                \\
    ios_base::sync_with_stdio(false);                                          \\
  } while (0)
#define GETSTR(p) fgets((p), sizeof(p), stdin)
#define CHOMP(p)                                                               \\
  do {                                                                         \\
    char *_q = (p) + strlen(p) - 1;                                            \\
    if (*_q == '\\n' || *_q == '\\r')                                            \\
      *_q = 0;                                                                 \\
  } while (0)
#define mp make_pair
#define pb push_back
template <class _T> _T MAX(_T p1, _T p2) { return (p1 > p2) ? p1 : p2; }
template <class _T> _T MIN(_T p1, _T p2) { return (p1 < p2) ? p1 : p2; }

typedef long long ll;
typedef unsigned long long ull;

#define X real()
#define Y imag()
typedef double D;
typedef complex<D> P;
#define EPS (1e-9)

#ifdef _DEBUG
#define pf printf
#define pr(n)                                                                  \\
  do {                                                                         \\
    cout << #n " = " << (n) << endl;                                           \\
  } while (0)
#define prfe(n)                                                                \\
  do {                                                                         \\
    cout << #n " =";                                                           \\
    FOREACH(_it, n) cout << " " << *_it;                                       \\
    cout << endl;                                                              \\
  } while (0)
#else
#define pf(...)   /* */
#define pr(...)   /* */
#define prfe(...) /* */
#endif

int p[][2] = {
    {1, 0},  {-1, 0},  {0, 1}, {0, -1}, {1, 1}, {1, -1},
    {-1, 1}, {-1, -1}, {2, 0}, {-2, 0}, {0, 2}, {0, -2},
};

bool rec(int n, int x0, int y0, char t[10][10],
         vector<pair<pair<char, char>, char>> &vii) {
  const int w = 10, h = 10;
  char t0[10][10];

  int num = 0;
  REP(y, h) REP(x, w) num += t[y][x];
  if (num == 0)
    return true;
  if (n == 0)
    return false;

  vector<pair<pair<char, char>, char>> viit;

#ifdef _DEBUG
  printf("--------\\n");
  REP(y, h) {
    REP(x, w) { printf("%d ", t[y][x]); }
    printf("\\n");
  }
#endif

  REP(y, h) {
    if (y >= 3) {
      REP(x, w) if (t[y - 3][x] > 0) return false;
    }
    if (y < y0)
      continue;
    REP(x, w) {
      if (y == y0 && x < x0)
        continue;
      if (x == 0 || x == w - 1 || y == 0 || y == h - 1)
        continue;
      if (t[y][x] == 0)
        continue;
      memcpy(t0, t, sizeof(t0));
      t0[y][x]--;
      REP(i, 12) {
        int nx = x + p[i][0];
        int ny = y + p[i][1];
        if (nx < 0 || ny < 0 || nx >= w || ny >= h)
          break;
        if (t0[ny][nx] == 0)
          break;
        t0[ny][nx]--;
        if (i % 4 == 3) {
          bool ret = rec(n - 1, x, y, t0, viit);
          if (!ret)
            continue;
          viit.push_back(mp(mp(x, y), i / 4 + 1));
          vii.insert(vii.end(), ALL(viit));
          return true;
        }
      }
    }
  }
  return false;
}

void solve(int n, char t[10][10]) {
  vector<pair<pair<char, char>, char>> v;
  rec(n, 0, 0, t, v);
  FOREACH(it, v) {
    printf("%d %d %d\\n", it->first.first, it->first.second, it->second);
  }
}

int main() {
  int n, m;
  cin >> n;

  char t[10][10];
  REP(i, 10) REP(j, 10) {
    cin >> m;
    t[i][j] = m;
  }
  solve(n, t);
  return 0;
}

Error: TLE

Fixed submission:


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <bitset>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c) __typeof__((c).begin())
#define FOREACH(it, c) for (ITER(c) it = (c).begin(); it != (c).end(); ++it)
#define RITER(c) __typeof__((c).rbegin())
#define RFOREACH(it, c) for (RITER(c) it = (c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n) REPEAT(i, 0, n)
#define RREP(i, n) RREPEAT(i, 0, n);
#define REPEAT(i, k, n) for (int i = (k); i < (k + n); ++i)
#define RREPEAT(i, k, n) for (int i = (k) + (n)-1; i >= (k); ++i)
#define ALL(c) (c).begin(), (c).end()
#define LLPOW(p1, p2) ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A) (sizeof(A) / sizeof((A)[0]))
#define CIN_NO_SYNC                                                            \\
  do {                                                                         \\
    cin.tie(0);                                                                \\
    ios_base::sync_with_stdio(false);                                          \\
  } while (0)
#define GETSTR(p) fgets((p), sizeof(p), stdin)
#define CHOMP(p)                                                               \\
  do {                                                                         \\
    char *_q = (p) + strlen(p) - 1;                                            \\
    if (*_q == '\\n' || *_q == '\\r')                                            \\
      *_q = 0;                                                                 \\
  } while (0)
#define mp make_pair
#define pb push_back
template <class _T> _T MAX(_T p1, _T p2) { return (p1 > p2) ? p1 : p2; }
template <class _T> _T MIN(_T p1, _T p2) { return (p1 < p2) ? p1 : p2; }

typedef long long ll;
typedef unsigned long long ull;

#define X real()
#define Y imag()
typedef double D;
typedef complex<D> P;
#define EPS (1e-9)

#ifdef _DEBUG
#define pf printf
#define pr(n)                                                                  \\
  do {                                                                         \\
    cout << #n " = " << (n) << endl;                                           \\
  } while (0)
#define prfe(n)                                                                \\
  do {                                                                         \\
    cout << #n " =";                                                           \\
    FOREACH(_it, n) cout << " " << *_it;                                       \\
    cout << endl;                                                              \\
  } while (0)
#else
#define pf(...)   /* */
#define pr(...)   /* */
#define prfe(...) /* */
#endif

int p[][2] = {
    {1, 0},  {-1, 0},  {0, 1}, {0, -1}, {1, 1}, {1, -1},
    {-1, 1}, {-1, -1}, {2, 0}, {-2, 0}, {0, 2}, {0, -2},
};

bool rec(int n, int x0, int y0, char t[10][10],
         vector<pair<pair<char, char>, char>> &vii) {
  const int w = 10, h = 10;
  char t0[10][10];
  char t1[100][10][10];

  memcpy(t0, t, sizeof(t0));
  memset(t1, 0, sizeof(t1));
  int ti = 0;
  REP(y, h) {
    REP(x, w) {
      if (t0[y][x] == 0)
        continue;
      int count = 1;
      t1[ti][y][x] = t0[y][x];
      t0[y][x] = 0;
      deque<pair<int, int>> q;
      q.pb(mp(x, y));
      while (!q.empty()) {
        int qx = q.front().first;
        int qy = q.front().second;
        q.pop_front();
        REP(i, 4) {
          int nx = qx + p[i][0];
          int ny = qy + p[i][1];
          if (nx < 0 || ny < 0 || nx >= w || ny >= h)
            continue;
          if (t0[ny][nx] == 0)
            continue;
          t1[ti][ny][nx] = t0[ny][nx];
          t0[ny][nx] = 0;
          q.pb(mp(nx, ny));
          count++;
        }
      }
      if (count < 5)
        return false;
      ti++;
    }
  }
  if (ti == 0)
    return true;
  if (n == 0)
    return false;

  vector<pair<pair<char, char>, char>> viit;

  REP(i, ti) {

#ifdef _DEBUG
    printf("---- [%d] ----\\n", i);
    REP(y, h) {
      REP(x, w) { printf("%d ", t1[i][y][x]); }
      printf("\\n");
    }
#endif
    REP(y, y0 - 2) REP(x, w) {
      if (t1[i][y][x] > 0)
        return false;
    }

    REP(y, h) {
      if (y < y0)
        continue;
      REP(x, w) {
        if (y == y0 && x < x0)
          continue;
        if (x == 0 || x == w - 1 || y == 0 || y == h - 1)
          continue;
        if (t1[i][y][x] == 0)
          continue;
        memcpy(t0, t1[i], sizeof(t0));
        t0[y][x]--;
        REP(i, 12) {
          int nx = x + p[i][0];
          int ny = y + p[i][1];
          if (nx < 0 || ny < 0 || nx >= w || ny >= h)
            break;
          if (t0[ny][nx] == 0)
            break;
          t0[ny][nx]--;
          if (i % 4 == 3) {
            bool ret = rec(n - 1, x, y, t0, viit);
            if (!ret)
              continue;
            viit.push_back(mp(mp(x, y), i / 4 + 1));
            // printf("%d %d %d\\n", x, y, i/4+1);
            goto blockend;
          }
        }
      }
    }
    return false;
  blockend:;
  }
  vii.insert(vii.end(), ALL(viit));

  return true;
}

void solve(int n, char t[10][10]) {
  vector<pair<pair<char, char>, char>> v;
  rec(n, 0, 0, t, v);
  FOREACH(it, v) {
    printf("%d %d %d\\n", it->first.first, it->first.second, it->second);
  }
}

int main() {
  int n, m;
  cin >> n;

  char t[10][10];
  REP(i, 10) REP(j, 10) {
    cin >> m;
    t[i][j] = m;
  }
  solve(n, t);
  return 0;
}