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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define all(c) c.begin(), c.end()
#define uni(c) c.erase(unique(all(c)), c.end())
#define pb push_back
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
int h, w;
vector<char> c;
map<char, PP> mp;
bool ret(vector<string> s, int bit, int now) {
  PP p = mp[c[now]];
  for (int i = p.first.first; i <= p.first.second; i++) {
    for (int j = p.second.first; j <= p.second.second; j++) {
      if (s[i][j] == c[now] || s[i][j] == '*') {
        s[i][j] = '*';
      } else {
        return false;
      }
    }
  }
  if (bit == (1 << c.size()) - 1)
    return true;

  for (int i = 0; i < c.size(); i++) {
    if (!((1 << i) & bit) && ret(s, bit | (1 << i), i))
      return true;
  }
  return false;
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    cin >> h >> w;
    vector<string> s(h);
    c.clear();
    for (int i = 0; i < h; i++) {
      cin >> s[i];
      for (int j = 0; j < w; j++) {
        if (find(all(c), s[i][j]) == c.end()) {
          c.pb(s[i][j]);
          mp[s[i][j]] = PP(P(i, i), P(j, j));
        } else {
          mp[s[i][j]] = PP(P(min(i, mp[s[i][j]].first.first),
                             max(i, mp[s[i][j]].first.second)),
                           P(min(j, mp[s[i][j]].second.first),
                             max(j, mp[s[i][j]].second.second)));
        }
      }
    }
    sort(all(c));
    bool flag = false;
    for (int i = 0; i < c.size(); i++) {
      if (ret(s, 1 << i, i)) {
        cout << "SAFE" << endl;
        flag = true;
        break;
      }
    }
    if (!flag)
      cout << "SUSPICIOUS" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define all(c) c.begin(), c.end()
#define uni(c) c.erase(unique(all(c)), c.end())
#define pb push_back
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
int h, w;
vector<char> c;
map<char, PP> mp;
bool ret(int now, vector<string> &s) {
  if (now == c.size())
    return true;
  /*int x1=100,x2=0,y1=100,y2=0;
  for(int i=0;i<h;i++){
          for(int j=0;j<w;j++){
                  if(s[i][j]==c[now]){
                          x1=min(x1,j);
                          x2=max(x2,j);
                          y1=min(y1,i);
                          y2=max(y2,i);
                  }
          }
  }*/
  PP p = mp[c[now]];
  for (int i = p.first.first; i <= p.first.second; i++) {
    for (int j = p.second.first; j <= p.second.second; j++) {
      if (s[i][j] == c[now] || s[i][j] == '*') {
        s[i][j] = '*';
      } else {
        return false;
      }
    }
  }
  return ret(now + 1, s);
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    cin >> h >> w;
    vector<string> s(h);
    c.clear();
    for (int i = 0; i < h; i++) {
      cin >> s[i];
      for (int j = 0; j < w; j++) {
        if (find(all(c), s[i][j]) == c.end()) {
          c.pb(s[i][j]);
          mp[s[i][j]] = PP(P(i, i), P(j, j));
        } else {
          mp[s[i][j]] = PP(P(min(i, mp[s[i][j]].first.first),
                             max(i, mp[s[i][j]].first.second)),
                           P(min(j, mp[s[i][j]].second.first),
                             max(j, mp[s[i][j]].second.second)));
        }
      }
    }
    sort(all(c));
    uni(c);
    bool flag = false;
    do {
      vector<string> temp = s;
      if (ret(0, temp)) {
        flag = true;
        break;
      }
    } while (next_permutation(all(c)));
    if (flag) {
      cout << "SAFE" << endl;
    } else
      cout << "SUSPICIOUS" << endl;
  }
  return 0;
}