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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int hx[][6] = {{0, 0, 1, -1, -1, -1}, {1, 1, 1, -1, 0, 0}};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
int INF = 1 << 29;

inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}

int W, H;
int gx, gy;
struct Board {
  int grid[32][32];
  int cx, cy;
  Board() {}
  void load() {
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        grid[y][x] = in();
        if (grid[y][x] == 2)
          cx = x, cy = y;
        if (grid[y][x] == 3)
          gx = x, gy = y;
      }
    }
  }
  bool in_bounds(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }
  bool movable(int d) {
    int nx = cx + dx[d], ny = cy + dy[d];
    return in_bounds(nx, ny) && grid[ny][nx] != 1;
  }
  bool move(int d) {
    while (movable(d)) {
      if (is_end())
        break;
      grid[cy][cx] = 0;
      cx += dx[d];
      cy += dy[d];
    }
    if (is_end())
      return true;
    grid[cy][cx] = 2;
    int nx = cx + dx[d], ny = cy + dy[d];
    if (!in_bounds(nx, ny))
      return false;
    grid[ny][nx] = 0;
    return true;
  }
  bool is_end() { return cx == gx && cy == gy; }
  void dump() {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        printf("%c", "_#*G"[grid[i][j]]);
      }
      puts("");
    }
  }
};

int ans;
void dfs(Board b, int turn = 0) {
  if (b.is_end()) {
    ans = min(ans, turn);
    return;
  }
  if (turn >= 10)
    return;
  for (int d = 0; d < 4; d++) {
    if (!b.movable(d))
      continue;
    Board nb = b;
    if (!nb.move(d))
      continue;
    dfs(nb, turn + 1);
  }
}

int main() {
  while (W = in(), H = in()) {
    Board b;
    b.load();
    ans = INF;
    dfs(b);
    if (ans == INF)
      puts("-1");
    else
      printf("%d\\n", ans);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int hx[][6] = {{0, 0, 1, -1, -1, -1}, {1, 1, 1, -1, 0, 0}};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int INF = 1 << 29;

inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}

char gx, gy;
char W, H;
struct Board {
  char grid[22][22];
  char cx, cy;
  char turn;
  Board() : turn(0) {}
  void load() {
    for (int y = 0; y <= H + 1; y++) {
      for (int x = 0; x <= W + 1; x++) {
        grid[y][x] = 1;
      }
    }
    for (int y = 1; y <= H; y++) {
      for (int x = 1; x <= W; x++) {
        grid[y][x] = in();
        if (grid[y][x] == 2)
          cy = y, cx = x;
        if (grid[y][x] == 3)
          gy = y, gx = x;
      }
    }
  }
  bool in_bounds(int x, int y) { return 1 <= x && x <= W && 1 <= y && y <= H; }
  bool movable(int d) {
    int nx = cx + dx[d];
    int ny = cy + dy[d];
    return in_bounds(nx, ny) && grid[ny][nx] != 1;
  }
  bool move(int d) {
    if (!movable(d))
      return false;
    while (movable(d)) {
      if (grid[cy][cx] == 3)
        break;
      cx += dx[d];
      cy += dy[d];
    }
    grid[cy + dy[d]][cx + dx[d]] = 0;
    turn++;
    return true;
  }
  bool is_end() { return cy == gy && cx == gx; }
};

int main() {
  while (W = in(), H = in()) {
    Board b;
    b.load();
    queue<Board> Q;
    Q.push(b);
    bool ok = false;
    while (!Q.empty()) {
      Board &bf = Q.front();
      if (bf.is_end()) {
        ok = true;
        break;
      }
      if (bf.turn >= 10) {
        Q.pop();
        continue;
      }
      for (int d = 0; d < 4; d++) {
        if (!bf.movable(d))
          continue;
        Board nb = bf;
        nb.move(d);
        Q.push(nb);
      }
      Q.pop();
    }
    if (ok)
      printf("%d\\n", Q.front().turn);
    else
      puts("-1");
  }
  return 0;
}