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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 110

bool solve() {
  int h, w;
  char c[SIZE][SIZE];
  string s;
  bool f[26] = {};

  scanf("%d%d", &h, &w);

  if (h == 0)
    return false;

  for (int i = 0; i < h; i++) {
    scanf("%s", c[i]);

    for (int j = 0; j < w; j++) {
      if (c[i][j] != '.' && f[c[i][j] - 'A'] == false) {
        f[c[i][j] - 'A'] = true;
        s += c[i][j];
      }
    }
  }

  sort(s.begin(), s.end());

  do {
    char copy_c[SIZE][SIZE];
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        copy_c[i][j] = c[i][j];
      }
    }

    bool check = true;

    for (int p = 0; p < s.size(); p++) {
      int min_h = INF, max_h = -1, min_w = INF, max_w = -1;

      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (copy_c[i][j] == s[p]) {
            min_h = min(min_h, i);
            max_h = max(max_h, i);
            min_w = min(min_w, j);
            max_w = max(max_w, j);
          }
        }
      }

      for (int i = min_h; i <= max_h; i++) {
        for (int j = min_w; j <= max_w; j++) {
          if (copy_c[i][j] != s[p] && copy_c[i][j] != '#')
            check = false;
          copy_c[i][j] = '#';
        }
      }
    }

    if (check) {
      puts("SAFE");
      return true;
    }

  } while (next_permutation(s.begin(), s.end()));

  puts("SUSPICIOUS");
  return true;
}

int main() {
  int t;

  scanf("%d", &t);

  while (t--)
    solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 110

bool solve() {
  int h, w;
  char c[SIZE][SIZE];
  string s;
  bool f[26] = {};

  scanf("%d%d", &h, &w);

  if (h == 0)
    return false;

  for (int i = 0; i < h; i++) {
    scanf("%s", c[i]);

    for (int j = 0; j < w; j++) {
      if (f[c[i][j] - 'A'] == false) {
        f[c[i][j] - 'A'] = true;
        s += c[i][j];
      }
    }
  }

  sort(s.begin(), s.end());

  do {
    char copy_c[SIZE][SIZE];
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        copy_c[i][j] = c[i][j];
      }
    }

    bool check = true;

    for (int p = 0; p < s.size(); p++) {
      int min_h = INF, max_h = -1, min_w = INF, max_w = -1;

      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (copy_c[i][j] == s[p]) {
            min_h = min(min_h, i);
            max_h = max(max_h, i);
            min_w = min(min_w, j);
            max_w = max(max_w, j);
          }
        }
      }

      for (int i = min_h; i <= max_h; i++) {
        for (int j = min_w; j <= max_w; j++) {
          if (copy_c[i][j] != s[p] && copy_c[i][j] != '#')
            check = false;
          copy_c[i][j] = '#';
        }
      }
    }

    if (check) {
      puts("SAFE");
      return true;
    }

  } while (next_permutation(s.begin(), s.end()));

  puts("SUSPICIOUS");
  return true;
}

int main() {
  int t;

  scanf("%d", &t);

  while (t--)
    solve();

  return 0;
}