Problem p02114 (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 <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;

// If select somewhere,num of white_area + 3

struct Data {
  Data(int arg_row, int arg_col) {
    row = arg_row;
    col = arg_col;
  }
  int row, col;
};

int N;
int diff_row[4] = {-1, 0, 0, 1}, diff_col[4] = {0, -1, 1, 0};
char table[127][128];

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

int main() {

  scanf("%d", &N);

  for (int row = 0; row < N; row++)
    scanf("%s", table[row]);

  int count = 0;
  int adj_row, adj_col;

  queue<Data> Q;

  for (int row = 0; row < N; row++) {
    for (int col = 0; col < N; col++) {
      if (table[row][col] == 'o') {
        count++;
        table[row][col] = 'x';

        Q.push(Data(row, col));

        while (!Q.empty()) {

          for (int i = 0; i < 4; i++) {
            adj_row = Q.front().row + diff_row[i];
            adj_col = Q.front().col + diff_col[i];

            if (!rangeCheck(adj_row, adj_col))
              continue;

            if (table[adj_row][adj_col] == 'o') {
              table[adj_row][adj_col] = 'x';
              Q.push(Data(adj_row, adj_col));
            }
          }
          Q.pop();
        }
      }
    }
  }

  printf("%d\\n", (count - 1) / 3);

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#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;

struct Data {
  Data(int arg_row, int arg_col) {
    row = arg_row;
    col = arg_col;
  }
  int row, col;
};

struct Info {
  char tmp_table[127][127];
  int count;
};

int N;

void copyInfo(Info &to, Info from) {
  for (int row = 0; row < N; row++) {
    for (int col = 0; col < N; col++) {
      to.tmp_table[row][col] = from.tmp_table[row][col];
    }
  }
}

int main() {

  scanf("%d", &N);
  char ans_table[N][N + 1];

  for (int row = 0; row < N; row++) {
    scanf("%s", ans_table[row]);
  }

  Info first;
  for (int row = 0; row < N; row++) {
    for (int col = 0; col < N; col++) {
      first.tmp_table[row][col] = 'o';
    }
  }
  first.count = 0;

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

  bool FLG;

  while (!Q.empty()) {
    FLG = true;
    for (int row = 0; row < N; row++) {
      for (int col = 0; col < N; col++) {
        if (Q.front().tmp_table[row][col] != ans_table[row][col]) {
          FLG = false;
          break;
        }
      }
      if (!FLG)
        break;
    }

    if (FLG) {
      printf("%d\\n", Q.front().count);
      return 0;
    }

    for (int row = 0; row < N; row++) {
      if (row % 2 == 0)
        continue;
      for (int col = 0; col < N; col++) {
        if (col % 2 == 0 || ans_table[row][col] == 'o' ||
            Q.front().tmp_table[row][col] == 'x')
          continue;

        Info next_info;
        copyInfo(next_info, Q.front());

        next_info.tmp_table[row][col] = 'x';

        FLG = true;

        for (int tmp_row = row - 1; tmp_row >= 0; tmp_row--) {
          if (ans_table[tmp_row][col] == 'o') {
            FLG = false;
            break;
          }
          if (next_info.tmp_table[tmp_row][col] == 'o') {
            next_info.tmp_table[tmp_row][col] = 'x';
          } else {
            break;
          }
        }

        if (FLG) {
          for (int tmp_col = col + 1; tmp_col <= N - 1; tmp_col++) {
            if (ans_table[row][tmp_col] == 'o') {
              FLG = false;
              break;
            }
            if (next_info.tmp_table[row][tmp_col] == 'o') {
              next_info.tmp_table[row][tmp_col] = 'x';
            } else {
              break;
            }
          }
        }

        if (FLG) {
          for (int tmp_row = row + 1; tmp_row <= N - 1; tmp_row++) {
            if (ans_table[tmp_row][col] == 'o') {
              FLG = false;
              break;
            }
            if (next_info.tmp_table[tmp_row][col] == 'o') {
              next_info.tmp_table[tmp_row][col] = 'x';
            } else {
              break;
            }
          }
        }

        if (FLG) {
          for (int tmp_col = col - 1; tmp_col >= 0; tmp_col--) {
            if (ans_table[row][tmp_col] == 'o') {
              FLG = false;
              break;
            }
            if (next_info.tmp_table[row][tmp_col] == 'o') {
              next_info.tmp_table[row][tmp_col] = 'x';
            } else {
              break;
            }
          }
        }

        if (FLG) {
          next_info.count = Q.front().count + 1;
          Q.push(next_info);
        }
      }
    }

    Q.pop();
  }

  return 0;
}