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

#include <cstdio>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;

typedef pair<vector<int>, int> P;

int zeroPosition(vector<int> a) {
  for (int i = 0; i < 9; i++) {
    if (a[i] == 0)
      return i;
  }

  return -1;
}

vector<int> swapDigit(vector<int> v, int a, int b) {
  vector<int> ret = v;
  int x = v[a];
  int y = v[b];
  ret[a] = y;
  ret[b] = x;

  return ret;
}

bool isSame(vector<int> a, vector<int> b) {
  for (int i = 0; i < 9; i++) {
    if (a[i] != b[i])
      return false;
  }

  return true;
}

int convert(vector<int> a) {
  int ret = 0;
  for (int i = 0; i < 9; i++) {
    ret *= 10;
    ret += a[i];
  }

  return ret;
}

int main() {
  set<int> S;
  queue<P> Q;
  vector<int> start;
  vector<int> goal; // 123456780
  for (int i = 0; i < 9; i++) {
    goal.push_back((i + 1) % 9);
  }

  for (int i = 0; i < 9; i++) {
    int d;
    scanf("%d", &d);
    start.push_back(d);
  }

  S.insert(convert(start));
  Q.push(make_pair(start, 0));
  int ans = 0;

  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    if (isSame(p.first, goal)) {
      ans = p.second;
      break;
    }
    int pos = zeroPosition(p.first);

    if ((pos % 3) != 0) { // ???????§???????
      vector<int> v = swapDigit(p.first, pos, pos - 1);
      if (S.find(convert(v)) == S.end()) {
        Q.push(make_pair(v, p.second + 1));
        S.insert(convert(v));
      }
    }

    if ((pos % 3) != 2) { // ???????§???????
      vector<int> v = swapDigit(p.first, pos, pos + 1);
      if (S.find(convert(v)) == S.end()) {
        Q.push(make_pair(v, p.second + 1));
        S.insert(convert(v));
      }
    }

    if (pos > 2) { // ???????§???????
      vector<int> v = swapDigit(p.first, pos, pos - 3);
      if (S.find(convert(v)) == S.end()) {
        Q.push(make_pair(v, p.second + 1));
        S.insert(convert(v));
      }
    }

    if (pos < 6) { // ???????§???????
      vector<int> v = swapDigit(p.first, pos, pos + 3);
      if (S.find(convert(v)) == S.end()) {
        Q.push(make_pair(v, p.second + 1));
        S.insert(convert(v));
      }
    }
  }

  printf("%d\\n", ans);

  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;

typedef pair<vector<int>, int> P;

int zeroPosition(vector<int> a) {
  for (int i = 0; i < 9; i++) {
    if (a[i] == 0)
      return i;
  }

  return -1;
}

vector<int> swapDigit(vector<int> v, int a, int b) {
  vector<int> ret = v;
  int x = v[a];
  int y = v[b];
  ret[a] = y;
  ret[b] = x;

  return ret;
}

bool isSame(vector<int> a, vector<int> b) {
  for (int i = 0; i < 9; i++) {
    if (a[i] != b[i])
      return false;
  }

  return true;
}

int convert(vector<int> a) {
  int ret = 0;
  for (int i = 0; i < 9; i++) {
    ret *= 10;
    ret += a[i];
  }

  return ret;
}

int main() {
  set<int> S;
  queue<P> Q;
  vector<int> start;
  vector<int> goal; // 123456780
  for (int i = 0; i < 9; i++) {
    goal.push_back((i + 1) % 9);
  }

  for (int i = 0; i < 9; i++) {
    int d;
    scanf("%d", &d);
    start.push_back(d);
  }

  S.insert(convert(start));
  Q.push(make_pair(start, 0));
  int ans = 0;

  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    if (isSame(p.first, goal)) {
      ans = p.second;
      break;
    }
    int pos = zeroPosition(p.first);

    if ((pos % 3) != 0) { // ???????§???????
      vector<int> v = swapDigit(p.first, pos, pos - 1);
      if (S.find(convert(v)) == S.end()) {
        Q.push(make_pair(v, p.second + 1));
      }
    }

    if ((pos % 3) != 2) { // ???????§???????
      vector<int> v = swapDigit(p.first, pos, pos + 1);
      if (S.find(convert(v)) == S.end()) {
        Q.push(make_pair(v, p.second + 1));
      }
    }

    if (pos > 2) { // ???????§???????
      vector<int> v = swapDigit(p.first, pos, pos - 3);
      if (S.find(convert(v)) == S.end()) {
        Q.push(make_pair(v, p.second + 1));
      }
    }

    if (pos < 6) { // ???????§???????
      vector<int> v = swapDigit(p.first, pos, pos + 3);
      if (S.find(convert(v)) == S.end()) {
        Q.push(make_pair(v, p.second + 1));
      }
    }
  }

  printf("%d\\n", ans);

  return 0;
}