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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

// cout vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << "\\t";
  }
  return s;
}

// cout 2-dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}

vvi board;
vi gl;
int ans;

void dfs(vi current, int moves) {
  // procedure of move
  if (moves >= 10) {
    return;
  }
  vvi search = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  for (auto v : search) {
    vi next = {current[0] + v[0], current[1] + v[1]};
    if (board[next[0]][next[1]] < 0) {
      continue;
    }
    next = current;
    while (board[next[0] + v[0]][next[1] + v[1]] >= 0) {
      next[0] += v[0];
      next[1] += v[1];
      if (next[0] == gl[0] && next[1] == gl[1]) {
        ans = min(ans, moves + 1);
        return;
      }
    }
    if (board[next[0] + v[0]][next[1] + v[1]] == -2) {
      continue;
    }
    if (board[next[0] + v[0]][next[1] + v[1]] == -1) {
      board[next[0] + v[0]][next[1] + v[1]] = 0;
      dfs(next, moves + 1);
      board[next[0] + v[0]][next[1] + v[1]] = -1;
    }
  }
  return;
}

int main() {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (h == 0) {
      break;
    }
    board.clear();
    board.resize(h + 2, vi(w + 2, -2));
    vi st(2); // h, w
    gl.clear();
    gl.resize(2); // h, w
    for (int ih = 1; ih <= h; ih++) {
      for (int iw = 1; iw <= w; iw++) {
        int tmp;
        cin >> tmp;
        if (tmp == 1) {
          tmp = -1;
        }
        if (tmp == 2) {
          st[0] = ih;
          st[1] = iw;
          board[ih][iw] = 0;
        } else if (tmp == 3) {
          gl[0] = ih;
          gl[1] = iw;
          board[ih][iw] = 0;
        } else {
          board[ih][iw] = tmp;
        }
      }
    }
    ans = 100;
    dfs(st, 0);
    if (ans > 10) {
      ans = -1;
    }

    printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

class Curling {
public:
  vvi board;
  int w, h;
  int moves;
};

int main() {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (h == 0) {
      break;
    }
    Curling cu;
    cu.board.resize(h + 2, vi(w + 2, -2)); // -2 : out of board
    cu.moves = 0;
    vi goal;
    FOR(ih, 1, h + 1) {
      FOR(iw, 1, w + 1) {
        int cell;
        cin >> cell;
        if (cell == 1) {
          cu.board[ih][iw] = -1; // -1 : block
        } else if (cell == 0) {
          cu.board[ih][iw] = 0; // nothing
        } else if (cell == 2) {
          cu.h = ih; // start
          cu.w = iw;
          cu.board[ih][iw] = 0;
        } else {
          goal = {ih, iw}; // goal
          cu.board[ih][iw] = 0;
        }
      }
    }

    deque<Curling> s;
    s.pb(cu);
    int ans = -1;
    while (!s.empty()) {
      Curling current = s.front();
      s.pop_front();
      if (current.moves >= 10) {
        continue;
      }
      vvi search = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
      for (auto v : search) {
        bool on_goal = false;
        int next_h = current.h + v[0];
        int next_w = current.w + v[1];
        if (current.board[next_h][next_w] < 0) { // cannot move
          continue;
        }
        Curling next = current;
        next.moves += 1;
        while (next.board[next.h + v[0]][next.w + v[1]] >= 0) {
          next.h += v[0];
          next.w += v[1];
          if (next.h == goal[0] && next.w == goal[1]) { // goal
            ans = next.moves;
            s.clear();
            on_goal = true;
            break;
          }
        }
        if (on_goal) {
          break;
        }
        if (next.board[next.h + v[0]][next.w + v[1]] == -2) { // out of board
          continue;
        }
        if (next.board[next.h + v[0]][next.w + v[1]] == -1) { // block
          next.board[next.h + v[0]][next.w + v[1]] = 0;
        }
        s.pb(next);
      }
    }
    printf("%d\\n", ans);
  }

  return 0;
}