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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define N 64
using namespace std;

bool isrectangle(char c, map<char, int> &s, map<char, set<char>> &cont) {
  set<char>::iterator its = cont[c].begin();
  while (its != cont[c].end()) {
    if (*its == '.')
      return false;
    if (s[c] < s[*its])
      return false;
    ++its;
  }
  return true;
}

int main() {
  int T;
  cin >> T;
  for (int tc = 1; tc <= T; ++tc) {
    int h, w;
    bool suspicious = true;
    map<char, int> s;
    map<char, set<char>> cont;
    char M[N][N];
    vector<int> x;
    cin >> h >> w;

    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> M[i][j];
        if (isalpha(M[i][j])) {
          s[M[i][j]] = 0;
        }
      }
    }

    for (int i = 0; i < s.size(); ++i) {
      x.push_back(i);
    }

    map<char, int>::iterator itm = s.begin();
    while (itm != s.end()) {
      int mini = N, maxi = -1, minj = N, maxj = -1;
      for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
          if (M[i][j] == itm->first) {
            mini = min(mini, i);
            maxi = max(maxi, i);
            minj = min(minj, j);
            maxj = max(maxj, j);
          }
        }
      }
      for (int i = mini; i <= maxi; ++i) {
        for (int j = minj; j <= maxj; ++j) {
          if (M[i][j] != itm->first) {
            cont[itm->first].insert(M[i][j]);
          }
        }
      }
      ++itm;
    }

    do {
      bool ok = true;
      map<char, int>::iterator its = s.begin();
      for (int i = 0; i < s.size(); ++i) {
        its->second = x[i];
        ++its;
      }
      for (its = s.begin(); its != s.end(); ++its) {
        if (!isrectangle(its->first, s, cont)) {
          ok = false;
        }
      }
      if (ok) {
        suspicious = false;
      }
    } while (next_permutation(x.begin(), x.end()));

    if (suspicious) {
      cout << "SUSPICIOUS";
    } else {
      cout << "SAFE";
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define N 64
using namespace std;

bool isrectangle(char c, map<char, int> &s, char M[][N], int mini, int maxi,
                 int minj, int maxj) {
  for (int i = mini; i <= maxi; ++i) {
    for (int j = minj; j <= maxj; ++j) {
      if (!isalpha(M[i][j]))
        return false;
      if (s[c] < s[M[i][j]])
        return false;
    }
  }
  return true;
}

int main() {
  int T;
  cin >> T;
  for (int tc = 1; tc <= T; ++tc) {
    int h, w;
    bool suspicious = true;
    map<char, int> s;
    vector<int> xdepth;
    bool susp[256];
    char M[N][N];
    cin >> h >> w;

    for (int i = 0; i < 256; ++i) {
      susp[i] = true;
    }
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> M[i][j];
        if (isalpha(M[i][j])) {
          s[M[i][j]] = 0;
        }
      }
    }

    xdepth.resize(s.size());
    for (int i = 0; i < s.size(); ++i) {
      xdepth[i] = i;
    }

    do {
      bool ok = true;
      map<char, int>::iterator its = s.begin();
      for (int i = 0; i < xdepth.size(); ++i) {
        its->second = xdepth[i];
        ++its;
      }
      for (its = s.begin(); its != s.end(); ++its) {
        int mini = N, maxi = -1, minj = N, maxj = -1;
        for (int i = 0; i < h; ++i) {
          for (int j = 0; j < w; ++j) {
            if (M[i][j] == its->first) {
              mini = min(mini, i);
              maxi = max(maxi, i);
              minj = min(minj, j);
              maxj = max(maxj, j);
            }
          }
        }
        if (!isrectangle(its->first, s, M, mini, maxi, minj, maxj)) {
          ok = false;
        }
      }
      if (ok) {
        suspicious = false;
      }
    } while (next_permutation(xdepth.begin(), xdepth.end()));

    if (suspicious) {
      cout << "SUSPICIOUS";
    } else {
      cout << "SAFE";
    }
    cout << endl;
  }
  return 0;
}