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

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;

void pr(mat a) {
  int h = a.size(), w = a[0].size();
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << a[i][j];
    }
    cout << endl;
  }
  cout << endl;
}

mat cut(mat a) {

  int h = a.size(), w = a[0].size();
  int ay = 1e9, ax = 1e9, by = 0, bx = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (a[i][j] != 0) {
        ay = min(ay, i);
        ax = min(ax, j);
        by = max(by, i);
        bx = max(bx, j);
      }

  mat res = mat(by - ay + 1, vec(bx - ax + 1));
  for (int y = ay; y <= by; y++)
    for (int x = ax; x <= bx; x++)
      res[y - ay][x - ax] = a[y][x];
  return res;
}

int main() {
  int Tc;
  cin >> Tc;
  cin.ignore();
  while (Tc--) {

    int F;
    string s;
    getline(cin, s);

    F = s.size() + 2;
    int py = F, px = F;
    mat A(F + F, vec(F + F, 0));
    A[py][px] = 1;
    for (int i = 0; i < (int)s.size(); i++) {
      if (s[i] == 'a')
        px++;
      if (s[i] == 'b')
        py--;
      if (s[i] == 'c')
        py--, px--;
      if (s[i] == 'd')
        px--;
      if (s[i] == 'e')
        py++;
      if (s[i] == 'f')
        py++, px++;
      A[py][px] = 1;
    }
    A = cut(A);

    getline(cin, s);
    bool ans = false;
    for (int T = 0; T < 6; T++) {
      F = s.size() + 2;
      mat B(F + F, vec(F + F, 0));
      py = F, px = F;
      B[py][px] = 1;

      for (int i = 0; i < (int)s.size(); i++) {
        if (s[i] == 'a')
          px++, s[i] = 'b';
        else if (s[i] == 'b')
          py--, s[i] = 'c';
        else if (s[i] == 'c')
          py--, px--, s[i] = 'd';
        else if (s[i] == 'd')
          px--, s[i] = 'e';
        else if (s[i] == 'e')
          py++, s[i] = 'f';
        else if (s[i] == 'f')
          py++, px++, s[i] = 'a';
        B[py][px] = 1;
      }

      B = cut(B);
      if (A == B)
        ans = true;
    }
    cout << (ans ? "true" : "false") << endl;
    string tmp;
    getline(cin, tmp);
    assert(tmp == "-");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;

void pr(mat a) {
  int h = a.size(), w = a[0].size();
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << a[i][j];
    }
    cout << endl;
  }
  cout << endl;
}

mat cut(mat a) {

  int h = a.size(), w = a[0].size();
  int ay = 1e9, ax = 1e9, by = 0, bx = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (a[i][j] != 0) {
        ay = min(ay, i);
        ax = min(ax, j);
        by = max(by, i);
        bx = max(bx, j);
      }

  mat res = mat(by - ay + 1, vec(bx - ax + 1));
  for (int y = ay; y <= by; y++)
    for (int x = ax; x <= bx; x++)
      res[y - ay][x - ax] = a[y][x];
  return res;
}

int main() {
  int Tc;
  cin >> Tc;
  while (Tc--) {

    int F;
    string s;
    cin >> s;
    F = s.size() + 2;
    int py = F, px = F;
    mat A(F + F, vec(F + F, 0));
    A[py][px] = 1;
    for (int i = 0; i < (int)s.size(); i++) {
      if (s[i] == 'a')
        px++;
      if (s[i] == 'b')
        py--;
      if (s[i] == 'c')
        py--, px--;
      if (s[i] == 'd')
        px--;
      if (s[i] == 'e')
        py++;
      if (s[i] == 'f')
        py++, px++;
      A[py][px] = 1;
    }
    A = cut(A);

    cin >> s;
    bool ans = false;
    for (int T = 0; T < 6; T++) {
      F = s.size() + 2;
      mat B(F + F, vec(F + F, 0));
      py = F, px = F;
      B[py][px] = 1;

      for (int i = 0; i < (int)s.size(); i++) {
        if (s[i] == 'a')
          px++, s[i] = 'b';
        else if (s[i] == 'b')
          py--, s[i] = 'c';
        else if (s[i] == 'c')
          py--, px--, s[i] = 'd';
        else if (s[i] == 'd')
          px--, s[i] = 'e';
        else if (s[i] == 'e')
          py++, s[i] = 'f';
        else if (s[i] == 'f')
          py++, px++, s[i] = 'a';
        B[py][px] = 1;
      }

      B = cut(B);
      if (A == B)
        ans = true;
    }
    cout << (ans ? "true" : "false") << endl;
    string tmp;
    cin >> tmp;
    assert(tmp == "-");
  }
  return 0;
}