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

#include <algorithm>
#include <bits/stdc++.h>
#include <cfloat>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.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 diff_row_sun[4] = {0, 0, 1, 1}, diff_col_sun[4] = {0, 1, 0, 1};
int diff_row[9] = {-2, -1, 0, 0, 0, 0, 0, 1, 2},
    diff_col[9] = {0, 0, -2, -1, 0, 1, 2, 0, 0};
int N;

struct Info {
  int row, col; //?????????????????§?¨?
  int rain_table[16], day;
};

bool rangeCheck(int row, int col) {
  if (row >= 0 && row <= 2 && col >= 0 && col <= 2) {
    return true;
  } else {
    return false;
  }
}

std::set<tuple<int, int, int, ull>> SET; // day,row,col,hash
decltype(SET)::iterator IT;

void func() {

  SET.clear();

  int table[N + 1][16];

  for (int i = 1; i <= N; i++) {
    for (int k = 0; k < 16; k++)
      scanf("%d", &table[i][k]);
  }

  Info first;
  first.row = 1;
  first.col = 1;
  for (int i = 0; i < 16; i++)
    first.rain_table[i] = 0;
  first.day = 1;

  bool FLG = false;
  bool check;

  //?????\\????¢?????????¨???????????´??????1????????£????????????
  if (table[1][5] == 1 || table[1][6] == 1 || table[1][9] == 1 ||
      table[1][10] == 1) {
    printf("0\\n");
    return;
  }

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

  int weather_map[4][4], row, col, tmp_row, tmp_col, next_row, next_col;

  while (!Q.empty()) {

    //??¨????????£??????????????\\??°????????????????????´??°
    for (int i = 0; i < 16; i++) {
      Q.front().rain_table[i]++;
    }
    //??¨????????£?????\\???0?????????
    Q.front().rain_table[4 * Q.front().row + Q.front().col] = 0;
    Q.front().rain_table[4 * Q.front().row + Q.front().col + 1] = 0;
    Q.front().rain_table[4 * (Q.front().row + 1) + Q.front().col] = 0;
    Q.front().rain_table[4 * (Q.front().row + 1) + Q.front().col + 1] = 0;

    // 7??\\??\\?????¨?????????????????°???????????£????????????
    check = true;
    for (int i = 0; i < 16; i++) {
      if (Q.front().rain_table[i] >= 7) {
        check = false;
        break;
      }
    }

    if (!check) {
      Q.pop();
      continue;
    }

    //????????¢??????
    if (Q.front().day >= N) {
      FLG = true;
      break;
    }

    //?§??????????
    for (int a = 0; a < 9; a++) {
      next_row = Q.front().row + diff_row[a];
      next_col = Q.front().col + diff_col[a];

      //?§????????????¶???:??????????????????????????????????????????
      // day??\\??????????????¨???????????¶??????????????????
      row = 0, col = 0;
      for (int i = 0; i < 16; i++) {
        weather_map[row][col++] = table[Q.front().day + 1][i];
        if (col >= 4) {
          col = 0;
          row++;
        }
      }

      //?§??????????????????????????????????1?????????
      if (rangeCheck(next_row, next_col)) {
        check = true;
        for (int k = 0; k < 4; k++) {
          tmp_row = next_row + diff_row_sun[k];
          tmp_col = next_col + diff_col_sun[k];
          if (weather_map[tmp_row][tmp_col] == 1) {
            check = false;
            break;
          }
        }
        if (check) { //???????????\\???????¨????
          ull pre = 0;
          ull current = 0;

          for (int i = 0; i < 16; i++) {
            current = Q.front().rain_table[i] + pre * MOD;
            pre = current;
          }

          auto Data = make_tuple(Q.front().day + 1, next_row, next_col, pre);
          if (SET.count(Data) != 0) {
            check = false;
          } else {
            SET.insert(Data);
          }
        }

        if (check) {
          Info next;
          next.row = next_row;
          next.col = next_col;
          for (int k = 0; k < 16; k++)
            next.rain_table[k] = Q.front().rain_table[k];
          next.day = Q.front().day + 1;
          Q.push(next);
        }
      }
    }

    Q.pop();
  }

  if (!FLG) {
    printf("0\\n");
  } else {
    printf("1\\n");
  }
}

int main() {

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    func();
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.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 diff_row_sun[4] = {0, 0, 1, 1}, diff_col_sun[4] = {0, 1, 0, 1};
int diff_row[9] = {-2, -1, 0, 0, 0, 0, 0, 1, 2},
    diff_col[9] = {0, 0, -2, -1, 0, 1, 2, 0, 0};
int N;

struct Info {
  int row, col; //?????????????????§?¨?
  int rain_table[16], day;
};

bool rangeCheck(int row, int col) {
  if (row >= 0 && row <= 2 && col >= 0 && col <= 2) {
    return true;
  } else {
    return false;
  }
}

void func() {

  int table[N + 1][16];

  for (int i = 1; i <= N; i++) {
    for (int k = 0; k < 16; k++)
      scanf("%d", &table[i][k]);
  }

  Info first;
  first.row = 1;
  first.col = 1;
  for (int i = 0; i < 16; i++)
    first.rain_table[i] = 0;
  first.day = 1;

  bool FLG = false;
  bool check;

  //?????\\????¢?????????¨???????????´??????1????????£????????????
  if (table[1][5] == 1 || table[1][6] == 1 || table[1][9] == 1 ||
      table[1][10] == 1) {
    printf("0\\n");
    return;
  }

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

  int map[4][4], row, col, tmp_row, tmp_col, next_row, next_col;

  while (!Q.empty()) {

    //??¨????????£??????????????\\??°????????????????????´??°
    for (int i = 0; i < 16; i++) {
      Q.front().rain_table[i]++;
    }
    //??¨????????£?????\\???0?????????
    Q.front().rain_table[4 * Q.front().row + Q.front().col] = 0;
    Q.front().rain_table[4 * Q.front().row + Q.front().col + 1] = 0;
    Q.front().rain_table[4 * (Q.front().row + 1) + Q.front().col] = 0;
    Q.front().rain_table[4 * (Q.front().row + 1) + Q.front().col + 1] = 0;

    // 7??\\??\\?????¨?????????????????°???????????£????????????
    check = true;
    for (int i = 0; i < 16; i++) {
      if (Q.front().rain_table[i] >= 7) {
        check = false;
        break;
      }
    }

    if (!check) {
      Q.pop();
      continue;
    }

    //????????¢??????
    if (Q.front().day >= N) {
      FLG = true;
      break;
    }

    //?§??????????
    for (int a = 0; a < 9; a++) {
      next_row = Q.front().row + diff_row[a];
      next_col = Q.front().col + diff_col[a];

      //?§????????????¶???:??????????????????????????????????????????
      // day??\\??????????????¨???????????¶??????????????????
      row = 0, col = 0;
      for (int i = 0; i < 16; i++) {
        map[row][col++] = table[Q.front().day + 1][i];
        if (col >= 4) {
          col = 0;
          row++;
        }
      }

      //?§??????????????????????????????????1?????????
      if (rangeCheck(next_row, next_col)) {
        check = true;
        for (int k = 0; k < 4; k++) {
          tmp_row = next_row + diff_row_sun[k];
          tmp_col = next_col + diff_col_sun[k];
          if (map[tmp_row][tmp_col] == 1) {
            check = false;
            break;
          }
        }
        if (check) {
          Info next;
          next.row = next_row;
          next.col = next_col;
          for (int k = 0; k < 16; k++)
            next.rain_table[k] = Q.front().rain_table[k];
          next.day = Q.front().day + 1;
          Q.push(next);
        }
      }
    }

    Q.pop();
  }

  if (!FLG) {
    printf("0\\n");
  } else {
    printf("1\\n");
  }
}

int main() {

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    func();
  }

  return 0;
}