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

// ------ Includes ------ //

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

using namespace std;

// ------ Directions ------ //

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

// ------ BFS ------ //

int BFS(vector<vector<int>> p) {
  // ------ Initialize ------ //

  vector<vector<int>> lastboard = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};

  map<vector<vector<int>>, int> D;

  queue<pair<vector<vector<int>>, int>> que;

  D[p] = 1;
  que.push(make_pair(p, 1));

  // ------ Search ------ //

  if (p == lastboard) {
    return 0;
  }

  while (!que.empty()) {
    pair<vector<vector<int>>, int> p1 = que.front();
    que.pop();

    // ------ 0 Searching ------ //

    int tx = -1, ty = -1;

    for (int i = 0; i < p1.first.size(); i++) {
      for (int j = 0; j < p1.first[i].size(); j++) {
        if (p1.first[i][j] == 0) {
          tx = j;
          ty = i;
        }
      }
    }

    // ------ Moving ------ //

    for (int dir = 0; dir < 4; dir++) {
      if (0 <= tx + dx[dir] && tx + dx[dir] < p1.first[0].size() &&
          0 <= ty + dy[dir] && ty + dy[dir] < p1.first.size()) {
        vector<vector<int>> p2 = p1.first;

        swap(p2[ty][tx], p2[ty + dy[dir]][tx + dx[dir]]);

        if (D[p2] == 0) {
          if (p2 == lastboard) {
            return p1.second;
          }

          D[p2] = p1.second + 1;

          que.push(make_pair(p2, p1.second + 1));
        }
      }
    }
  }

  return -1;
}

int main() {
  vector<vector<int>> board(3, vector<int>(3, -1));
  ;

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

  cout << BFS(board) << endl;

  return 0;
}

Error: MLE

Fixed submission:

// ------ Includes ------ //

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

using namespace std;

// ------ Directions ------ //

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

// ------ BFS ------ //

int BFS(vector<vector<int>> p) {
  // ------ Initialize ------ //

  vector<vector<int>> lastboard = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};

  map<vector<vector<int>>, int> D;

  queue<pair<vector<vector<int>>, int>> que;

  D[p] = 1;
  que.push(make_pair(p, 1));

  // ------ Search ------ //

  while (!que.empty()) {
    pair<vector<vector<int>>, int> p1 = que.front();
    que.pop();

    // ------ 0 Searching ------ //

    int tx = -1, ty = -1;

    for (int i = 0; i < p1.first.size(); i++) {
      for (int j = 0; j < p1.first[i].size(); j++) {
        if (p1.first[i][j] == 0) {
          tx = j;
          ty = i;
        }
      }
    }

    // ------ Moving ------ //

    for (int dir = 0; dir < 4; dir++) {
      if (0 <= tx + dx[dir] && tx + dx[dir] < p1.first[0].size() &&
          0 <= ty + dy[dir] && ty + dy[dir] < p1.first.size()) {
        vector<vector<int>> p2 = p1.first;

        swap(p2[ty][tx], p2[ty + dy[dir]][tx + dx[dir]]);

        if (p2 == lastboard) {
          return p1.second;
        }

        D[p2] = p1.second + 1;

        que.push(make_pair(p2, p1.second + 1));
      }
    }
  }

  return -1;
}

int main() {
  vector<vector<int>> board(3, vector<int>(3, -1));
  ;

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

  cout << BFS(board) << endl;

  return 0;
}