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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

vector<vector<int>> fld(30, vi(30));
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
int h, w, ans;

bool check(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w)
    return true;
  return false;
}

void solve(int x, int y, int n) {
  if (n == 10)
    return;
  REP(i, 4) {
    int nx = x;
    int ny = y;
    if (!check(nx + dx[i], ny + dy[i]) || fld[nx + dx[i]][ny + dy[i]] == 1)
      continue;
    while (1) {
      nx += dx[i];
      ny += dy[i];
      if (!check(nx, ny))
        break;
      if (fld[nx][ny] == 1)
        break;
      if (fld[nx][ny] == 3) {
        ans = min(ans, n + 1);
        return;
      }
    }
    if (!check(nx, ny))
      continue;
    fld[nx][ny] = 0;
    solve(nx - dx[i], ny - dy[i], n + 1);
    fld[nx][ny] = 1;
  }
}

int main() {
  while (cin >> w >> h, w) {
    ans = 100;
    int x, y;
    REP(i, h) REP(j, w) {
      cin >> fld[i][j];
      if (fld[i][j] == 2) {
        x = i;
        y = j;
        fld[i][j] = 0;
      }
    }
    solve(x, y, 0);
    cout << (ans == 100 ? -1 : ans) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#include <regex>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);

int w, h;

int main() {
  while (cin >> w >> h, w) {
    vector<vector<char>> v(h, vector<char>(w));
    REP(i, h) REP(j, w) cin >> v[i][j];
    typedef pair<vector<vector<char>>, short> p;
    queue<p> que;
    que.push(p(v, 0));
    while (que.size()) {
      auto copy = que.front().first;
      int cnt = que.front().second;
      que.pop();
      if (cnt == 10) {
        cout << -1 << endl;
        goto end;
      }
      cnt++;
      int x, y;
      REP(i, h) REP(j, w) {
        if (copy[i][j] == '2') {
          x = i;
          y = j;
        }
      }
      int dx[] = {-1, 0, 0, 1};
      int dy[] = {0, -1, 1, 0};
      REP(i, 4) {
        auto fld = copy;
        int tx = x;
        int ty = y;
        int nx = x + dx[i];
        int ny = y + dy[i];
        if ((nx < 0 || ny < 0 || nx >= h || ny >= w) || fld[nx][ny] == '1')
          continue;
        while (1) {
          int nx = tx + dx[i];
          int ny = ty + dy[i];
          if (nx < 0 || ny < 0 || nx >= h || ny >= w)
            break;
          else if (fld[nx][ny] == '0') {
            fld[nx][ny] = '2';
            fld[tx][ty] = '0';
            tx = nx;
            ty = ny;
          } else if (fld[nx][ny] == '1') {
            fld[nx][ny] = '0';
            que.push(p(fld, cnt));
            break;
          } else {
            cout << cnt << endl;
            goto end;
          }
        }
      }
    }
    cout << -1 << endl;
  end:;
  }
}