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

#include <bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
string str[100];
int h, w;
int num;
bool flag;
bool used[100][100];
char moji[100];
int vx[4] = {1, 0, -1, 0};
int vy[4] = {0, 1, 0, -1};
void dfs(int Bit, int depth) {
  if (Bit == ((1 << num) - 1)) {
    flag = true;
    return;
  }
  for (int i = 0; i < num; i++) {
    if (!(Bit & (1 << i))) {
      bool used2[100][100] = {};
      int lemi = 100, lema = 0, rimi = 100, rima = 0;
      for (int j = 0; j < h; j++) {
        for (int k = 0; k < w; k++) {
          if (str[j][k] == moji[i]) {
            used2[j][k] = true;
            used[j][k] = true;
            lemi = min(lemi, k);
            lema = max(lema, k);
            rima = max(rima, j);
            rimi = min(rimi, j);
          }
        }
      }
      bool fll = true;
      for (int j = rimi; j <= rima; j++) {
        for (int k = lemi; k <= lema; k++) {
          if (!used[j][k]) {
            fll = false;
          }
        }
      }
      if (fll)
        dfs(Bit + (1 << i), depth + 1);
      for (int j = rimi; j <= rima; j++) {
        for (int k = lemi; k <= lema; k++) {
          if (used2[j][k]) {
            used[j][k] = false;
          }
        }
      }
    }
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> h >> w;
    num = 0;
    for (int i = 0; i < 55; i++)
      for (int j = 0; j < 55; j++)
        used[i][j] = false;
    flag = false;
    bool alfa[26] = {};
    for (int i = 0; i < h; i++) {
      cin >> str[i];
      for (int j = 0; j < w; j++) {
        alfa[str[i][j] - 'A'] = true;
      }
    }
    int cnt = 0;
    for (int i = 0; i < 26; i++) {
      if (alfa[i]) {
        num++;
        moji[cnt] = ((char)(i + 'A'));
        cnt++;
      }
    }
    dfs(0, 0);
    if (flag)
      cout << "SAFE" << endl;
    else
      cout << "SUSPICIOUS" << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
string str[60];
int h, w;
int num;
bool flag;
bool used[55][55];
char moji[10];
int vx[4] = {1, 0, -1, 0};
int vy[4] = {0, 1, 0, -1};
void dfs(int Bit, int depth) {
  if (depth == ((1 << num) - 1)) {
    flag = true;
    return;
  }
  for (int i = 0; i < num; i++) {
    if (!(Bit & (1 << i))) {
      bool used2[55][55] = {};
      for (int j = 0; j < h; j++) {
        for (int k = 0; k < w; k++) {
          if (moji[i] == str[j][k]) {
            queue<pair<int, int>> que;
            que.push(mp(j, k));
            used2[j][k] = true;
            used[j][k] = true;
            while (!(que.empty())) {
              pair<int, int> now = que.front();
              que.pop();
              for (int l = 0; l < 4; l++) {
                int x = now.F + vx[l], y = now.S + vy[l];
                if (x >= 0 && x < h && y >= 0 && y < w && (!used2[x][y])) {
                  que.push(mp(x, y));
                  if (used[x][y] == false)
                    used2[x][y] = true;
                  used[x][y] = true;
                }
              }
            }
          }
        }
      }
      int lemi = 100, lema = 0, rimi = 100, rima = 0;
      for (int j = 0; i < h; i++) {
        for (int k = 0; k < w; k++) {
          if (used2[j][k]) {
            lemi = min(lemi, k);
            lema = max(lema, k);
            rima = max(rima, j);
            rimi = max(rimi, j);
          }
        }
      }
      for (int j = rimi; j <= rima; j++) {
        for (int k = lemi; k <= lema; k++) {
          if (used[j][k])
            return;
        }
      }
      dfs(Bit + (1 << i), depth + 1);
      for (int j = 0; j < h; j++) {
        for (int k = 0; k < w; k++) {
          if (used2[j][k]) {
            used[j][k] = false;
          }
        }
      }
    }
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> h >> w;
    num = 0;
    for (int i = 0; i < 55; i++)
      for (int j = 0; j < 55; j++)
        used[i][j] = false;
    flag = false;
    bool alfa[26] = {};
    for (int i = 0; i < h; i++) {
      cin >> str[i];
      for (int j = 0; j < w; j++) {
        alfa[str[i][j] - 'A'] = true;
      }
    }
    int cnt = 0;
    for (int i = 0; i < 26; i++) {
      num += alfa[i];
      if (alfa[i]) {
        moji[cnt] = (char)(i + 'A');
        cnt++;
      }
    }
    dfs(0, 0);
    if (flag)
      cout << "SAFE" << endl;
    else
      cout << "SUSPICIOUS" << endl;
  }
  return 0;
}