Problem p03389 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int visited[501][501][501];

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int a, b, c;

  memset(visited, -1, sizeof(visited));
  cin >> a >> b >> c;
  visited[a][b][c] = 0;

  queue<pair<int, pair<int, int>>> que;
  que.push(make_pair(a, make_pair(b, c)));

  while (!que.empty()) {
    int A = que.front().first;
    int B = que.front().second.first;
    int C = que.front().second.second;
    que.pop();

    if (A == B && A == C) {
      cout << visited[A][B][C] << '\n';
      break;
    }

    if (A + 2 <= 500 && visited[A + 2][B][C] == -1) {
      visited[A + 2][B][C] = visited[A][B][C] + 1;
      que.push(make_pair(A + 2, make_pair(B, C)));
    }
    if (B + 2 <= 500 && visited[A][B + 2][C] == -1) {
      visited[A][B + 2][C] = visited[A][B][C] + 1;
      que.push(make_pair(A, make_pair(B + 2, C)));
    }
    if (C + 2 <= 500 && visited[A][B][C + 2] == -1) {
      visited[A][B][C + 2] = visited[A][B][C] + 1;
      que.push(make_pair(A, make_pair(B, C + 2)));
    }
    if (A + 1 <= 500 && B + 1 <= 500 && visited[A + 1][B + 1][C] == -1) {
      visited[A + 1][B + 1][C] = visited[A][B][C] + 1;
      que.push(make_pair(A + 1, make_pair(B + 1, C)));
    }
    if (A + 1 <= 500 && C + 1 <= 500 && visited[A + 1][B][C + 1] == -1) {
      visited[A + 1][B][C + 1] = visited[A][B][C] + 1;
      que.push(make_pair(A + 1, make_pair(B, C + 1)));
    }
    if (B + 1 <= 500 && C + 1 <= 500 && visited[A][B + 1][C + 1] == -1) {
      visited[A][B + 1][C + 1] = visited[A][B][C] + 1;
      que.push(make_pair(A, make_pair(B + 1, C + 1)));
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int visited[251][251][251];

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int a, b, c;

  memset(visited, -1, sizeof(visited));
  cin >> a >> b >> c;
  visited[a][b][c] = 0;

  queue<pair<int, pair<int, int>>> que;
  que.push(make_pair(a, make_pair(b, c)));

  while (!que.empty()) {
    int A = que.front().first;
    int B = que.front().second.first;
    int C = que.front().second.second;
    que.pop();

    if (A == B && A == C) {
      cout << visited[A][B][C] << '\n';
      break;
    }

    if (A + 2 <= 500 && visited[A + 2][B][C] == -1) {
      visited[A + 2][B][C] = visited[A][B][C] + 1;
      que.push(make_pair(A + 2, make_pair(B, C)));
    }
    if (B + 2 <= 500 && visited[A][B + 2][C] == -1) {
      visited[A][B + 2][C] = visited[A][B][C] + 1;
      que.push(make_pair(A, make_pair(B + 2, C)));
    }
    if (C + 2 <= 500 && visited[A][B][C + 2] == -1) {
      visited[A][B][C + 2] = visited[A][B][C] + 1;
      que.push(make_pair(A, make_pair(B, C + 2)));
    }
    if (A + 1 <= 500 && B + 1 <= 500 && visited[A + 1][B + 1][C] == -1) {
      visited[A + 1][B + 1][C] = visited[A][B][C] + 1;
      que.push(make_pair(A + 1, make_pair(B + 1, C)));
    }
    if (A + 1 <= 500 && C + 1 <= 500 && visited[A + 1][B][C + 1] == -1) {
      visited[A + 1][B][C + 1] = visited[A][B][C] + 1;
      que.push(make_pair(A + 1, make_pair(B, C + 1)));
    }
    if (B + 1 <= 500 && C + 1 <= 500 && visited[A][B + 1][C + 1] == -1) {
      visited[A][B + 1][C + 1] = visited[A][B][C] + 1;
      que.push(make_pair(A, make_pair(B + 1, C + 1)));
    }
  }
  return 0;
}