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

#include <bits/stdc++.h>
#define Board vector<vector<int>>
using namespace std;
int d[5] = {0, -1, 0, 1, 0};

int v[20][20];
int max_deep;
int w, h;

int dfs(int x, int y, int z) {
  if (z > max_deep)
    return max_deep;
  int ret = max_deep;
  for (int i = 0; i < 4; i++) {
    int ax = x;
    int ay = y;
    if (v[ax + d[i]][ay + d[i + 1]] == 1)
      continue;
    while (ax >= 0 && ax < w && ay >= 0 && ay < h && v[ax][ay] % 2 == 0) {
      ax += d[i];
      ay += d[i + 1];
    }
    if (ax < 0 || ax >= w || ay < 0 || ay >= h) {
      continue;
    }
    if (v[ax][ay] == 3) {
      return max_deep = z;
    }
    if (v[ax][ay] == 1) {
      v[ax][ay] = 0;
      ret = min(ret, dfs(ax - d[i], ay - d[i + 1], z + 1));
      v[ax][ay] = 1;
    }
  }
  return ret;
}

int solve() {
  pair<int, int> start;
  max_deep = 11;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> v[j][i];
      if (v[j][i] == 2)
        start = make_pair(j, i);
    }
  }
  int ans = dfs(start.first, start.second, 1);
  if (ans == 11)
    ans = -1;
  cout << ans << endl;
}

int main() {
  while (cin >> w >> h, w > 0 && h > 0) {
    solve();
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define Board vector<vector<int>>
using namespace std;
int d[5] = {0, -1, 0, 1, 0};

pair<int, int> get_position(Board b, int a) {
  for (unsigned int i = 0; i < b.size(); i++) {
    for (unsigned int j = 0; j < b[i].size(); j++) {
      if (b[i][j] == a) {
        return make_pair(i, j);
      }
    }
  }
  return make_pair(-1, -1);
}

int solve(int w, int h) {
  //	cout << "-----" << endl;
  Board v(w, vector<int>(h, 0));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> v[j][i];
    }
  }
  queue<pair<Board, int>> q;
  q.push(make_pair(v, 1));
  while (!q.empty()) {
    int count = q.front().second;
    if (count > 10) {
      cout << -1 << endl;
      return 0;
    }
    Board board = q.front().first;
    pair<int, int> current = get_position(board, 2);
    //		cout << current.first << ", " << current.second << endl;
    /*		for(int i=0;i<h;i++){
                            for(int j=0;j<w;j++){
                                    cout << board[j][i];
                            }
                            cout << endl;
                    }*/
    board[current.first][current.second] = 0;
    q.pop();
    for (int i = 0; i < 4; i++) {
      pair<int, int> c = current;
      Board b;
      for (auto boardx : board) {
        vector<int> bh;
        for (auto boardy : boardx) {
          bh.emplace_back(boardy);
        }
        b.emplace_back(bh);
      }
      if (c.first + d[i] < 0 || c.first + d[i] >= w ||
          c.second + d[i + 1] < 0 || c.second + d[i + 1] >= h) {
        continue;
      }
      if (board[c.first + d[i]][c.second + d[i + 1]] == 1)
        continue;
      while (c.first >= 0 && c.first < w && c.second >= 0 && c.second < h &&
             b[c.first][c.second] % 2 == 0) {
        c.first += d[i];
        c.second += d[i + 1];
      }
      if (c.first < 0 || c.first >= w || c.second < 0 || c.second >= h) {
        continue;
      }
      if (board[c.first][c.second] == 1) {
        b[c.first][c.second] = 0;
        c.first -= d[i];
        c.second -= d[i + 1];
        b[c.first][c.second] = 2;
        //				cout << " " << c.first << "," << c.second <<
        //endl;
        q.push(make_pair(b, count + 1));
      } else if (b[c.first][c.second] == 3) {
        cout << count << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}

int main() {
  int w, h;
  while (cin >> w >> h, w > 0 && h > 0) {
    solve(w, h);
  }
}