Problem p00471 (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 base_map[10][10], house_index[10][10];
int H, W, finish_state;
int church_row, church_col;
int POW[24], diff_row[4] = {-1, 0, 0, 1}, diff_col[4] = {0, -1, 1, 0};

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

int ans;

void recursive(int state, int row, int col) {

  if (state == finish_state) {
    if (row == church_row || col == church_col) {
      ans++;
    }
    return;
  }

  int next_row, next_col, next_state;

  for (int i = 0; i < 4; i++) {
    next_row = row + diff_row[i];
    next_col = col + diff_col[i];

    while (rangeCheck(next_row, next_col) == true &&
           ((base_map[next_row][next_col] != 1) ||
            ((state & (1 << house_index[next_row][next_col])) != 0))) {
      next_row += diff_row[i];
      next_col += diff_col[i];
    }

    if (!rangeCheck(next_row, next_col))
      continue;

    next_state = state + POW[house_index[next_row][next_col]];
    recursive(next_state, next_row, next_col);
  }
}

void func() {

  for (int row = 0; row < H; row++) {
    for (int col = 0; col < W; col++) {
      house_index[row][col] = -1;
    }
  }

  int index = 0;
  for (int row = 0; row < H; row++) {
    for (int col = 0; col < W; col++) {
      scanf("%d", &base_map[row][col]);
      if (base_map[row][col] == 1) {
        house_index[row][col] = index++;
      } else if (base_map[row][col] == 2) {
        church_row = row;
        church_col = col;
      }
    }
  }

  ans = 0, finish_state = POW[index] - 1;

  recursive(0, church_row, church_col);

  printf("%d\\n", ans);
}

int main() {

  for (int i = 0; i < 24; i++)
    POW[i] = pow(2, i);

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

    func();
  }

  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 <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;

struct Info {
  Info(int arg_state, int arg_row, int arg_col) {
    state = arg_state;
    row = arg_row;
    col = arg_col;
  }
  int state, row, col;
};

int base_map[10][10], house_index[10][10];
int H, W;
int POW[24], diff_row[4] = {-1, 0, 0, 1}, diff_col[4] = {0, -1, 1, 0};

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

queue<Info> Q;

void func() {

  for (int row = 0; row < H; row++) {
    for (int col = 0; col < W; col++) {
      house_index[row][col] = -1;
    }
  }

  int church_row, church_col;

  int index = 0;
  for (int row = 0; row < H; row++) {
    for (int col = 0; col < W; col++) {
      scanf("%d", &base_map[row][col]);
      if (base_map[row][col] == 1) {
        house_index[row][col] = index++;
      } else if (base_map[row][col] == 2) {
        church_row = row;
        church_col = col;
      }
    }
  }

  int ans = 0, finish_state = POW[index] - 1;

  Q.push(Info(0, church_row, church_col));

  int next_row, next_col, next_state;

  while (!Q.empty()) {

    if (Q.front().state == finish_state) {
      if (Q.front().row == church_row || Q.front().col == church_col) {
        ans++;
      }
      Q.pop();
    } else {

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

        while (rangeCheck(next_row, next_col) == true &&
               ((base_map[next_row][next_col] != 1) ||
                ((Q.front().state & (1 << house_index[next_row][next_col])) !=
                 0))) {
          next_row += diff_row[i];
          next_col += diff_col[i];
        }

        if (!rangeCheck(next_row, next_col))
          continue;

        next_state = Q.front().state + POW[house_index[next_row][next_col]];
        Q.push(Info(next_state, next_row, next_col));
      }
      Q.pop();
    }
  }

  printf("%d\\n", ans);
}

int main() {

  for (int i = 0; i < 24; i++)
    POW[i] = pow(2, i);

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

    func();
  }

  return 0;
}