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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

struct state {
  vector<vector<int>> bd;
  int cnt;
};

class EightPuzzle {
public:
  queue<state> q;
  map<vector<vector<int>>, bool> m;
  vector<vector<int>> goal;
  vector<int> dr, dc;
  EightPuzzle() {}
  EightPuzzle(state st) {
    q.push(st);
    goal = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};
    dr = {1, -1, 0, 0};
    dc = {0, 0, 1, -1};
  }
  int solve();
  void find0(vector<vector<int>> &b, int &x, int &y);
};

void EightPuzzle::find0(vector<vector<int>> &b, int &r, int &c) {
  for (r = 0; r < 3; ++r)
    for (c = 0; c < 3; ++c)
      if (b[r][c] == 0)
        return;
}

int EightPuzzle::solve() {
  while (!q.empty()) {
    state st = q.front();

    if (st.bd == goal) {
      cout << st.cnt << endl;
      return 0;
    }

    int r, c;
    find0(st.bd, r, c);

    for (auto i = 0; i < 4; ++i) {
      int nr = r + dr[i];
      int nc = c + dc[i];
      if (nr < 0 || nr > 2 || nc < 0 || nc > 2)
        continue;

      state nst = st;
      nst.bd[r][c] = st.bd[nr][nc];
      nst.bd[nr][nc] = 0;
      nst.cnt = st.cnt + 1;

      if (m.count(nst.bd) == 0) {
        q.push(nst);
        m[nst.bd] = true;
      }
    }
    q.pop();
  }

  return 0;
}

int main() {
  state st;
  st.cnt = 0;

  for (auto i = 0; i < 3; ++i) {
    vector<int> vl;
    for (auto j = 0; j < 3; ++j) {
      int v;
      cin >> v;
      vl.push_back(v);
    }
    st.bd.push_back(vl);
  }

  EightPuzzle ep(st);

  ep.solve();

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

struct state {
  vector<vector<int>> bd;
  int cnt;
};

class EightPuzzle {
public:
  queue<state> q;
  vector<vector<int>> goal;
  vector<int> dr, dc;
  EightPuzzle() {}
  EightPuzzle(state st) {
    q.push(st);
    goal = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};
    dr = {1, -1, 0, 0};
    dc = {0, 0, 1, -1};
  }
  int solve();
  void find0(vector<vector<int>> &b, int &x, int &y);
};

void EightPuzzle::find0(vector<vector<int>> &b, int &r, int &c) {
  for (r = 0; r < 3; ++r)
    for (c = 0; c < 3; ++c)
      if (b[r][c] == 0)
        return;
}

int EightPuzzle::solve() {
  while (!q.empty()) {
    state st = q.front();

    if (st.bd == goal) {
      cout << st.cnt << endl;
      return 0;
    }

    int r, c;
    find0(st.bd, r, c);

    for (auto i = 0; i < 4; ++i) {
      int nr = r + dr[i];
      int nc = c + dc[i];
      if (nr < 0 || nr > 2 || nc < 0 || nc > 2)
        continue;

      state nst = st;
      nst.bd[r][c] = st.bd[nr][nc];
      nst.bd[nr][nc] = 0;
      nst.cnt = st.cnt + 1;
      q.push(nst);
    }
    q.pop();
  }

  return 0;
}

int main() {
  state st;
  st.cnt = 0;

  for (auto i = 0; i < 3; ++i) {
    vector<int> vl;
    for (auto j = 0; j < 3; ++j) {
      int v;
      cin >> v;
      vl.push_back(v);
    }
    st.bd.push_back(vl);
  }

  EightPuzzle ep(st);

  ep.solve();

  return 0;
}