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

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int i, j, k;
int m, n, coun, ans, jud = 1, s;
int town[11][11];
P start;
void dfs(int y, int x, int flag);

inline int bit(int n, int i) { return n | 1 << i; }

inline bool bitj(int n, int i) {
  if (n & 1 << i)
    return false;
  else
    return true;
}
int main() {
  while (cin >> m >> n && m && n) {
    ans = 0;
    coun = 1;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        cin >> town[i][j];
        if (town[i][j] == 2) {
          start = make_pair(i, j);
          s = coun;
        }
        if (town[i][j]) {
          town[i][j] = coun;
          coun++;
        }
      }
    }
    jud = 1;
    for (i = 0; i < coun; i++) {
      jud *= 2;
    }
    dfs(start.first, start.second, 1 << s);
    cout << ans << endl;
  }
}
void dfs(int y, int x, int flag) {
  /*if (start == make_pair(y, x)) {
          if (flag == jud - 1) {
                  ans++;
          }
          if (flag) {
                  return;
          }
  }*/
  for (i = x + 1; i <= m; i++) {
    if (town[y][i] && bitj(flag, town[y][i])) {
      dfs(y, i, bit(flag, town[y][i]));
      break;
    }
    if (town[y][i] == s && bit(flag, town[y][i]) == jud - 2) {
      ans++;
      break;
    }
  }
  for (i = x - 1; i > 0; i--) {
    if (town[y][i] && bitj(flag, town[y][i])) {
      dfs(y, i, bit(flag, town[y][i]));
      break;
    }
    if (town[y][i] == s && bit(flag, town[y][i]) == jud - 2) {
      ans++;
      break;
    }
  }
  for (i = y + 1; i <= n; i++) {
    if (town[i][x] && bitj(flag, town[i][x])) {
      dfs(i, x, bit(flag, town[i][x]));
      break;
    }
    if (town[i][x] == s && bit(flag, town[i][x]) == jud - 2) {
      ans++;
      break;
    }
  }
  for (i = y - 1; i > 0; i--) {
    if (town[i][x] && bitj(flag, town[i][x])) {
      dfs(i, x, bit(flag, town[i][x]));
      break;
    }
    if (town[i][x] == s && bit(flag, town[i][x]) == jud - 2) {
      ans++;
      break;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int i, j, k;
int m, n, coun, s;
int dp[25][1024 * 1024 * 16], town[11][11];
P p[25];
vector<int> g[25][4];
void bfs(int now, int flag);
int main() {
  while (cin >> m >> n && m && n) {
    coun = 1;
    memset(dp, 0, sizeof(dp));
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        cin >> town[i][j];
        if (town[i][j] == 2) {
          s = coun;
        }
        if (town[i][j]) {
          town[i][j] = coun;
          p[coun] = make_pair(i, j);
          coun++;
        }
      }
    }
    for (i = 1; i < coun; i++) {
      for (j = 1; j < coun; j++) {
        if (i != j) {
          if (p[i].first == p[j].first && i > j) {
            g[i][2].push_back(j);
          }
          if (p[i].first == p[j].first && i < j) {
            g[i][3].push_back(j);
          }
          if (p[i].second == p[j].second && i > j) {
            g[i][1].push_back(j);
          }
          if (p[i].second == p[j].second && i < j) {
            g[i][0].push_back(j);
          }
        }
      }
    }
    bfs(s, 0);
    j = 1;
    for (i = 0; i < coun - 1; i++)
      j = j * 2;
    cout << dp[s][j - 1] << endl;

    /*for (i = 1; i < coun; i++) {
            for (j = 0; j < 4; j++) {
                    for (k = 0; k < g[i][j].size(); k++) {
                            cout << g[i][j][k] << ' ';
                    }
            }
            cout << endl;
    }
    for (i = 1; i <= n; i++) {
            for (j = 1; j <= m; j++) {
                    cout << town[i][j] << ' ';
            }
            cout << endl;
    }*/
  }
}
void bfs(int start, int flag) {
  dp[start][flag] = 1;
  vector<P> que;
  que.push_back(make_pair(flag, start));
  while (!que.empty()) {
    // cout << que.size()<<' ';
    P pp;
    pp = que[0];
    start = pp.second;
    flag = pp.first;
    // cout << start << ' ' <<  static_cast<std::bitset<24> >(flag)  << endl;
    que.erase(que.begin());
    if (flag & 1 << (s - 1)) {
      continue;
    }
    //
    for (j = 0; j < g[start][3].size(); j++) {
      if (town[p[start].first][p[g[start][3][j]].second]) {
        if (flag & 1 << (town[p[start].first][p[g[start][3][j]].second] - 1)) {
          break;
        } else {
          int flag2 =
              flag | 1 << (town[p[start].first][p[g[start][3][j]].second] - 1);
          if (dp[town[p[start].first][p[g[start][3][j]].second]][flag2] == 0) {
            que.push_back(make_pair(
                flag2, town[p[start].first][p[g[start][3][j]].second]));
          }
          dp[town[p[start].first][p[g[start][3][j]].second]][flag2] +=
              dp[town[p[start].first][p[start].second]][flag];
        }
      }
    }
    //
    /*for (j = p[start].second; j <= m; j++) {
            if (town[p[start].first][j]&&j!=p[start].second) {
                    if (flag & 1 << (town[p[start].first][j]-1)) {
                            break;
                    }
                    else {
                            int flag2 = flag | 1 << (town[p[start].first][j] -
    1); if (dp[town[p[start].first][j]][flag2] == 0) {
                                    que.push_back(make_pair(flag2,
    town[p[start].first][j]));
                            }
                            dp[town[p[start].first][j]][flag2] +=
    dp[town[p[start].first][p[start].second]][flag];
                    }
            }
    }*/

    for (j = g[start][2].size() - 1; j >= 0; j--) {
      if (town[p[start].first][p[g[start][2][j]].second]) {
        if (flag & 1 << (town[p[start].first][p[g[start][2][j]].second] - 1)) {
          break;
        } else {
          int flag2 =
              flag | 1 << (town[p[start].first][p[g[start][2][j]].second] - 1);
          if (dp[town[p[start].first][p[g[start][2][j]].second]][flag2] == 0) {
            que.push_back(make_pair(
                flag2, town[p[start].first][p[g[start][2][j]].second]));
          }
          dp[town[p[start].first][p[g[start][2][j]].second]][flag2] +=
              dp[town[p[start].first][p[start].second]][flag];
        }
      }
    }

    // for (j = p[start].second; j >= 1; j--) {
    //	if (town[p[start].first][j]&&j != p[start].second) {
    //		if (flag & 1 << (town[p[start].first][j]- 1)) {
    //			break;
    //		}
    //		else {
    //			int flag2 = flag | 1 << (town[p[start].first][j] - 1);
    //			if (dp[town[p[start].first][j]][flag2] == 0) {
    //				que.push_back(make_pair(flag2,
    //town[p[start].first][j]));
    //				//	cout << 2 << endl;
    //			}
    //			dp[town[p[start].first][j]][flag2] +=
    //dp[town[p[start].first][p[start].second]][flag];
    //		}
    //	}
    // }
    for (j = 0; j < g[start][0].size(); j++) {
      if (town[p[g[start][0][j]].first][p[start].second]) {
        if (flag & 1 << (town[p[g[start][0][j]].first][p[start].second] - 1)) {
          break;
        } else {
          int flag2 =
              flag | 1 << (town[p[g[start][0][j]].first][p[start].second] - 1);
          if (dp[town[p[g[start][0][j]].first][p[start].second]][flag2] == 0) {
            que.push_back(make_pair(
                flag2, town[p[g[start][0][j]].first][p[start].second]));
            //	cout << 3 << endl;
          }
          dp[town[p[g[start][0][j]].first][p[start].second]][flag2] +=
              dp[town[p[start].first][p[start].second]][flag];
        }
      }
    }

    // for (j = p[start].first; j <= n; j++) {
    //	if (town[j][p[start].second]&& j != p[start].first) {
    //		if (flag & 1 << (town[j][p[start].second] - 1)) {
    //			break;
    //		}
    //		else {
    //			int flag2 = flag | 1 << (town[j][p[start].second] - 1);
    //			if (dp[town[j][p[start].second]][flag2] == 0) {
    //				que.push_back(make_pair(flag2,
    //town[j][p[start].second]));
    //				//	cout << 3 << endl;
    //			}
    //			dp[town[j][p[start].second]][flag2] +=
    //dp[town[p[start].first][p[start].second]][flag];
    //		}
    //	}
    // }
    for (j = g[start][1].size() - 1; j >= 0; j--) {
      if (town[p[g[start][1][j]].first][p[start].second]) {
        if (flag & 1 << (town[p[g[start][1][j]].first][p[start].second] - 1)) {
          break;
        } else {
          int flag2 =
              flag | 1 << (town[p[g[start][1][j]].first][p[start].second] - 1);
          if (dp[town[p[g[start][1][j]].first][p[start].second]][flag2] == 0) {
            que.push_back(make_pair(
                flag2, town[p[g[start][1][j]].first][p[start].second]));
            //	cout << 3 << endl;
          }
          dp[town[p[g[start][1][j]].first][p[start].second]][flag2] +=
              dp[town[p[start].first][p[start].second]][flag];
        }
      }
    }
    // for (j = p[start].first; j >= 1; j--) {
    //	if (town[j][p[start].second] && j != p[start].first) {
    //		if (flag & 1 << (town[j][p[start].second] - 1)) {
    //			break;
    //		}
    //		else {
    //			int flag2 = flag | 1 << (town[j][p[start].second] - 1);
    //			if (dp[town[j][p[start].second]][flag2] == 0) {
    //				que.push_back(make_pair(flag2,
    //town[j][p[start].second]));
    //				//cout << 4 << endl;
    //				//cout << j << ' ' << flag2 << ' ' << p[start].first <<
    //endl;
    //			}
    //			dp[town[j][p[start].second]][flag2] +=
    //dp[town[p[start].first][p[start].second]][flag];
    //		}
    //	}
    // }
  }
}