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

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

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

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int w, h;
  while (cin >> w >> h, w | h) {
    vector<vector<int>> b(h, vector<int>(w));
    int si = -1, sj = -1, ti = -1, tj = -1;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> b[i][j];
        if (b[i][j] == 2) {
          si = i;
          sj = j;
          b[i][j] = 0;
        }
        if (b[i][j] == 3) {
          ti = i;
          tj = j;
          b[i][j] = 0;
        }
      }
    }
    int res = -1;
    vector<int> can(1 << 20);
    for (int i = 0; i < (1 << 20); i++) {
      if (can[i])
        continue;
      int ni = si, nj = sj;
      vector<int> ci, cj;
      int out = -1;
      for (int j = 0; j < 10; j++) {
        int to = (i >> (j * 2)) & 3;
        bool ok = false, ng = false;
        ni += dx[to];
        nj += dy[to];
        if (ni < 0 || h <= ni || nj < 0 || w <= nj || b[ni][nj] != 0) {
          out = j + 1;
          break;
        }
        if (ni == ti && nj == tj) {
          res = res == -1 ? j + 1 : min(res, j + 1);
          break;
        }
        while (b[ni][nj] == 0) {
          ni += dx[to];
          nj += dy[to];
          if (ni < 0 || h <= ni || nj < 0 || w <= nj) {
            ng = true;
            break;
          }
          if (ni == ti && nj == tj) {
            ok = true;
            break;
          }
        }
        if (ok) {
          res = res == -1 ? j + 1 : min(res, j + 1);
          out = j + 1;
          break;
        }
        if (ng) {
          out = j + 1;
          break;
        }
        b[ni][nj] = 0;
        ci.push_back(ni);
        cj.push_back(nj);
        ni -= dx[to];
        nj -= dy[to];
      }
      for (int j = 0; j < (int)ci.size(); j++) {
        b[ci[j]][cj[j]] = 1;
      }
      if (out != -1) {
        for (int x = 0; x < (1 << (20 - out * 2)); x++) {
          can[(x << (out * 2)) + i] = 1;
        }
      }
    }
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

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

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int w, h;
  while (cin >> w >> h, w | h) {
    vector<vector<int>> b(h, vector<int>(w));
    int si = -1, sj = -1, ti = -1, tj = -1;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> b[i][j];
        if (b[i][j] == 2) {
          si = i;
          sj = j;
          b[i][j] = 0;
        }
        if (b[i][j] == 3) {
          ti = i;
          tj = j;
          b[i][j] = 0;
        }
      }
    }
    int res = -1;
    for (int i = 0; i < (1 << 20); i++) {
      int ni = si, nj = sj;
      vector<int> ci, cj;
      for (int j = 0; j < 10; j++) {
        if (res != -1 && j >= res)
          break;
        int to = (i >> (j * 2)) & 3;
        bool ok = false, ng = false;
        ni += dx[to];
        nj += dy[to];
        if (ni < 0 || h <= ni || nj < 0 || w <= nj || b[ni][nj] != 0)
          break;
        if (ni == ti && nj == tj) {
          res = res == -1 ? j + 1 : min(res, j + 1);
          break;
        }
        while (b[ni][nj] == 0) {
          ni += dx[to];
          nj += dy[to];
          if (ni < 0 || h <= ni || nj < 0 || w <= nj) {
            ng = true;
            break;
          }
          if (ni == ti && nj == tj) {
            ok = true;
            break;
          }
        }
        if (ok) {
          res = res == -1 ? j + 1 : min(res, j + 1);
          break;
        }
        if (ng) {
          break;
        }
        b[ni][nj] = 0;
        ci.push_back(ni);
        cj.push_back(nj);
        ni -= dx[to];
        nj -= dy[to];
      }
      for (int j = 0; j < (int)ci.size(); j++) {
        b[ci[j]][cj[j]] = 1;
      }
    }
    printf("%d\\n", res);
  }
  return 0;
}