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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int N;
vector<string> g;
int used[128], x1[128], x2[128], y1[128], y2[128];
vector<int> p;

vector<char> v;
bool bt() {

  for (int k = 0; k < p.size(); k++) {
    for (int i = y1[p[k]]; i <= y2[p[k]]; i++) {
      for (int j = x1[p[k]]; j <= x2[p[k]]; j++) {
        char c = g[i][j];
        if (c == '.')
          return false;
        if (find(p.begin(), p.end(), p[k]) > find(p.begin(), p.end(), c)) {
          return false;
        }
      }
    }
  }

  for (int k = 0; k < v.size(); k++) {
    if (!used[v[k]]) {
      used[v[k]] = true;
      p.push_back(v[k]);
      if (bt())
        return true;
      p.pop_back();
      used[v[k]] = false;
    }
  }
  return p.size() == v.size();
}
int main() {
  int T;

  cin >> T;
  while (T--) {
    int W, H;
    cin >> H >> W;
    vector<string> data(H);
    for (int i = 0; i < H; i++)
      cin >> data[i];

    g = data;

    int check[128];

    for (int i = 0; i < 128; i++)
      x1[i] = x2[i] = y1[i] = y2[i] = used[i] = check[i] = 0;

    for (int i = 0; i < 128; i++)
      x1[i] = y1[i] = 9999;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        char c = data[i][j];
        if (c != '.') {
          check[c] = true;
          x1[c] = min(j, x1[c]);
          x2[c] = max(j, x2[c]);
          y1[c] = min(i, y1[c]);
          y2[c] = max(i, y2[c]);
        }
      }
    }

    v.clear();
    p.clear();

    for (int i = 0; i < 128; i++)
      if (check[i])
        v.push_back(i);

    cout << (bt() ? "SAFE" : "SUSPICIOUS") << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
  int T;

  cin >> T;
  while (T--) {
    int W, H;
    cin >> H >> W;
    map<char, int> x1, x2, y1, y2;
    vector<string> data(H);
    for (int i = 0; i < H; i++)
      cin >> data[i];
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        char c = data[i][j];
        if (c != '.') {
          if (x1.find(c) == x1.end())
            x1[c] = j;
          else
            x1[c] = min(j, x1[c]);
          x2[c] = max(j, x2[c]);
          if (y1.find(c) == y1.end())
            y1[c] = i;
          else
            y1[c] = min(i, y1[c]);

          y2[c] = max(i, y2[c]);
        }
      }
    }
    vector<char> v;
    for (map<char, int>::iterator it = x1.begin(); it != x1.end(); ++it)
      v.push_back(it->first);
    bool safe = false;
    sort(v.begin(), v.end());
    do {
      map<char, int> memo;
      for (int i = 0; i < v.size(); i++)
        memo[v[i]] = i;
      bool ok = true;
      for (int k = 0; k < v.size(); k++) {
        for (int i = y1[v[k]]; i <= y2[v[k]]; i++) {
          for (int j = x1[v[k]]; j <= x2[v[k]]; j++) {
            char c = data[i][j];
            if (c == '.') {
              ok = false;
            }
            if (memo[c] < memo[v[k]]) {
              ok = false;
            }
          }
        }
      }
      safe |= ok;
    } while (next_permutation(v.begin(), v.end()));
    cout << (safe ? "SAFE" : "SUSPICIOUS") << endl;
  }
}