Problem p01416 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int, int> P;
string mp[501];
int m, n, used[30];
vector<P> a[30];

int saiki(int);

bool check_x(int y, int x1, int x2) {
  for (int i = x1 + 1; i < x2; i++)
    if (mp[y][i] != '.' && !saiki(mp[y][i] - 'A'))
      return 0;
  return 1;
}

bool check_y(int y1, int y2, int x) {
  for (int i = y1 + 1; i < y2; i++)
    if (mp[i][x] != '.' && !saiki(mp[i][x] - 'A'))
      return 0;
  return 1;
}

int saiki(int ch) {
  if (ch < 0)
    return 0;
  int x1 = a[ch][0].x, x2 = a[ch][1].x;
  int y1 = a[ch][0].y, y2 = a[ch][1].y;
  int res = 0;
  if (used[ch] || mp[y1][x1] == '.')
    return 0;
  used[ch] = 1;

  if (x1 == x2 && y2 - y1 != 1)
    res = check_y(y1, y2, x1);
  if (y1 == y2 && x2 - x1 != 1)
    res = check_x(y1, x1, x2);
  if (x1 != x2 && y1 != y2) {
    if (x1 < x2) {
      res = check_x(y1, x1, x2 + 1) & check_y(y1, y2, x2);
      res |= check_y(y1, y2 + 1, x1) & check_x(y2, x1, x2);
    } else {
      swap(x1, x2);
      res = check_x(y1, x1 - 1, x2) & check_y(y1, y2, x1);
      res |= check_y(y1, y2, x2) & check_x(y2, x1, x2 + 1);
      swap(x1, x2);
    }
  }

  if (res)
    mp[y1][x1] = mp[y2][x2] = '.';
  used[ch] = 0;
  return res;
}

int main() {

  cin >> m >> n;
  for (int i = 0; i < m; i++)
    cin >> mp[i];

  int ans = 0;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      if (mp[i][j] != '.')
        a[mp[i][j] - 'A'].push_back(P(j, i)), ans++;

  int flg = 1;
  while (flg) {
    flg = 0;
    for (int i = 0; i < 30; i++)
      if (a[i].size() && saiki(i))
        a[i].clear(), flg = 1;
  }

  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      if (mp[i][j] != '.')
        ans--;

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int, int> P;
string mp[501];
int m, n, used[30];
vector<P> a[30];

int saiki(int);

bool check_x(int y, int x1, int x2) {
  for (int i = x1 + 1; i < x2; i++)
    if (mp[y][i] != '.' && !saiki(mp[y][i] - 'A'))
      return 0;
  return 1;
}

bool check_y(int y1, int y2, int x) {
  for (int i = y1 + 1; i < y2; i++)
    if (mp[i][x] != '.' && !saiki(mp[i][x] - 'A'))
      return 0;
  return 1;
}

void out() {
  //  for(int i=0;i<m;i++)cout<<mp[i]<<endl;
}

int saiki(int ch) {
  int x1 = a[ch][0].x, x2 = a[ch][1].x;
  int y1 = a[ch][0].y, y2 = a[ch][1].y;
  int res = 0;
  if (used[ch] || mp[y1][x1] == '.')
    return 0;
  used[ch] = 1;

  if (x1 == x2 && y2 - y1 != 1)
    res = check_y(y1, y2, x1);
  if (y1 == y2 && x2 - x1 != 1)
    res = check_x(y1, x1, x2);
  if (x1 != x2 && y1 != y2) {
    if (x1 < x2) {
      res = check_x(y1, x1, x2 + 1) & check_y(y1, y2, x2);
      res |= check_y(y1, y2 + 1, x1) & check_x(y2, x1, x2);
    } else {
      swap(x1, x2);
      res = check_x(y1, x1 - 1, x2) & check_y(y1, y2, x1);
      res |= check_y(y1, y2, x2) & check_x(y2, x1, x2 + 1);
      swap(x1, x2);
    }
  }

  if (res)
    mp[y1][x1] = mp[y2][x2] = '.';
  used[ch] = 0;
  return res;
}

int main() {

  cin >> m >> n;
  for (int i = 0; i < m; i++)
    cin >> mp[i];
  for (int i = 0; i < m; i++)
    a[i].clear();
  for (int i = 0; i < 30; i++)
    used[i] = 0;

  int ans = 0;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      if (mp[i][j] != '.')
        a[mp[i][j] - 'A'].push_back(P(j, i)), ans++;

  int flg = 1;
  while (flg) {
    flg = 0;
    for (int i = 0; i < 30; i++)
      if (a[i].size() && saiki(i))
        a[i].clear(), flg = 1;
  }
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      if (mp[i][j] != '.')
        ans--;

  cout << ans << endl;
  return 0;
}