Problem p01007 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m;
int t[15][15];
vector<int> v[15];

void vrotate(int r, int c, int size, int angle) {
  if (angle == 0 || angle == 360)
    return;

  memset(t, 0, sizeof(t));

  rep(i, angle / 90) {
    int col = c;
    for (int i = r + size - 1; i >= r; i--, col++) {
      REP(j, c, c + size) { t[r + j - c][col] = v[i][j]; }
    }

    REP(i, r, r + size) {
      REP(j, c, c + size) { v[i][j] = t[i][j]; }
    }
  }
}

void reversal(int r, int c, int size) {
  REP(i, r, min(n, r + size)) {
    REP(j, c, min(n, c + size)) { v[i][j] = !v[i][j]; }
  }
}

void leftshift(int r) {
  int t = v[r][0];
  rep(i, n) { v[r][i] = v[r][i + 1]; }
  v[r][n - 1] = t;
  // rotate(v[r].begin(),v[r].begin()+1,v[r].end());
}

void rightshift(int r) {
  int t = v[r][n - 1];
  for (int i = n - 1; i >= 1; i--) {
    v[r][i] = v[r][i - 1];
  }
  v[r][0] = t;
  // rotate(v[r].rbegin(),v[r].rbegin()+1,v[r].rend());
}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool used[15][15];

bool can(int y, int x) {
  if (0 <= y && y < n && 0 <= x && x < n)
    return true;
  return false;
}

void islandreversal(int r, int c) {
  queue<P> que;
  que.push(mp(r, c));

  memset(used, 0, sizeof(used));
  used[r][c] = true;
  int t = v[r][c];
  while (que.size()) {
    P p = que.front();
    que.pop();
    int y = p.first;
    int x = p.second;

    if (v[y][x] == t) {
      v[y][x] = !v[y][x];
    }

    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];

      if (can(ny, nx) && !used[ny][nx] && v[ny][nx] == t) {
        que.push(mp(ny, nx));
        used[ny][nx] = true;
      }
    }
  }
}

int main() {
  cin >> n >> m;

  memset(v, 0, sizeof(v));
  rep(i, n) {
    v[i].resize(n);
    rep(j, n) cin >> v[i][j];
  }

  rep(i, m) {
    int o;
    cin >> o;

    int r, c, size, angle;

    if (o == 0) { // rotate
      cin >> r >> c >> size >> angle;
      r--;
      c--;

      vrotate(r, c, size, angle);
    } else if (o == 1) { // reversal
      cin >> r >> c >> size;
      r--;
      c--;

      reversal(r, c, size);
    } else if (o == 2) { // left shift
      cin >> r;
      r--;

      leftshift(r);
    } else if (o == 3) { // right shift
      cin >> r;
      r--;
      rightshift(r);
    } else if (o == 4) { // Island reversal
      cin >> r >> c;
      r--;
      c--;
      islandreversal(r, c);
    }

    // rep(j, n) {
    // 	rep(k, n) {
    // 		cout << v[j][k] << " ";
    // 	}
    // 	cout << endl;
    // }
  }

  rep(i, n) {
    rep(j, n) {
      cout << v[i][j];

      if (j == n - 1)
        cout << endl;
      else
        cout << " ";
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m;
int t[15][15];
vector<int> v[15];

void vrotate(int r, int c, int size, int angle) {
  if (angle == 0 || angle == 360)
    return;

  memset(t, 0, sizeof(t));

  rep(i, angle / 90) {
    int col = c;
    for (int i = r + size - 1; i >= r; i--, col++) {
      REP(j, c, c + size) { t[r + j - c][col] = v[i][j]; }
    }

    REP(i, r, r + size) {
      REP(j, c, c + size) { v[i][j] = t[i][j]; }
    }
  }
}

void reversal(int r, int c, int size) {
  REP(i, r, r + size) {
    REP(j, c, c + size) { v[i][j] = !v[i][j]; }
  }
}

void leftshift(int r) {
  int t = v[r][0];
  rep(i, n) { v[r][i] = v[r][i + 1]; }
  v[r][n - 1] = t;
  // rotate(v[r].begin(),v[r].begin()+1,v[r].end());
}

void rightshift(int r) {
  int t = v[r][n - 1];
  for (int i = n - 1; i >= 1; i--) {
    v[r][i] = v[r][i - 1];
  }
  v[r][0] = t;
  // rotate(v[r].rbegin(),v[r].rbegin()+1,v[r].rend());
}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

bool can(int y, int x) {
  if (0 <= y && y < n && 0 <= x && x < n)
    return true;
  return false;
}

void islandreversal(int r, int c) {
  queue<P> que;
  que.push(mp(r, c));
  int t = v[r][c];
  while (que.size()) {
    P p = que.front();
    que.pop();
    int y = p.first, x = p.second;

    if (v[y][x] == t) {
      v[y][x] = !v[y][x];
    }

    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];

      if (can(ny, nx) && v[ny][nx] == t) {
        que.push(mp(ny, nx));
      }
    }
  }
}

int main() {
  cin >> n >> m;

  memset(v, 0, sizeof(v));
  rep(i, n) {
    v[i].resize(n);
    rep(j, n) cin >> v[i][j];
  }

  rep(i, m) {
    int o;
    cin >> o;

    int r, c, size, angle;

    if (o == 0) { // rotate
      cin >> r >> c >> size >> angle;
      r--;
      c--;

      vrotate(r, c, size, angle);
    } else if (o == 1) { // reversal
      cin >> r >> c >> size;
      r--;
      c--;

      reversal(r, c, size);
    } else if (o == 2) { // left shift
      cin >> r;
      r--;

      leftshift(r);
    } else if (o == 3) { // right shift
      cin >> r;
      r--;
      rightshift(r);
    } else if (o == 4) { // Island reversal
      cin >> r >> c;
      r--;
      c--;
      islandreversal(r, c);
    }

    // rep(j, n) {
    // 	rep(k, n) {
    // 		cout << v[j][k] << " ";
    // 	}
    // 	cout << endl;
    // }
  }

  rep(i, n) {
    rep(j, n) {
      cout << v[i][j];

      if (j == n - 1)
        cout << endl;
      else
        cout << " ";
    }
  }

  return 0;
}