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

#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using Board = vector<vector<int>>;

int W, H;

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

bool inBounds(int x, int y) {
  if (x < 0 || W <= x) {
    return false;
  }
  if (y < 0 || H <= y) {
    return false;
  }

  return true;
}

// ?§??????????????????´????????????????????¨?????????
bool mov(Board &b, int x, int y, int dx, int dy, int &nx, int &ny) {
  while (true) {
    x += dx;
    y += dy;

    if (!inBounds(x, y)) {
      return false;
    }

    if (b[y][x] == 1) {
      nx = x - dx;
      ny = y - dy;
      b[y][x] = 0;
      return true;
    }

    if (b[y][x] == 3) {
      nx = x;
      ny = y;
      return true;
    }
  }

  return true;
}

void dfs(Board b, int x, int y, int d, int &ans) {
  // ??´??????????????????
  if (b[y][x] == 3) {
    ans = min(ans, d);
    return;
  }

  if (d >= 10) {
    return;
  }

  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i];
    int ny = y + dy[i];

    if (!inBounds(nx, ny)) {
      continue;
    }
    if (b[ny][nx] == 1) {
      continue;
    }

    Board nb(b);

    if (!mov(nb, x, y, dx[i], dy[i], nx, ny)) {
      continue;
    }

    dfs(nb, nx, ny, d + 1, ans);
  }
}

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

    Board b(H, vector<int>(W, 0));

    int sx, sy;
    int gx, gy;

    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> b[i][j];
        if (b[i][j] == 2) {
          sx = j;
          sy = i;
        }

        if (b[i][j] == 3) {
          gx = j;
          gy = i;
        }
      }
    }

    int ans = 11;
    dfs(b, sx, sy, 0, ans);
    if (ans > 10) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using Board = vector<vector<int>>;

int W, H;

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

bool inBounds(int x, int y) {
  if (x < 0 || W <= x) {
    return false;
  }
  if (y < 0 || H <= y) {
    return false;
  }

  return true;
}

// ?§??????????????????´????????????????????¨?????????
bool mov(Board &b, int x, int y, int dx, int dy, int &nx, int &ny) {
  while (true) {
    x += dx;
    y += dy;

    if (!inBounds(x, y)) {
      return false;
    }

    if (b[y][x] == 1) {
      nx = x - dx;
      ny = y - dy;
      b[y][x] = 0;
      return true;
    }

    if (b[y][x] == 3) {
      nx = x;
      ny = y;
      return true;
    }
  }

  return true;
}

void dfs(Board b, int x, int y, int d, int &ans) {
  if (d > 10) {
    return;
  }

  // ??´??????????????????
  if (b[y][x] == 3) {
    ans = min(ans, d);
    return;
  }

  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i];
    int ny = y + dy[i];

    if (!inBounds(nx, ny)) {
      continue;
    }
    if (b[ny][nx] == 1) {
      continue;
    }

    Board nb(b);

    if (!mov(nb, x, y, dx[i], dy[i], nx, ny)) {
      continue;
    }

    dfs(nb, nx, ny, d + 1, ans);
  }
}

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

    Board b(H, vector<int>(W, 0));

    int sx, sy;
    int gx, gy;

    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> b[i][j];
        if (b[i][j] == 2) {
          sx = j;
          sy = i;
        }

        if (b[i][j] == 3) {
          gx = j;
          gy = i;
        }
      }
    }

    int ans = 11;
    dfs(b, sx, sy, 0, ans);
    if (ans > 10) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }

  return 0;
}