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

#include <iostream>
#include <queue>
using namespace std;
int W, H, sx, sy;
int A[21][21];
int inf = 1e9;

bool in(int x, int y) { return 1 <= x && x <= H && 1 <= y && y <= W; }

int dfs(int x, int y, int ti) {
  if (ti == 10)
    return inf;
  int res = inf;
  if (in(x + 1, y) && A[x + 1][y] != 1) {
    for (int i = x + 1; i <= H; i++) {
      if (A[i][y] == 1) {
        A[i][y] = 0;
        res = min(res, dfs(i - 1, y, ti + 1) + 1);
        A[i][y] = 1;
        break;
      }
      if (A[i][y] == 3)
        return 1;
    }
  }
  if (in(x - 1, y) && A[x - 1][y] != 1) {
    for (int i = x - 1; i >= 1; i--) {
      if (A[i][y] == 1) {
        A[i][y] = 0;
        res = min(res, dfs(i + 1, y, ti + 1) + 1);
        A[i][y] = 1;
        break;
      }
      if (A[i][y] == 3)
        return 1;
    }
  }
  if (in(x, y + 1) && A[x][y + 1] != 1) {
    for (int i = y + 1; i <= W; i++) {
      if (A[x][i] == 1) {
        A[x][i] = 0;
        res = min(res, dfs(x, i - 1, ti + 1) + 1);
        A[x][i] = 1;
        break;
      }
      if (A[x][i] == 3)
        return 1;
    }
  }
  if (in(x, y - 1) && A[x][y - 1] != 1) {
    for (int i = y - 1; i >= 1; i--) {
      if (A[x][i] == 1) {
        A[x][i] = 0;
        res = min(res, dfs(x, i + 1, ti + 1) + 1);
        A[x][i] = 1;
        break;
      }
      if (A[x][i] == 3)
        return 1;
    }
  }
  return res;
}

int main() {
  while (cin >> W >> H && W > 0) {
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> A[i][j];
        if (A[i][j] == 2) {
          sx = i;
          sy = j;
        }
      }
    }
    int ans = dfs(sx, sy, 0);
    cout << (ans != inf ? ans : -1) << endl;
  }
}


Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
int W, H, sx, sy;
int A[21][21];

struct Board {
  int field[21][21], x, y, ti;
};

bool in(int x, int y) { return 1 <= x && x <= H && 1 <= y && y <= W; }

int main() {
  while (cin >> W >> H && W > 0) {
    queue<Board> Q;
    Board init;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> init.field[i][j];
        if (init.field[i][j] == 2) {
          init.x = i;
          init.y = j;
        }
      }
    }
    init.ti = 0;
    Q.push(init);
    int ans = -1;
    while (!Q.empty() && ans == -1) {
      Board now = Q.front();
      if (now.ti == 10)
        break;
      Q.pop();
      if (in(now.x + 1, now.y) && now.field[now.x + 1][now.y] != 1) {
        for (int i = now.x + 1; i <= H; i++) {
          if (now.field[i][now.y] == 1) {
            Board ne = now;
            ne.field[i][ne.y] = 0;
            ne.x = i - 1;
            ne.ti++;
            Q.push(ne);
            break;
          }
          if (now.field[i][now.y] == 3) {
            ans = now.ti + 1;
            break;
          }
        }
      }
      if (in(now.x - 1, now.y) && now.field[now.x - 1][now.y] != 1) {
        for (int i = now.x - 1; i >= 1; i--) {
          if (now.field[i][now.y] == 1) {
            Board ne = now;
            ne.field[i][now.y] = 0;
            ne.x = i + 1;
            ne.ti++;
            Q.push(ne);
            break;
          }
          if (now.field[i][now.y] == 3) {
            ans = now.ti + 1;
            break;
          }
        }
      }
      if (in(now.x, now.y + 1) && now.field[now.x][now.y + 1] != 1) {
        for (int j = now.y + 1; j <= W; j++) {
          if (now.field[now.x][j] == 1) {
            Board ne = now;
            ne.field[now.x][j] = 0;
            ne.y = j - 1;
            ne.ti++;
            Q.push(ne);
            break;
          }
          if (now.field[now.x][j] == 3) {
            ans = now.ti + 1;
            break;
          }
        }
      }
      if (in(now.x, now.y - 1) && now.field[now.x][now.y - 1] != 1) {
        for (int j = now.y - 1; j >= 1; j--) {
          if (now.field[now.x][j] == 1) {
            Board ne = now;
            ne.field[now.x][j] = 0;
            ne.y = j + 1;
            ne.ti++;
            Q.push(ne);
            break;
          }
          if (now.field[now.x][j] == 3) {
            ans = now.ti + 1;
            break;
          }
        }
      }
    }
    cout << ans << endl;
  }
}