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

#include <bits/stdc++.h>
#define GET_MACRO(_1, _2, _3, _4, _5, NAME, ...) NAME
#define pr(...) GET_MACRO(__VA_ARGS__, pr5, pr4, pr3, pr2, pr1)(__VA_ARGS__)
#define pr1(a) (#a) << "=" << (a)
#define pr2(a, b) pr1(a) << ", " << pr1(b)
#define pr3(a, b, c) pr2(a, b) << ", " << pr1(c)
#define pr4(a, b, c, d) pr3(a, b, c) << ", " << pr1(d)
#define pr5(a, b, c, d, e) pr4(a, b, c, d) << ", " << pr1(e)
#define int long long
#define double long double
using namespace std;
const int N = 100010;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
typedef pair<int, int> P;
typedef long long ll;
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
ostream &operator<<(ostream &o, P p) {
  return o << "(" << p.first << "," << p.second << ")";
}
istream &operator>>(istream &i, P &p) { return i >> p.first >> p.second; }
ostream &operator<<(ostream &o, vector<auto> &a) {
  int i = 0;
  for (auto t : a)
    o << (i++ ? " " : "") << t;
  return o;
}
istream &operator>>(istream &i, vector<auto> &a) {
  for (auto &t : a)
    i >> t;
  return i;
}
void prArr(auto a, string s = " ") {
  int i = 0;
  for (auto t : a)
    cout << (i++ ? s : "") << t;
  cout << endl;
}

typedef vector<int> vec;
typedef vector<vec> mat;

mat move(const mat a, const mat b) {
  int n = a.size();
  mat res(n, vec(n));

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int y = a[i][j] / n;
      int x = a[i][j] % n;
      res[i][j] = b[y][x];
    }
  return res;
}

int n;
int pos;
string str;
mat E;

mat bnf(mat);
int number();

mat repeat(mat res) {
  assert(str[pos++] == '(');
  mat x = bnf(E);
  assert(str[pos++] == ')');

  int num = number();
  while (num) {
    if (num % 2)
      res = move(res, x);
    x = move(x, x);
    num /= 2;
  }
  return res;
}

int number() {
  assert(isdigit(str[pos]));

  int res = 0;
  while (pos < (int)str.size() && isdigit(str[pos]))
    res = res * 10 + str[pos++] - '0';
  return res;
}

mat L() {
  assert(str[pos++] == 'L');

  mat res = E;
  int k = number() - 1;
  for (int i = 0; i < n - 1; i++)
    swap(res[k][n - 1 - i], res[k][n - 2 - i]);
  return res;
}

mat R() {
  assert(str[pos++] == 'R');

  mat res = E;
  int k = number() - 1;
  for (int i = 0; i < n - 1; i++)
    swap(res[k][i], res[k][i + 1]);
  return res;
};

mat U() {
  assert(str[pos++] == 'U');

  mat res = E;
  int k = number() - 1;
  for (int i = 0; i < n - 1; i++)
    swap(res[n - 1 - i][k], res[n - 2 - i][k]);
  return res;
};

mat D() {
  assert(str[pos++] == 'D');

  mat res = E;
  int k = number() - 1;
  for (int i = 0; i < n - 1; i++)
    swap(res[i][k], res[i + 1][k]);
  return res;
};

mat bnf(mat res) {
  while (pos < (int)str.size()) {
    char ch = str[pos];
    if (ch == '(')
      res = repeat(res);
    else if (ch == 'L')
      res = move(res, L());
    else if (ch == 'R')
      res = move(res, R());
    else if (ch == 'U')
      res = move(res, U());
    else if (ch == 'D')
      res = move(res, D());
    else
      break;
  }
  return res;
}

signed main() {
  int L;
  cin >> n >> L;
  cin >> str;

  E = mat(n, vec(n));

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      E[i][j] = i * n + j;

  mat mv = bnf(E);

  mat ans(n, vec(n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int x = mv[i][j] % n;
      int y = mv[i][j] / n;
      ans[y][x] = i * n + j;
    }

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      ans[i][j]++;

  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define GET_MACRO(_1, _2, _3, _4, _5, NAME, ...) NAME
#define pr(...) GET_MACRO(__VA_ARGS__, pr5, pr4, pr3, pr2, pr1)(__VA_ARGS__)
#define pr1(a) (#a) << "=" << (a)
#define pr2(a, b) pr1(a) << ", " << pr1(b)
#define pr3(a, b, c) pr2(a, b) << ", " << pr1(c)
#define pr4(a, b, c, d) pr3(a, b, c) << ", " << pr1(d)
#define pr5(a, b, c, d, e) pr4(a, b, c, d) << ", " << pr1(e)
#define int long long
#define double long double
using namespace std;
const int N = 100010;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
typedef pair<int, int> P;
typedef long long ll;
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
// ostream& operator<<(ostream& o,P p){return
// o<<"("<<p.first<<","<<p.second<<")";} istream& operator>>(istream& i,P
// &p){return i>>p.first>>p.second;} ostream& operator<<(ostream& o,vector<auto>
// &a){int i=0;for(auto t:a)o<<(i++?" ":"")<<t;return o;} istream&
// operator>>(istream& i,vector<auto> &a){for(auto &t:a)i>>t;return i;} void
// prArr(auto a,string s=" "){int i=0;for(auto
// t:a)cout<<(i++?s:"")<<t;cout<<endl;}

typedef vector<int> vec;
typedef vector<vec> mat;

mat move(const mat &a, const mat &b) {
  int n = a.size();
  mat res(n, vec(n));

  assert(a.size() == b.size());
  assert(a[0].size() == b[0].size());
  assert(a[0].size() == a.size());

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int y = a[i][j] / n;
      int x = a[i][j] % n;
      assert(y < (int)b.size() && x < (int)b[y].size());
      res[i][j] = b[y][x];
    }
  return res;
}

int n;
int pos;
string str;

mat bfs();
mat repeat();
int number();

mat repeat() {
  assert(str[pos] == '(');
  pos++;
  mat x = bfs();
  assert(str[pos] == ')');
  pos++;
  int num = number();

  mat res(n, vec(n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      res[i][j] = i * n + j;

  while (num) {
    if (num % 2)
      res = move(res, x);
    x = move(x, x);
    num /= 2;
  }
  return res;
}

int number() {
  assert(isdigit(str[pos]));
  int res = 0;
  while (pos < (int)str.size() && isdigit(str[pos]))
    res = res * 10 + str[pos++] - '0';
  return res;
}

void L(mat &A) {
  assert(str[pos++] == 'L');
  int k = number() - 1;
  for (int i = 0; i < n - 1; i++)
    swap(A[k][i], A[k][i + 1]);
}

void R(mat &A) {
  assert(str[pos++] == 'R');
  int k = number() - 1;
  for (int i = 0; i < n - 1; i++)
    swap(A[k][n - 1 - i], A[k][n - 2 - i]);
};

mat U(mat &A) {
  assert(str[pos++] == 'U');
  int k = number() - 1;
  for (int i = 0; i < n - 1; i++)
    swap(A[i][k], A[i + 1][k]);
};

mat D(mat &A) {
  assert(str[pos++] == 'D');
  int k = number() - 1;
  for (int i = 0; i < n - 1; i++)
    swap(A[n - 1 - i][k], A[n - 2 - i][k]);
};

mat bfs() {
  mat res(n, vec(n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      res[i][j] = i * n + j;

  while (pos < (int)str.size()) {
    char ch = str[pos];
    if (ch == '(')
      res = repeat();
    else if (ch == 'L')
      L(res);
    else if (ch == 'R')
      R(res);
    else if (ch == 'U')
      U(res);
    else if (ch == 'D')
      D(res);
    else
      break;
  }
  return res;
}

signed main() {
  int L;
  cin >> n >> L;
  cin >> str;

  mat ans = bfs();
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      ans[i][j]++;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (j)
        cout << " ";
      cout << ans[i][j];
    }
    cout << endl;
  }

  return 0;
}