alexjercan/bugnet huggingface dataset:
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

constexpr int MAX = 20;
constexpr int NOT_FOUND = 11;

int w, h, ans;
int grid[MAX][MAX];

void dfs(int y, int x, int count) {
  int nx, ny;
  if (count >= ans)
    return;

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

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

    if (grid[ny][nx] == 1)
      continue;

    while (0 <= ny && ny < h && 0 <= nx && nx < w && grid[ny][nx] != 1) {
      if (grid[ny][nx] == 3) {
        ans = count;
        return;
      } else {
        ny += dy[i];
        nx += dx[i];
      }
    }

    if (grid[ny][nx] == 1 && 0 <= ny && ny < h && 0 <= nx && nx < w) {
      grid[ny][nx] = 0;
      dfs(ny - dy[i], nx - dx[i], count + 1);
      grid[ny][nx] = 1;
    }
  }
}

int main(int argc, char const *argv[]) {
  while (cin >> w >> h, w || h) {
    ans = NOT_FOUND;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> grid[i][j];
      }
    }

    int count = 1;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (grid[i][j] == 2) {
          dfs(i, j, count);
        }
      }
    }

    if (ans == NOT_FOUND)
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

constexpr int MAX = 20;
constexpr int NOT_FOUND = 100000;

int w, h, ans;

void dfs(int y, int x, int count, int grid[MAX][MAX]) {
  if (grid[y][x] == 3) {
    if (count < ans && count != 0)
      ans = count;
    return;
  }
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};

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

    if (grid[ny][nx] == 1)
      continue;

    while (0 <= ny && ny < h && 0 <= nx && nx < w && grid[ny][nx] != 1) {
      if (grid[ny][nx] == 3)
        break;
      nx += dx[i];
      ny += dy[i];
    }

    if (grid[ny][nx] == 1) {
      count++;
      grid[ny][nx] = 0;
      dfs(ny - dy[i], nx - dx[i], count, grid);
      grid[ny][nx] = 1;
    }

    if (grid[ny][nx] == 3) {
      count++;
      dfs(ny, nx, count, grid);
    }
  }
}

int main(int argc, char const *argv[]) {
  int grid[MAX][MAX];

  while (cin >> w >> h, w || h) {
    ans = NOT_FOUND;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        grid[i][j] = -1;
        ;
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> grid[i][j];
      }
    }

    int count = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (grid[i][j] == 2) {
          dfs(i, j, count, grid);
        }
      }
    }

    if (ans == NOT_FOUND || ans >= 11)
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

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

int saiki(int count, int dirx, int diry, vector<vector<int>> map, int x,
          int y) {
  int res = 11;
  while (true) {
    x += dirx;
    y += diry;
    if (x < 0 || x >= w || y < 0 || y >= h) {
      return 11;
    }
    if (map[y][x] == 1) {
      map[y][x] = 0;
      break;
    } else if (map[y][x] == 3) {
      return count;
    }
  }
  if (count == 10)
    return 11;
  x -= dirx;
  y -= diry;
  for (int i = 0; i < 4; i++) {
    if (x + dx[i] > -1 && x + dx[i] < w && y + dy[i] > -1 && y + dy[i] < h &&
        map[y + dy[i]][x + dx[i]] != 1) {
      res = min(res, saiki(count + 1, dx[i], dy[i], map, x, y));
    }
  }
  return res;
}

int main() {

  int temp;
  int x, y;
  vector<vector<int>> map;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      map.push_back(vector<int>());
      for (int j = 0; j < w; j++) {
        cin >> temp;
        map[i].push_back(temp);
        if (temp == 2) {
          x = j;
          y = i;
        }
      }
    }
    int res = 11;
    for (int i = 0; i < 4; i++) {
      if (x + dx[i] > -1 && x + dx[i] < w && y + dy[i] > -1 && y + dy[i] < h &&
          map[y + dy[i]][x + dx[i]] != 1) {
        res = min(res, saiki(1, dx[i], dy[i], map, x, y));
      }
    }
    if (res > 10)
      res = -1;
    cout << res << endl;
    map.clear();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

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

int saiki(int count, int dirx, int diry, vector<vector<int>> map, int x,
          int y) {
  if (count > 10)
    return 11;
  int res = 11;
  while (true) {
    x += dirx;
    y += diry;
    if (x < 0 || x >= w || y < 0 || y >= h) {
      return 11;
    }
    if (map[y][x] == 1) {
      map[y][x] = 0;
      break;
    } else if (map[y][x] == 3) {
      return count;
    }
  }
  x -= dirx;
  y -= diry;
  for (int i = 0; i < 4; i++) {
    if (x + dx[i] > -1 && x + dx[i] < w && y + dy[i] > -1 && y + dy[i] < h &&
        map[y + dy[i]][x + dx[i]] != 1) {
      res = min(res, saiki(count + 1, dx[i], dy[i], map, x, y));
    }
  }
  return res;
}

int main() {

  int temp;
  int x, y;
  vector<vector<int>> map;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      map.push_back(vector<int>());
      for (int j = 0; j < w; j++) {
        cin >> temp;
        map[i].push_back(temp);
        if (temp == 2) {
          x = j;
          y = i;
        }
      }
    }
    int res = 11;
    for (int i = 0; i < 4; i++) {
      if (x + dx[i] > -1 && x + dx[i] < w && y + dy[i] > -1 && y + dy[i] < h &&
          map[y + dy[i]][x + dx[i]] != 1) {
        res = min(res, saiki(1, dx[i], dy[i], map, x, y));
      }
    }
    if (res > 10)
      res = -1;
    cout << res << endl;
    map.clear();
  }

  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#define MAX_WH 20
#define MAX_VALUE 10000
using namespace std;

int w, h; // h:i,w:j
int si, sj, gi, gj;

int board[MAX_WH][MAX_WH];

int vx[4] = {1, -1, 0, 0};
int vy[4] = {0, 0, 1, -1};

int back(int i, int j, int sum) {
  int mi = MAX_VALUE;
  if (sum < 11) {
    for (int k = 0; k < 4; k++) {
      int pi, pj;
      pi = i + vx[k];
      pj = j + vy[k];
      if (-1 < pi && pi < h && -1 < pj && pj < w && board[pi][pj] != 1) {
        while (1) {
          if (0 > pi || pi > h - 1 || 0 > pj || pj > w - 1)
            break;
          if (pi == gi && pj == gj) {
            mi = sum + 1;
            break;
          }
          if (board[pi][pj] == 1) {
            board[pi][pj] = 0;
            mi = min(mi, back(pi - vx[k], pj - vy[k], sum + 1));
            board[pi][pj] = 1;
            break;
          }
          pi += vx[k];
          pj += vy[k];
        }
      }
    }
  }
  return mi;
}

void solve() {
  int n = back(si, sj, 0);
  if (n < 11) {
    printf("%d\\n", n);
  } else {
    printf("-1\\n");
  }
}

int main() {
  while (1) {
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int n;
        scanf(" %d", &n);
        board[i][j] = n;
        if (n == 2) {
          si = i;
          sj = j;
        }
        if (n == 3) {
          gi = i;
          gj = j;
        }
      }
    }
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#define MAX_WH 20
#define MAX_VALUE 10000
using namespace std;

int w, h; // h:i,w:j
int si, sj, gi, gj;

int board[MAX_WH][MAX_WH];

int vx[4] = {1, -1, 0, 0};
int vy[4] = {0, 0, 1, -1};

int back(int i, int j) {
  int mi = MAX_VALUE;
  for (int k = 0; k < 4; k++) {
    int pi, pj;
    pi = i + vx[k];
    pj = j + vy[k];
    if (-1 < pi && pi < h && -1 < pj && pj < w && board[pi][pj] != 1) {
      while (1) {
        if (0 > pi || pi > h - 1 || 0 > pj || pj > w - 1)
          break;
        if (pi == gi && pj == gj) {
          mi = 1;
          break;
        }
        if (board[pi][pj] == 1) {
          board[pi][pj] = 0;
          mi = min(mi, 1 + back(pi - vx[k], pj - vy[k]));
          board[pi][pj] = 1;
          break;
        }
        pi += vx[k];
        pj += vy[k];
      }
    }
  }
  return mi;
}

void solve() {
  int n = back(si, sj);
  if (n < 11) {
    printf("%d\\n", n);
  } else {
    printf("-1\\n");
  }
}

int main() {
  while (1) {
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int n;
        scanf(" %d", &n);
        board[i][j] = n;
        if (n == 2) {
          si = i;
          sj = j;
        }
        if (n == 3) {
          gi = i;
          gj = j;
        }
      }
    }
    solve();
  }
  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

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

int ans = 11;
vector<vector<int>> mp;
void dfs(int i, int j, int direction, int num, vector<vector<int>> mp) {
  while (mp[i + dx[direction]][j + dy[direction]] == 0) {
    i += dx[direction];
    j += dy[direction];
  }
  if (mp[i + dx[direction]][j + dy[direction]] == 3) {
    ans = min(num, ans);
    return;
  }
  if (mp[i + dx[direction]][j + dy[direction]] == 1 && num < 10) {
    mp[i + dx[direction]][j + dy[direction]] = 0;
    for (int k = 0; k < 4; k++) {
      if (mp[i + dx[k]][j + dy[k]] == 1)
        continue;
      if (mp[i + dx[k]][j + dy[k]] == -1)
        continue;
      // if(dx )
      dfs(i, j, k, num + 1, mp);
    }
  }
}

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    mp.resize(h + 2, vector<int>(w + 2, -1));
    int def_i, def_j;
    for (int i = 1; i < h + 1; i++) {
      for (int j = 1; j < w + 1; j++) {
        cin >> mp[i][j];
        if (mp[i][j] == 2) {
          def_i = i;
          def_j = j;
        }
      }
    }
    mp[def_i][def_j] = 0;
    for (int i = 0; i < 4; i++) {
      if (mp[def_i + dx[i]][def_j + dy[i]] != 0 &&
          mp[def_i + dx[i]][def_j + dy[i]] != 3)
        continue;
      dfs(def_i, def_j, i, 1, mp);
    }
    if (ans == 11)
      cout << -1 << endl;
    else
      cout << ans << endl;
    ans = 11;
    mp.clear();
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

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

int ans = 11;
vector<vector<int>> mp;
void dfs(int i, int j, int direction, int num, vector<vector<int>> mp) {
  if (mp[i + dx[direction]][j + dy[direction]] == 3) {
    ans = min(num, ans);
    return;
  }
  if (mp[i + dx[direction]][j + dy[direction]] == 0) {
    dfs(i + dx[direction], j + dy[direction], direction, num, mp);
    return;
  }
  if (mp[i + dx[direction]][j + dy[direction]] == 1 && num < 10) {
    mp[i + dx[direction]][j + dy[direction]] = 0;
    for (int k = 0; k < 4; k++) {
      if (mp[i + dx[k]][j + dy[k]] == 1 || mp[i + dx[k]][j + dy[k]] == -1)
        continue;
      dfs(i, j, k, num + 1, mp);
    }
  }
}

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    mp.resize(h + 2, vector<int>(w + 2, -1));
    int def_i, def_j;
    for (int i = 1; i < h + 1; i++) {
      for (int j = 1; j < w + 1; j++) {
        cin >> mp[i][j];
        if (mp[i][j] == 2) {
          def_i = i;
          def_j = j;
        }
      }
    }
    mp[def_i][def_j] = 0;
    for (int i = 0; i < 4; i++) {
      if (mp[def_i + dx[i]][def_j + dy[i]] != 0 &&
          mp[def_i + dx[i]][def_j + dy[i]] != 3)
        continue;
      dfs(def_i, def_j, i, 1, mp);
    }
    if (ans == 11)
      cout << -1 << endl;
    else
      cout << ans << endl;
    ans = 11;
    mp.clear();
  }
}
Problem p00725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int a[22][22], h, w, mi;
using namespace std;
void dfs(int y, int x, int depth) {
  if (depth >= mi)
    return;
  r(i, 4) {
    int y1 = y, x1 = x, c = 0, cc = 1;
    while (1) {
      if (i > 1) {
        if (y1 < 0 || y1 >= h) {
          c++;
          goto P;
        }
        if (cc && i == 2 && a[y1 + 1][x1] == 1) {
          c++;
          goto P;
        }
        if (cc && i == 3 && a[y1 - 1][x1] == 1) {
          c++;
          goto P;
        }
        if (y1 + 1 < h && i == 2 && a[y1 + 1][x1])
          goto P;
        if (y1 - 1 >= 0 && i == 3 && a[y1 - 1][x1])
          goto P;
      } else {
        if (x1 < 0 || x1 >= w) {
          c++;
          goto P;
        }
        if (cc && i == 0 && a[y1][x1 + 1] == 1) {
          c++;
          goto P;
        }
        if (cc && i == 1 && a[y1][x1 - 1] == 1) {
          c++;
          goto P;
        }
        if (x1 + 1 < w && i == 0 && a[y1][x1 + 1])
          goto P;
        if (x1 - 1 >= 0 && i == 1 && a[y1][x1 - 1])
          goto P;
      }
      y1 += dy[i];
      x1 += dx[i];
      cc = 0;
    }
  P:;
    int y2 = y1 + dy[i], x2 = x1 + dx[i];
    if (!c) {
      if (a[y2][x2] == 3 || mi < depth + 1) {
        mi = depth + 1;
        return;
      } else {
        a[y2][x2] = 0;
        dfs(y1, x1, depth + 1);
        a[y2][x2] = 1;
      }
    }
  }
}
int main() {
  int hh, ww;
  while (cin >> w >> h, w) {
    r(i, h) r(j, w) {
      cin >> a[i][j];
      if (a[i][j] == 2) {
        a[i][j] = 0;
        hh = i;
        mi = 11;
        ww = j;
      }
    }
    dfs(hh, ww, 0);
    if (mi == 11)
      cout << -1 << endl;
    else
      cout << mi << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int a[22][22], h, w, mi;
using namespace std;
void dfs(int y, int x, int depth) {
  if (depth >= mi)
    return;
  r(i, 4) {
    int y1 = y, x1 = x, c = 0;
    while (1) {
      if (i > 1) {
        if (y1 < 0 || y1 >= h) {
          c++;
          goto P;
        }
        if (i == 2 && a[y1 + 1][x1])
          goto P;
        if (i == 3 && a[y1 - 1][x1])
          goto P;
      } else {
        if (x1 < 0 || x1 >= w) {
          c++;
          goto P;
        }
        if (i == 0 && a[y1][x1 + 1])
          goto P;
        if (i == 1 && a[y1][x1 - 1])
          goto P;
      }
      y1 += dy[i];
      x1 += dx[i];
    }
  P:;
    int y2 = y1 + dy[i], x2 = x1 + dx[i];
    if (!c) {
      if (a[y2][x2] == 3) {
        mi = depth + 1;
        return;
      } else {
        a[y2][x2] = 0;
        dfs(y1, x1, depth + 1);
        a[y2][x2] = 1;
      }
    }
  }
}
int main() {
  int hh, ww;
  while (cin >> w >> h, w) {
    r(i, h) r(j, w) {
      cin >> a[i][j];
      if (a[i][j] == 2) {
        hh = i;
        mi = 10;
        ww = j;
      }
    }
    dfs(hh, ww, 0);
    cout << mi << endl;
  }
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

#define rep(i, n) for (int i = 0; i < n; i++)

int w, h;
int f[20][20];
int ans;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

void rec(int n, int x, int y) {

  if (n > 10)
    return;

  rep(i, 4) {
    int mx = x + dx[i];
    int my = y + dy[i];

    if (f[my][mx] == 1)
      continue;
    if (0 > mx || mx >= w)
      continue;
    if (0 > my || my >= h)
      continue;

    while (1) {
      if (f[my][mx] == 3) {
        if (n < ans)
          ans = n;
        return;
      }

      mx += dx[i], my += dy[i];
      if (0 > mx || mx >= w)
        break;
      if (0 > my || my >= h)
        break;

      if (f[my][mx] == 1) {
        f[my][mx] = 0;
        rec(n + 1, mx - dx[i], my - dy[i]);
        f[my][mx] = 1;
        break;
      }
    }
  }
}

int main() {

  int sx, sy;

  while (std::cin >> w >> h, (w || h)) {

    rep(i, h) {
      rep(j, w) {
        std::cin >> f[i][j];
        if (f[i][j] == 2) {
          sx = j, sy = i;
        }
      }
    }
    ans = 11;
    rec(1, sx, sy);
    if (ans > 10)
      ans = -1;
    std::cout << ans << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

#define rep(i, n) for (int i = 0; i < n; i++)

int w, h;
int f[20][20];
int ans;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

void rec(int n, int x, int y) {

  rep(i, 4) {
    int mx = x + dx[i];
    int my = y + dy[i];

    if (f[my][mx] == 1)
      continue;
    if (0 > mx || mx >= w)
      continue;
    if (0 > my || my >= h)
      continue;

    while (1) {
      if (f[my][mx] == 3) {
        ans = n;
        return;
      }

      mx += dx[i], my += dy[i];
      if (0 > mx || mx >= w)
        break;
      if (0 > my || my >= h)
        break;

      if (f[my][mx] == 1) {
        f[my][mx] = 0;
        rec(n + 1, mx - dx[i], my - dy[i]);
        f[my][mx] = 1;
        break;
      }
    }
  }
}

int main() {

  int sx, sy;

  while (std::cin >> w >> h, (w || h)) {

    rep(i, h) {
      rep(j, w) {
        std::cin >> f[i][j];
        if (f[i][j] == 2) {
          sx = j, sy = i;
        }
      }
    }
    ans = 11;
    rec(1, sx, sy);
    if (ans > 10)
      ans = -1;
    std::cout << ans << std::endl;
  }
  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

int h, w;
vvi g;

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

int const inf = 1 << 28;

inline bool in(int x, int y) { return 0 <= x && 0 <= y && x < w && y < h; }

int ans = inf;

void rec(int x, int y, int depth) {
  if (depth >= 11)
    return;
  rep(d, 4) {
    int cx = x, cy = y;
    if (!in(cx + dx[d], cy + dy[d]) || g[cy + dy[d]][cx + dx[d]] == 1)
      continue;
    while (1) {
      int nx = cx + dx[d];
      int ny = cy + dy[d];
      if (!in(nx, ny))
        goto next;
      if (g[cy][cx] == 0) {
        if (g[ny][nx] == 0) {
          cx = nx, cy = ny;
          continue;
        }
        if (g[ny][nx] == 1) {
          g[ny][nx] = 0;
          rec(cx, cy, depth + 1);
          g[ny][nx] = 1;
          break;
        }
        if (g[ny][nx] == 3) {
          ans = min(ans, depth + 1);
          break;
        }
      }
      if (g[cy][cx] == 3) {
        ans = min(ans, depth);
      }
    }
  next:;
  }
}

void solve() {
  rep(i, h) rep(j, w) if (g[i][j] == 2) {
    g[i][j] = 0;
    rec(j, i, 0);
  }
}

int main() {
  while (cin >> w >> h && w) {
    ans = inf;
    g.assign(h, vi(w));
    rep(i, h) rep(j, w) cin >> g[i][j];
    solve();
    cout << (ans > 10 ? -1 : ans) << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define iter(it, c) for (auto it = c.begin(); it != c.end(); it++)

#define s system

int main(void) {
  s("ruby");
  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  while (true) {
    int w, h;
    cin >> w >> h;
    if (!w)
      break;
    VV<> a(h, V<>(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 >> a[i][j];
        if (a[i][j] == 2)
          si = i, sj = j;
        if (a[i][j] == 3)
          ti = i, tj = j;
      }
    V<> di{1, 0, -1, 0}, dj{0, 1, 0, -1};
    auto in = [&](int i, int j) -> bool {
      return 0 <= i and i < h and 0 <= j and j < w;
    };
    int res = 11;
    auto dfs = [&](const auto &dfs, int k, int i, int j, int d) -> void {
      if (k == 10)
        return;
      int ni = i + di[d], nj = j + dj[d];
      if (!in(ni, nj) or a[ni][nj] == 1)
        return;
      i = ni, j = nj;
      while (true) {
        if (i == ti and j == tj) {
          res = min(res, k + 1);
          return;
        }
        ni = i + di[d], nj = j + dj[d];
        if (!in(ni, nj))
          return;
        if (a[ni][nj] == 1) {
          a[ni][nj] = 0;
          for (int d = 0; d < 4; ++d) {
            dfs(dfs, k + 1, i, j, d);
          }
          a[ni][nj] = 1;
          return;
        }
        i = ni, j = nj;
      }
    };
    for (int d = 0; d < 4; ++d)
      dfs(dfs, 0, si, sj, d);
    cout << (res == 11 ? -1 : res) << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  while (true) {
    int w, h;
    cin >> w >> h;
    if (!w)
      break;
    VV<> a(h, V<>(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 >> a[i][j];
        if (a[i][j] == 2)
          si = i, sj = j;
        if (a[i][j] == 3)
          ti = i, tj = j;
      }
    V<> di{1, 0, -1, 0}, dj{0, 1, 0, -1};
    auto in = [&](int i, int j) -> bool {
      return 0 <= i and i < h and 0 <= j and j < w;
    };
    int res = 11;
    for (int x = 0; x < 1 << 20; ++x) {
      auto b = a;
      int i = si, j = sj;
      for (int k = 0; k < 10; ++k) {
        int d = x >> 2 * k & 3;
        int ni = i + di[d], nj = j + dj[d];
        if (!in(ni, nj) or b[ni][nj] == 1)
          break;
        i = ni, j = nj;
        bool ok = true;
        while (true) {
          if (i == ti and j == tj) {
            res = min(res, k + 1);
            ok = false;
            break;
          }
          ni = i + di[d], nj = j + dj[d];
          if (!in(ni, nj)) {
            ok = false;
            break;
          }
          if (b[ni][nj] == 1) {
            b[ni][nj] = 0;
            break;
          }
          i = ni, j = nj;
        }
        if (!ok)
          break;
      }
    }
    cout << (res == 11 ? -1 : res) << '\\n';
  }
}