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

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) cerr << #x << "=" << static_cast<bitset<16>>(x) << endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

const int INF = 10000000;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2 * 123500;

/** Problem1144 : Curling 2.0 **/
int w, h, ans;

typedef struct {
  int field[20][20];
  P pos;
  int cost;
} S;

void dfs(S t) {
  int x = t.pos.first, y = t.pos.second;

  if (t.cost > 10)
    return;

  rep(i, 4) {
    int nx = x, ny = y;
    if (t.field[nx + dx[i]][ny + dy[i]] == 1)
      goto next;
    while (1) {
      nx += dx[i];
      ny += dy[i];
      if (0 <= nx && nx < w && 0 <= ny && ny < h) {
        if (t.field[nx][ny] == 3) {
          ans = min(ans, t.cost + 1);
        }
        if (t.field[nx][ny] == 1) {
          t.field[nx][ny] = 0;
          break;
        }
      } else {
        goto next;
      }
    }
    t.cost++;
    t.pos = P(nx - dx[i], ny - dy[i]);
    dfs(t);
    t.cost--;
    t.pos = P(x, y);
    t.field[nx][ny] = 1;
  next:;
  }
}

int main() {
  while (cin >> w >> h, w || h) {
    ans = INF;
    S s = {{0}, P(0, 0), 0};
    rep(y, h) rep(x, w) {
      cin >> s.field[x][y];
      if (s.field[x][y] == 2)
        s.pos = P(x, y);
    }

    dfs(s);

    if (ans <= 10)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) cerr << #x << "=" << static_cast<bitset<16>>(x) << endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

const int INF = 10000000;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 2 * 123500;

/** Problem1144 : Curling 2.0 **/
typedef struct {
  int field[20][20];
  P pos;
  int cost;
} S;

int main() {
  int w, h, ans;
  while (cin >> w >> h, w || h) {
    ans = INF;
    S s = {{0}, P(0, 0), 0};
    rep(y, h) rep(x, w) {
      cin >> s.field[x][y];
      if (s.field[x][y] == 2)
        s.pos = P(x, y);
    }

    queue<S> Q;
    Q.push(s);

    while (Q.size()) {
      S t = Q.front();
      Q.pop();
      int x = t.pos.first, y = t.pos.second;

      if (t.cost > 10)
        continue;

      rep(i, 4) {
        int nx = x, ny = y;
        if (t.field[nx + dx[i]][ny + dy[i]] == 1)
          goto next;
        while (1) {
          nx += dx[i];
          ny += dy[i];
          if (0 <= nx && nx < w && 0 <= ny && ny < h) {
            if (t.field[nx][ny] == 3) {
              ans = min(ans, t.cost + 1);
              goto next;
            }
            if (t.field[nx][ny] == 1) {
              t.field[nx][ny] = 0;
              break;
            }
          } else {
            goto next;
          }
        }
        t.cost++;
        t.pos = P(nx - dx[i], ny - dy[i]);
        Q.push(t);
        t.cost--;
        t.pos = P(x, y);
        t.field[nx][ny] = 1;
      next:;
      }
    }
    if (ans <= 10)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}