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

#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

int w, h;
int mindepth = 0;
int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

bool isValid(int x, int y) { return !(x < 0 || y < 0 || x >= w || y >= h); }

void dfs_search(vector<vector<int>> map, int x, int y, int depth) {
  if (depth >= 10 || depth >= mindepth)
    return;
  for (int i = 0; i < 4; i++) {
    vector<vector<int>> currentmap = map;
    int cx = x, cy = y;
    if (!isValid(cx + d[i][1], cy + d[i][0]) ||
        currentmap[cy + d[i][0]][cx + d[i][1]] == 1)
      continue;
    for (; isValid(cx, cy); cx += d[i][1], cy += d[i][0]) {
      if (currentmap[cy][cx] == 1) {
        currentmap[cy][cx] = 0;
        dfs_search(currentmap, cx - d[i][1], cy - d[i][0], depth + 1);
        break;
      }
      if (currentmap[cy][cx] == 3) {
        mindepth = depth + 1;
        break;
      }
    }
  }
}

int main(void) {

  while (true) {
    cin >> w >> h;
    if (!(w | h))
      break;
    mindepth = 1 << 30;
    vector<vector<int>> startmap(h, vector<int>(w, 0));
    int sx, sy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> startmap[i][j];
        if (startmap[i][j] == 2) {
          startmap[i][j] = 0;
          sx = j;
          sy = i;
        }
      }
    }
    dfs_search(startmap, sx, sy, 0);
    if (mindepth == (1 << 30))
      mindepth = -1;
    cout << mindepth << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

int w, h;
int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

struct state {
  int map[20][20];
  int x, y, vx, vy, depth;
  bool isClear;
  bool valid() {
    if (x < 0 || x >= w || y < 0 || y >= h)
      return false;
    return true;
  }
  bool move() {
    if (valid() && map[y + vy][x + vx] == 1)
      return false;
    while (true) {
      x += vx;
      y += vy;
      if (!valid())
        return false;
      if (map[y][x] == 1) {
        map[y][x] = 0;
        x -= vx;
        y -= vy;
        return true;
      } else if (map[y][x] == 3) {
        isClear = true;
        return true;
      }
    }
  }
};

int main(void) {

  while (true) {
    cin >> w >> h;
    if (!(w | h))
      break;
    state start;

    start.isClear = false;
    start.depth = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int tmp;
        cin >> tmp;
        if (tmp == 2) {
          tmp = 0;
          start.x = j;
          start.y = i;
        }
        start.map[i][j] = tmp;
      }
    }

    queue<state> q;
    q.push(start);

    int ans = -1;

    while (!q.empty()) {

      state s = q.front();
      q.pop();

      if (s.isClear) {
        ans = s.depth;
        break;
      }
      if (s.depth == 10)
        continue;
      if (s.depth > 10)
        break;

      for (int i = 0; i < 4; i++) {
        state news = s;
        news.depth++;
        news.vy = d[i][0];
        news.vx = d[i][1];
        if (news.move())
          q.push(news);
      }
    }

    cout << ans << endl;
  }

  return 0;
}