Problem p01127 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

typedef pair<int, int> P;
int h, w;
bool ans;

void debug(vector<string> s) {
  cout << "[debug]" << endl;
  for (int y = 0; y < h; y++) {
    cout << s[y] << endl;
  }
  cout << endl;
}

// ツ債カツ湘」ツづ?右ツ可コツづ個催?標ツづーツ陛板つキ
pair<P, P> get_size(const vector<string> &s, char c) {
  int max_x = 0, max_y = 0, min_x = 1e+8, min_y = 1e+8;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (s[y][x] == c) {
        max_x = max(max_x, x);
        max_y = max(max_y, y);
        min_x = min(min_x, x);
        min_y = min(min_y, y);
      }
    }
  }
  return pair<P, P>(P(min_x, min_y), P(max_x, max_y));
}

// ツ陳キツ陛サツ形ツつゥツ陳イツづ猟づゥ
bool is_rect(const vector<string> &s, char c) {
  pair<P, P> pp = get_size(s, c);

  int min_x = pp.first.first;
  int min_y = pp.first.second;
  int max_x = pp.second.first;
  int max_y = pp.second.second;
  // cout << "char c : " << c << " (" << min_x << "," << min_y << ") , (" <<
  // max_x << "," << max_y << ")" << endl;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (min_x <= x && min_y <= y && x <= max_x && y <= max_y) {
        if (s[y][x] == c || s[y][x] == '*') {
        } else {
          return false;
        }
      } else {
        if (s[y][x] == c)
          return false;
      }
    }
  }
  return true;
}

// ツ嘉猟閉ィツづーツ偲ヲツづィツ渉慊つュ
vector<string> remove(vector<string> s, char c) {
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (s[y][x] == c) {
        s[y][x] = '*';
      }
    }
  }
  return s;
}

// memo[i] : iツ氾板姪堋づ個嘉猟閉ィツづーツ陳イツづ猟つスツつゥツづ?つ、ツつゥ,
// vc[i] : iツ氾板姪堋づ個嘉猟閉ィツづーツ表ツつキツ閉カツ篠? s :
// w*hツづ個湘ウツ妥? cnt : ツ偲ヲツづィツ渉慊つ「ツつスツ嘉猟閉ィツづ個青?
void solve(vector<bool> memo, vector<char> vc, vector<string> s, int cnt) {
  if (ans)
    return;
  if (cnt == vc.size()) {
    ans = true;
    return;
  }

  for (int i = 0; i < vc.size(); i++) {
    if (memo[i])
      continue;
    // debug( s );
    char c = vc[i];
    if (is_rect(s, c)) {
      memo[i] = true;
      s = remove(s, c);
      // debug( s );
      solve(memo, vc, s, cnt + 1);
      memo[i] = false;
    }
  }
}

int main() {
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    cin >> w >> h;
    vector<string> s;
    for (int y = 0; y < h; y++) {
      string s_;
      cin >> s_;
      s.push_back(s_);
    }

    int f[256] = {0};
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        char c = s[y][x];
        if (c >= 'A' && c <= 'Z') {
          f[c] = 1;
        }
      }
    }
    vector<char> vc;
    for (char c = 'A'; c <= 'Z'; c++) {
      if (f[c]) {
        vc.push_back(c);
      }
    }

    // debug( s );
    if (vc.size() == 0) {
      cout << "SAFE" << endl;
    } else {
      ans = false;
      vector<bool> memo((int)vc.size(), false);
      solve(memo, vc, s, 0);
      if (ans) {
        cout << "SAFE" << endl;
      } else {
        cout << "SUSPICIOUS" << endl;
      }
    }
  }
}

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

typedef pair<int, int> P;
int h, w;
bool ans;

void debug(vector<string> s) {
  cout << "[debug]" << endl;
  for (int y = 0; y < h; y++) {
    cout << s[y] << endl;
  }
  cout << endl;
}

// ツ債カツ湘」ツづ?右ツ可コツづ個催?標ツづーツ陛板つキ
pair<P, P> get_size(const vector<string> &s, char c) {
  int max_x = 0, max_y = 0, min_x = 1e+8, min_y = 1e+8;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (s[y][x] == c) {
        max_x = max(max_x, x);
        max_y = max(max_y, y);
        min_x = min(min_x, x);
        min_y = min(min_y, y);
      }
    }
  }
  return pair<P, P>(P(min_x, min_y), P(max_x, max_y));
}

// ツ陳キツ陛サツ形ツつゥツ陳イツづ猟づゥ
bool is_rect(const vector<string> &s, char c) {
  pair<P, P> pp = get_size(s, c);

  int min_x = pp.first.first;
  int min_y = pp.first.second;
  int max_x = pp.second.first;
  int max_y = pp.second.second;
  // cout << "char c : " << c << " (" << min_x << "," << min_y << ") , (" <<
  // max_x << "," << max_y << ")" << endl;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (min_x <= x && min_y <= y && x <= max_x && y <= max_y) {
        if (s[y][x] == c || s[y][x] == '*') {
        } else {
          return false;
        }
      } else {
        if (s[y][x] == c)
          return false;
      }
    }
  }
  return true;
}

// ツ嘉猟閉ィツづーツ偲ヲツづィツ渉慊つュ
vector<string> remove(vector<string> s, char c) {
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (s[y][x] == c) {
        s[y][x] = '*';
      }
    }
  }
  return s;
}

// memo[i] : iツ氾板姪堋づ個嘉猟閉ィツづーツ陳イツづ猟つスツつゥツづ?つ、ツつゥ,
// vc[i] : iツ氾板姪堋づ個嘉猟閉ィツづーツ表ツつキツ閉カツ篠? s :
// w*hツづ個湘ウツ妥? cnt : ツ偲ヲツづィツ渉慊つ「ツつスツ嘉猟閉ィツづ個青?
void solve(vector<bool> memo, vector<char> vc, vector<string> s, int cnt) {
  if (ans)
    return;
  if (cnt == vc.size()) {
    ans = true;
    return;
  }

  for (int i = 0; i < vc.size(); i++) {
    if (memo[i])
      continue;
    // debug( s );
    char c = vc[i];
    if (is_rect(s, c)) {
      memo[i] = true;
      s = remove(s, c);
      // debug( s );
      solve(memo, vc, s, cnt + 1);
      memo[i] = false;
    }
  }
}

int main() {
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    cin >> h >> w;
    vector<string> s;
    for (int y = 0; y < h; y++) {
      string s_;
      cin >> s_;
      s.push_back(s_);
    }

    int f[256] = {0};
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        char c = s[y][x];
        if (c >= 'A' && c <= 'Z') {
          f[c] = 1;
        }
      }
    }
    vector<char> vc;
    for (char c = 'A'; c <= 'Z'; c++) {
      if (f[c]) {
        vc.push_back(c);
      }
    }

    // debug( s );
    if (vc.size() == 0) {
      cout << "SAFE" << endl;
    } else {
      ans = false;
      vector<bool> memo((int)vc.size(), false);
      solve(memo, vc, s, 0);
      if (ans) {
        cout << "SAFE" << endl;
      } else {
        cout << "SUSPICIOUS" << endl;
      }
    }
  }
}