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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int N, ans[10][10];
int diff_row_1[5] = {-1, 0, 0, 0, 1}, diff_col_1[5] = {0, -1, 0, 1, 0};
int diff_row_2[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1},
    diff_col_2[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int diff_row_3[13] = {-2, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 2},
    diff_col_3[13] = {0, -1, 0, 1, -2, -1, 0, 1, 2, -1, 0, 1, 0};
bool Found = false;

struct Info {
  void set(int arg_x, int arg_y, int arg_size) {
    x = arg_x;
    y = arg_y;
    size = arg_size;
  }
  int x, y, size;
};

struct Data {
  int table[10][10];
  Info info[13];
  int count;
};

Info result[13];

void recursive(int base_row, int base_col, Data data) {

  if (Found == true)
    return;

  bool FLG;

  if (data.count == N) {

    FLG = true;

    for (int row = 0; row < 10; row++) {
      for (int col = 0; col < 10; col++) {
        if (data.table[row][col] != ans[row][col]) {
          FLG = false;
          break;
        }
      }
      if (!FLG)
        break;
    }

    if (FLG) {

      for (int i = 0; i < N; i++) {
        result[i].set(data.info[i].x, data.info[i].y, data.info[i].size);
      }
      Found = true;
    }
    return;
  }

  for (int row = 0; row <= base_row - 3; row++) {
    for (int col = 0; col < 10; col++) {
      if (data.table[row][col] != ans[row][col])
        return;
    }
  }

  if (base_row >= 2) {
    for (int col = 0; col < base_col; col++) {
      if (data.table[base_row - 2][col] != ans[base_row - 2][col])
        return;
    }
  }

  bool size_1 = true, size_2 = true, size_3 = true;

  // size_1
  for (int i = 0; i < 5; i++) {
    if (data.table[base_row + diff_row_1[i]][base_col + diff_col_1[i]] >=
        ans[base_row + diff_row_1[i]][base_col + diff_col_1[i]]) {
      size_1 = false;
      break;
    }
  }

  // size_2
  for (int i = 0; i < 9; i++) {
    if (data.table[base_row + diff_row_2[i]][base_col + diff_col_2[i]] >=
        ans[base_row + diff_row_2[i]][base_col + diff_col_2[i]]) {
      size_2 = false;
      break;
    }
  }

  // size_3
  for (int i = 0; i < 13; i++) {
    if (data.table[base_row + diff_row_3[i]][base_col + diff_col_3[i]] >=
        ans[base_row + diff_row_3[i]][base_col + diff_col_3[i]]) {
      size_3 = false;
      break;
    }
  }

  if (base_col == 9) {

    if (base_row == 9)
      return;
    recursive(base_row + 1, 0, data);
  } else {
    recursive(base_row, base_col + 1, data);
  }

  if (size_1) {

    Data next;
    for (int i = 0; i < 10; i++) {
      for (int k = 0; k < 10; k++)
        next.table[i][k] = data.table[i][k];
    }
    // 垂らす
    for (int i = 0; i < 5; i++) {
      next.table[base_row + diff_row_1[i]][base_col + diff_col_1[i]]++;
    }

    for (int i = 0; i < data.count; i++) {
      next.info[i].set(data.info[i].x, data.info[i].y, data.info[i].size);
    }
    next.info[data.count].set(base_col, base_row, 1);
    next.count = data.count + 1;

    recursive(base_row, base_col, next);
  }

  if (size_2) {

    Data next;
    for (int i = 0; i < 10; i++) {
      for (int k = 0; k < 10; k++)
        next.table[i][k] = data.table[i][k];
    }
    // 垂らす
    for (int i = 0; i < 9; i++) {
      next.table[base_row + diff_row_2[i]][base_col + diff_col_2[i]]++;
    }

    for (int i = 0; i < data.count; i++) {
      next.info[i].set(data.info[i].x, data.info[i].y, data.info[i].size);
    }
    next.info[data.count].set(base_col, base_row, 2);
    next.count = data.count + 1;

    recursive(base_row, base_col, next);
  }

  if (size_3) {
    Data next;
    for (int i = 0; i < 10; i++) {
      for (int k = 0; k < 10; k++)
        next.table[i][k] = data.table[i][k];
    }
    // 垂らす
    for (int i = 0; i < 13; i++) {
      next.table[base_row + diff_row_3[i]][base_col + diff_col_3[i]]++;
    }

    for (int i = 0; i < data.count; i++) {
      next.info[i].set(data.info[i].x, data.info[i].y, data.info[i].size);
    }
    next.info[data.count].set(base_col, base_row, 3);
    next.count = data.count + 1;

    recursive(base_row, base_col, next);
  }
}

int main() {

  scanf("%d", &N);

  Data first;
  first.count = 0;

  for (int i = 0; i < 10; i++) {
    for (int k = 0; k < 10; k++) {
      scanf("%d", &ans[i][k]);
      first.table[i][k] = 0;
    }
  }

  recursive(1, 1, first);

  for (int i = 0; i < N; i++) {
    printf("%d %d %d\\n", result[i].x, result[i].y, result[i].size);
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

int N, ans[10][10];
int diff_row_1[5] = {-1, 0, 0, 0, 1}, diff_col_1[5] = {0, -1, 0, 1, 0};
int diff_row_2[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1},
    diff_col_2[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int diff_row_3[13] = {-2, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 2},
    diff_col_3[13] = {0, -1, 0, 1, -2, -1, 0, 1, 2, -1, 0, 1, 0};

struct Info {
  void set(int arg_x, int arg_y, int arg_size) {
    x = arg_x;
    y = arg_y;
    size = arg_size;
  }
  int x, y, size;
};

struct Data {
  int table[10][10];
  Info info[13];
  int count;
};

int main() {

  Info result[13];
  int FLG;

  scanf("%d", &N);

  Data first;
  first.count = 0;

  for (int i = 0; i < 10; i++) {
    for (int k = 0; k < 10; k++) {
      scanf("%d", &ans[i][k]);
      first.table[i][k] = 0;
    }
  }

  queue<Data> Q;
  Q.push(first);

  Data data;

  while (!Q.empty()) {

    data = Q.front();
    Q.pop();

    if (data.count ==
        N) { // N???????????????????????????????????¢????????????????????????N-1?????\\?????§????????¢??§????????´????????????????????\\????????????????????????????????¨?§£?????????

      FLG = 1;

      //????????¢??????
      for (int y = 0; y < 10; y++) {
        for (int x = 0; x < 10; x++) {
          if (data.table[y][x] != ans[y][x]) {
            FLG = 0;
            break;
          }
        }
        if (FLG == 0)
          break;
      }

      if (FLG == 1) { //????????¢

        //?????°?????¢?¨?
        for (int i = 1; i <= N; i++) {
          result[i].set(data.info[i].x, data.info[i].y, data.info[i].size);
        }
        break;
      } else {
        continue;
      }
    }

    //?????????1????????????
    for (int y = 1; y <= 8; y++) {
      for (int x = 1; x <= 8; x++) {
        // y,x???????????¨????????????????????¨?????£?§£???????????????1??\\??????????????¨????¢??????????
        FLG = 1;
        for (int i = 0; i < 5; i++) {
          if (data.table[y + diff_row_1[i]][x + diff_col_1[i]] >=
              ans[y + diff_row_1[i]][x + diff_col_1[i]]) {
            FLG = 0;
            break;
          }
        }

        if (FLG == 1) { //??????????????´???
          Data next;
          for (int i = 0; i < 10; i++) {
            for (int k = 0; k < 10; k++)
              next.table[i][k] = data.table[i][k];
          }
          //?????????
          for (int i = 0; i < 5; i++) {
            next.table[y + diff_row_1[i]][x + diff_col_1[i]]++;
          }
          for (int i = 1; i <= data.count; i++) {
            next.info[i].set(data.info[i].x, data.info[i].y, data.info[i].size);
          }
          next.count = data.count + 1;
          next.info[next.count].set(x, y, 1);
          Q.push(next);
        }
      }
    }

    //?????????2????????????
    for (int y = 1; y <= 8; y++) {
      for (int x = 1; x <= 8; x++) {
        // y,x???????????¨????????????????????¨?????£?§£???????????????1??\\??????????????¨????¢??????????
        FLG = 1;
        for (int i = 0; i < 9; i++) {
          if (data.table[y + diff_row_2[i]][x + diff_col_2[i]] >=
              ans[y + diff_row_2[i]][x + diff_col_2[i]]) {
            FLG = 0;
            break;
          }
        }

        if (FLG == 1) { //??????????????´???
          Data next;
          for (int i = 0; i < 10; i++) {
            for (int k = 0; k < 10; k++)
              next.table[i][k] = data.table[i][k];
          }
          //?????????
          for (int i = 0; i < 9; i++) {
            next.table[y + diff_row_2[i]][x + diff_col_2[i]]++;
          }
          for (int i = 1; i <= data.count; i++) {
            next.info[i].set(data.info[i].x, data.info[i].y, data.info[i].size);
          }
          next.count = data.count + 1;
          next.info[next.count].set(x, y, 2);
          Q.push(next);
        }
      }
    }

    //?????????3????????????
    for (int y = 2; y <= 7; y++) {
      for (int x = 2; x <= 7; x++) {
        // y,x???????????¨????????????????????¨?????£?§£???????????????1??\\??????????????¨????¢??????????
        FLG = 1;
        for (int i = 0; i < 13; i++) {
          if (data.table[y + diff_row_3[i]][x + diff_col_3[i]] >=
              ans[y + diff_row_3[i]][x + diff_col_3[i]]) {
            FLG = 0;
            break;
          }
        }

        if (FLG == 1) { //??????????????´???
          Data next;
          for (int i = 0; i < 10; i++) {
            for (int k = 0; k < 10; k++)
              next.table[i][k] = data.table[i][k];
          }
          //?????????
          for (int i = 0; i < 13; i++) {
            next.table[y + diff_row_3[i]][x + diff_col_3[i]]++;
          }
          for (int i = 1; i <= data.count; i++) {
            next.info[i].set(data.info[i].x, data.info[i].y, data.info[i].size);
          }
          next.count = data.count + 1;
          next.info[next.count].set(x, y, 3);
          Q.push(next);
        }
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    printf("%d %d %d\\n", result[i].x, result[i].y, result[i].size);
  }

  return 0;
}