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

/*  */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// #pragma comment(linker,"/STACK:102400000,1024000")

#define sti set<int>
#define stpii set<pair<int, int>>
#define mpii map<int, int>
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define per(i, a, n) for (int i = n - 1; i >= a; --i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define getBits(x) __builtin_popcount(x)
#define getBitIdx(x) __builtin_ffs(x)

typedef long long LL;
const int maxn = 45;
int s[maxn][maxn];
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
int n, m;
int stx, sty, edx, edy;
int ansd;

inline bool judge(int x, int y) { return x < 0 || x >= n || y < 0 || y >= m; }

void init() {
  rep(i, 0, n) {
    rep(j, 0, m) {
      if (s[i][j] == 2) {
        stx = i;
        sty = j;
        s[i][j] = 0;
      } else if (s[i][j] == 3) {
        edx = i;
        edy = j;
        s[i][j] = 0;
      }
    }
  }
}

void dfs(int dep, int x, int y) {
  if (dep >= ansd)
    return;
  if (x == edx && y == edy) {
    ansd = dep;
    return;
  }

  int xx, yy;
  int flag;

  rep(i, 0, 4) {
    xx = x;
    yy = y;
    if (judge(xx + dir[i][0], yy + dir[i][1]) ||
        s[xx + dir[i][0]][yy + dir[i][1]] == 1)
      continue;

    flag = 1;
    while (true) {
      if (judge(xx + dir[i][0], yy + dir[i][1])) {
        flag = 0;
        break;
      }

      if (s[xx + dir[i][0]][yy + dir[i][1]] == 1) {
        flag = -1;
        break;
      }

      xx += dir[i][0];
      yy += dir[i][1];

      if (xx == edx && yy == edy) {
        break;
      }
    }

    if (flag == 0)
      continue;

    if (flag == -1) {
      s[xx + dir[i][0]][yy + dir[i][1]] = 0;
    }

    dfs(dep + 1, xx, yy);

    if (flag == -1) {
      s[xx + dir[i][0]][yy + dir[i][1]] = 1;
    }
  }
}

void solve() {
  init();
  ansd = 11;
  dfs(0, stx, sty);
  if (ansd == 11)
    ansd = -1;
  printf("%d\\n", ansd);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // #ifndef ONLINE_JUDGE
  // freopen("data.in", "r", stdin);
  // freopen("data.out", "w", stdout);
  // #endif

  while (scanf("%d %d", &m, &n) != EOF && (n || m)) {
    rep(i, 0, n) rep(j, 0, m) scanf("%d", &s[i][j]);
    solve();
  }

  // #ifndef ONLINE_JUDGE
  // printf("time = %ldms.\\n", clock());
  // #endif

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int const DIRECTION[][2] = {-1, 0, 0, 1, 1, 0, 0, -1};

struct qelem {
  int row, col;
  int cnt;
  vector<vector<char>> grid;
};

int w, h;
queue<qelem> que;

int main() {
  while (cin >> w >> h && !(w == 0 && h == 0)) {
    qelem step;
    step.grid = vector<vector<char>>(h, vector<char>(w));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> step.grid[i][j];
        if (step.grid[i][j] == '2') {
          step.row = i;
          step.col = j;
          step.grid[i][j] = '0';
        }
      }
    }

    while (!que.empty())
      que.pop();
    bool yes = false;
    step.cnt = 0;
    que.push(step);
    while (!que.empty()) {
      step = que.front();
      que.pop();

      if (step.cnt >= 10)
        break;

      for (int i = 0; i < 4; i++) {
        int dr = step.row + DIRECTION[i][0], dc = step.col + DIRECTION[i][1];
        while (0 <= dr && dr < h && 0 <= dc && dc < w) {
          if (step.grid[dr][dc] != '0')
            break;
          dr += DIRECTION[i][0];
          dc += DIRECTION[i][1];
        }

        if (dr < 0 || dr >= h || dc < 0 || dc >= w)
          continue;
        else if (step.grid[dr][dc] == '3') {
          yes = true;
          step.cnt++;
          break;
        } else if (dr == step.row + DIRECTION[i][0] &&
                   dc == step.col + DIRECTION[i][1])
          continue;
        qelem tmp = step;
        tmp.row = dr - DIRECTION[i][0];
        tmp.col = dc - DIRECTION[i][1];
        tmp.grid[dr][dc] = '0';
        tmp.cnt++;
        que.push(tmp);

        // for (int i = 0; i < h; i++) {
        // for (int j = 0; j < w; j++) cout << step.grid[i][j] << ' ';
        // cout << "|";
        // for (int j = 0; j < w; j++) {
        // if (tmp.grid[i][j] != step.grid[i][j]) cout << " #";
        // else cout << ' ' << tmp.grid[i][j];
        // }
        // cout << endl;
        // }
        // cout << "cnt= " << step.cnt << endl;
      }
      if (yes)
        break;
    }
    if (yes)
      cout << step.cnt << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}