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

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

constexpr int NX = 3, NY = 3;
constexpr int SIZE = NX * NY;

using namespace std;

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

set<vector<int>> visited;

bool IsSolved(const vector<int> &s) {
  int cnt = s[SIZE - 1] == 0 ? 1 : 0;
  for (int i = 0; i < SIZE; ++i) {
    if (s[i] == i + 1)
      ++cnt;
  }
  return cnt == SIZE;
}

bool IsVisited(const vector<int> &s) {
  return visited.find(s) != visited.end();
}

int Solve(const vector<int> &init_s) {

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

  queue<PuzzleState> state;
  state.push(make_pair(0, init_s));
  visited.insert(init_s);

  while (!state.empty()) {

    PuzzleState curr = state.front();

    if (IsSolved(curr.second))
      return curr.first;
    state.pop();

    int x, y;
    for (int i = 0; i < SIZE; ++i) {
      if (curr.second[i] == 0) {
        x = i % NX;
        y = i / NY;
        break;
      }
    }

    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx >= 0 && nx < NX && ny >= 0 && ny < NY) {

        swap(curr.second[NX * y + x], curr.second[NX * ny + nx]);
        curr.first += 1;

        if (!IsVisited(curr.second)) {
          state.push(curr);
          visited.insert(curr.second);
        }

        curr.first -= 1;
        swap(curr.second[NX * y + x], curr.second[NX * ny + nx]);
      }
    }
  }
  return 1000000;
}

int main() {

  vector<int> s(SIZE);
  for (int i = 0; i < SIZE; ++i)
    cin >> s[i];

  cout << Solve(s) << endl;

  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

vector<int> board(9);
set<vector<int>> reachedState;

void PrintBoard() {
  for (int row = 0; row < 3; ++row) {
    for (int col = 0; col < 3; ++col)
      cout << board[row * 3 + col];
    cout << endl;
  }
}

bool IsReachedState(const vector<int> &b) {
  return reachedState.find(b) != reachedState.end();
}

void RegisterState(const vector<int> &b) { reachedState.insert(b); }

bool IsCompleted(const vector<int> &b) {
  for (int i = 0; i < 8; ++i) {
    if (b[i] != i + 1)
      return false;
  }
  return b[8] == 0;
}

int main() {

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

  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> board[i * 3 + j];
    }
  }

  //    PrintBoard();

  queue<pair<vector<int>, int>> stateQue;
  stateQue.push(make_pair(board, 0));

  while (!stateQue.empty()) {

    vector<int> state = stateQue.front().first;
    int step = stateQue.front().second;
    stateQue.pop();

    if (IsCompleted(state)) {
      cout << step << endl;
      board = state;
      //	    PrintBoard();
      break;
    }

    //	if ( IsReachedState( state ) ) continue;
    //	RegisterState( state );

    int x, y;
    for (int i = 0; i < 9; ++i) {
      if (state[i] == 0) {
        x = i % 3;
        y = i / 3;
        break;
      }
    }

    for (int i = 0; i < 4; ++i) {

      int nx = x + dx[i];
      int ny = y + dy[i];

      if (nx < 0 || nx > 2 || ny < 0 || ny > 2)
        continue;

      vector<int> next = state;
      swap(next[3 * y + x], next[3 * ny + nx]);
      stateQue.push(make_pair(next, step + 1));
    }
  }

  return 0;
}