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

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 29
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ":" << x << endl
#define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl
#define CININIT cin.tie(0), ios::sync_with_stdio(false)
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

constexpr ll mod = 1e9 + 7;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int H, W;
vector<vector<int>> maze;
int sx, sy, gx, gy;

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

int dfs(int x, int y, int cost) {
  //    auto tab = maze;
  //    tab[x][y]=4;
  //    cout << "--------------------------------------------------" << endl;
  //    for(int i=0;i<H;i++){
  //        for(int j=0;j<W;j++) cout << tab[i][j] << " ";
  //        cout << endl;
  //    }
  if (cost > 9)
    return -1;

  rep(i, 4) {
    for (int j = 1; x + dx[i] * j >= 0 and x + dx[i] * j < H and
                    y + dy[i] * j >= 0 and y + dy[i] * j < W;
         j++) {
      int nx = x + dx[i] * j, ny = y + dy[i] * j;
      if (maze[nx][ny] == 1)
        break;
      if (nx == gx and ny == gy) {
        //                cout << "return : " << cost + 1 << endl;
        return cost + 1;
      }
    }
  }

  int ret = -1;
  rep(i, 4) {
    if (in(x + dx[i], y + dy[i], H, W) and maze[x + dx[i]][y + dy[i]] == 1)
      continue;
    for (int j = 1; x + dx[i] * j >= 0 and x + dx[i] * j < H and
                    y + dy[i] * j >= 0 and y + dy[i] * j < W;
         j++) {
      int nx = x + dx[i] * j, ny = y + dy[i] * j;
      if (maze[nx][ny] == 1) {
        maze[nx][ny] = 0;
        int tmp = dfs(x + dx[i] * (j - 1), y + dy[i] * (j - 1), cost + 1);
        maze[nx][ny] = 1;
        if (tmp >= 0) {
          if (ret == -1)
            ret = tmp;
          else
            ret = min(ret, tmp);
        }
        break;
      }
    }
  }
  //   if(ret==-1) cout << "error" << endl;
  return ret;
}

void solve() {
  vvi tmp;
  swap(maze, tmp);
  maze = vvi(H, vi(W));
  rep(i, H) rep(j, W) {
    cin >> maze[i][j];
    if (maze[i][j] == 2) {
      sx = i;
      sy = j;
    }
    if (maze[i][j] == 3) {
      gx = i;
      gy = j;
    }
  }

  int ans = dfs(sx, sy, 0);
  cout << ans << endl;
}

int main() {
  while (cin >> W >> H) {
    if (H == 0 and W == 0)
      break;
    solve();
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 29
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ":" << x << endl
#define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl
#define CININIT cin.tie(0), ios::sync_with_stdio(false)
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

constexpr ll mod = 1e9 + 7;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

struct node {
  int x, y, cost;
  vector<vector<bool>> maze;
  node() {}
  node(int i, int j, int k, vector<vector<bool>> v)
      : x(i), y(j), cost(k), maze(v) {}
};

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

void solve(const int H, const int W) {
  // debug2(H,W);
  vector<vector<bool>> maze(H, vector<bool>(W));
  int sx, sy, gx, gy;
  rep(i, H) rep(j, W) {
    int input;
    cin >> input;
    if (input == 2) {
      sx = i;
      sy = j;
    }
    if (input == 3) {
      gx = i;
      gy = j;
    }

    if (input == 1)
      maze[i][j] = false;
    else
      maze[i][j] = true;
  }

  //    debug2(sx,sy);
  //    debug2(gx,gy);

  queue<node> que;
  que.push(node(sx, sy, 0, maze));

  while (!que.empty()) {
    node p = que.front();
    que.pop();
    if (p.cost > 9)
      break;
    int x = p.x, y = p.y;
    //        cerr << "pop : (" << x << " " << y << ")" << endl;
    auto v = p.maze;

    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= H || ny >= W || v[nx][ny] == false)
        continue;

      // debug(i);
      for (int j = 1; x + dx[i] * j < H and x + dx[i] * j >= 0 and
                      y + dy[i] * j >= 0 and y + dy[i] * j < W;
           j++) {
        //                cerr << "j= " << j << " ... (" << x+dx[i]*j << " " <<
        //                y+dy[i]*j << ")" << endl;
        if (x + dx[i] * j == gx and y + dy[i] * j == gy) {
          cout << p.cost + 1 << endl;
          return;
        }
        if (in(x + dx[i] * j, y + dy[i] * j, H, W) and
            v[x + dx[i] * j][y + dy[i] * j] == false) {
          auto nv = v;
          nv[x + dx[i] * j][y + dy[i] * j] = true;
          que.push(
              node(x + dx[i] * (j - 1), y + dy[i] * (j - 1), p.cost + 1, nv));
          //                    cerr << "push : (" << x+dx[i]*(j-1) << "," << y
          //                    + dy[i]*(j-1) <<")" << endl;
          break;
        }
      }
    }
  }
  cout << -1 << endl;
}

int main() {
  int w, h;
  while (cin >> w >> h) {
    if (w == 0 and h == 0)
      break;
    solve(h, w);
  }
}