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

#include <iostream>

using namespace std;

int w, h;
int f[21][21];
int ans;

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

void rec(int n, int x, int y) {
  if (n >= ans) {
    return;
  }

  for (int i = 0; i < 4; i++) {
    int mx = x, my = y;
    mx += dx[i];
    my += dy[i];
    if (mx < 0 || mx >= w) {
      continue;
    }
    if (my < 0 || my >= h) {
      continue;
    }
    if (f[my][mx] == 1) {
      continue;
    }

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

      mx += dx[i];
      my += dy[i];
      if (mx < 0 || mx >= w) {
        break;
      }
      if (my < 0 || 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() {
  while (cin >> w >> h, (w || h)) {
    int sx, sy;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> f[y][x];
        if (f[y][x] == 2) {
          sx = x;
          sy = y;
        }
      }
    }

    ans = 11;
    rec(1, sx, sy);
    if (ans > 10) {
      ans = -1;
    }
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
int mod(ll a) {
  int c = a % 1000000007;
  if (c >= 0)
    return c;
  else
    return c + 1000000007;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
#define inf 100000000 /*10^8*/

/////////////////////////////////

int w, h;
int room[21][21];
int sx, sy, gx, gy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int minn = inf;
void dfs(int x, int y, int count, int r[21][21]) {
  count++;
  if (count >= minn)
    return;
  int ro[21][21];
  for (int i = 0; i <= h; i++)
    for (int j = 0; j <= w; j++)
      ro[i][j] = r[i][j];
  for (int i = 0; i <= 3; i++) {
    int xx = x + dx[i], yy = y + dy[i];
    if (0 <= xx && xx <= h - 1 && 0 <= yy && yy <= w - 1 && ro[xx][yy] == 1)
      continue;
    while (0 <= xx && xx <= h - 1 && 0 <= yy && yy <= w - 1 &&
           ro[xx][yy] != 1) {
      if (ro[xx][yy] == 3) {
        minn = min(minn, count);
        return;
      }
      xx += dx[i], yy += dy[i];
    }
    if (xx < 0 || h - 1 < xx || yy < 0 || w - 1 < yy)
      continue;
    if (ro[xx][yy] == 1) {
      ro[xx][yy] = 0;
      dfs(xx - dx[i], yy - dy[i], count, ro);
    }
  }
  return;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0)
      break;
    minn = inf;
    for (int i = 0; i <= h - 1; i++)
      for (int j = 0; j <= w - 1; j++) {
        cin >> room[i][j];
        if (room[i][j] == 2)
          sx = i, sy = j, room[i][j] = 0;
      }
    dfs(sx, sy, 0, room);
    if (minn > 10)
      minn = -1;
    cout << minn << endl;
  }
  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define MAX_W 20
#define MAX_H 20
#define INF (1e6)

int w, h;
int stage[MAX_H][MAX_W];

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

int sx, sy, gx, gy;

int dfs(int x, int y, int c) {
  int res = INF;
  if (c >= 10)
    return res;
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || ny < 0 || nx >= w || ny >= h)
      continue;
    if (stage[ny][nx] == 1)
      continue;
    if (stage[ny][nx] == 3)
      return c + 1;
    while (1) {
      nx += dx[i];
      ny += dy[i];
      if (nx < 0 || ny < 0 || nx >= w || ny >= h)
        break;
      if (stage[ny][nx] == 3)
        return c + 1;
      else if (stage[ny][nx] == 1) {
        stage[ny][nx] = 0;
        res = std::min(res, dfs(nx - dx[i], ny - dy[i], c + 1));
        stage[ny][nx] = 1;
        break;
      }
    }
  }
  return res;
}

int main() {
  while (1) {
    std::cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        std::cin >> stage[i][j];
        if (stage[i][j] == 2) {
          sx = j;
          sy = i;
          stage[sy][sx] = 0;
        } else if (stage[i][j] == 3) {
          gx = j;
          gy = i;
        }
      }
    }
    int res = (dfs(sx, sy, 0) == INF) ? -1 : dfs(sx, sy, 0);
    std::cout << res << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stack>

#define W_MAX 20
#define H_MAX 20
#define Wall 1

typedef std::pair<int, int> P;

struct P_impl {
  int first, second, count;
  P_impl(int a, int b, int c) : first(a), second(b), count(c) {}
};

int h, w, V[H_MAX][W_MAX];

int dfs(P S, P G) {
  std::stack<P_impl> st;
  st.push(P_impl(S.first, S.second, 0));
  while (!st.empty()) {
    P_impl p = st.top();
    st.pop();
    if (p.first == G.first && p.second == G.second) {
      return p.count;
      break;
    }
    for (int i = 1; i < h; ++i) {
      if (V[p.first + i][p.second] == Wall) {
        st.push(P_impl(p.first + i, p.second, p.count + 1));
        V[p.first + i][p.second] = 0;
      } else if (V[p.first - i][p.second] == Wall) {
        st.push(P_impl(p.first + i, p.second, p.count + 1));
        V[p.first - i][p.second] = 0;
      }
    }
    for (int i = 1; i < w; ++i) {
      if (V[p.first][p.second + i] == Wall) {
        st.push(P_impl(p.first + i, p.second, p.count + 1));
        V[p.first][p.second + i] = 0;
      } else if (V[p.first][p.second - i] == Wall) {
        st.push(P_impl(p.first + i, p.second, p.count + 1));
        V[p.first][p.second - i] = 0;
      }
    }
  }
  if (st.empty())
    return -1;
}

int main() {
  while (1) {
    int si, sj, gi, gj, ans;
    std::cin >> h >> w;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        std::cin >> V[i][j];
        if (V[i][j] == 2)
          si = i;
        sj = j;
        if (V[i][j] == 3)
          gi = i;
        gj = j;
      }
    }
    ans = dfs(P(si, sj), P(gi, gj));
    std::cout << ans << std::endl;
  }
  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;
const static int dy[] = {0, 1, 0, -1};
const static int dx[] = {1, 0, -1, 0};
const static int INF = 1 << 30;

int w, h, mas[20][20];
Pi S, G;

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

int rec(int x, int y, int d, int cost) {
  if (cost > 10)
    return (INF);
  if (make_pair(x, y) == G)
    return (cost);
  int ret = INF;
  if (d == -1) {
    for (int i = 0; i < 4; i++) {
      if (!In(x + dx[i], y + dy[i]))
        continue;
      if (In(x + dx[i], y + dy[i]) && mas[y + dy[i]][x + dx[i]] == 1)
        continue;
      ret = min(ret, rec(x + dx[i], y + dy[i], i, cost + 1));
    }
  } else {
    x += dx[d], y += dy[d];
    if (In(x, y)) {
      if (mas[y][x] == 1) {
        mas[y][x] = 0;
        ret = rec(x - dx[d], y - dy[d], -1, cost);
        mas[y][x] = 1;
      } else {
        ret = rec(x, y, d, cost);
      }
    }
  }
  return (ret);
}

int main() {
  while (cin >> w >> h, w) {
    S = make_pair(-1, -1);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> mas[i][j];
        if (mas[i][j] == 2) {
          S = make_pair(j, i);
        } else if (mas[i][j] == 3) {
          G = make_pair(j, i);
        }
      }
    }
    int ret = rec(S.first, S.second, -1, 0);
    if (S == make_pair(-1, -1) || ret == INF)
      cout << -1 << endl;
    else
      cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int w, h, mas[20][20];
pair<int, int> Start, Goal;
const int dy[] = {0, 1, -1, 0}, dx[] = {1, 0, 0, -1};

int rec(int x, int y, int d = -1) {
  int ret = 114514;

  if (make_pair(x, y) == Goal) {
    return (0);
  } else if (~d) {
    x += dx[d];
    y += dy[d];
    if (x < 0 || x >= w || y < 0 || y >= h) {
      ret = 114514;
    } else if (mas[y][x] == 1) {
      mas[y][x] = 0;
      ret = rec(x - dx[d], y - dy[d], -1);
      mas[y][x] = 1;
    } else {
      ret = rec(x, y, d);
    }
  } else {
    for (int i = 0; i < 4; i++) {
      ret = min(ret, rec(y + dy[i], x + dx[i], i) + 1);
    }
  }
  return (ret);
}

int main() {
  while (cin >> w >> h, w) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> mas[i][j];
        if (mas[i][j] == 2)
          Start = make_pair(j, i);
        else if (mas[i][j] == 3)
          Goal = make_pair(j, i);
      }
    }
    int Cost = rec(Start.first, Start.second);
    if (Cost > 10)
      cout << -1 << endl;
    else
      cout << Cost << endl;
  }
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
using namespace std;
int w, h, ans;
int map[20][20];
int mvx[4] = {0, 0, 1, -1};
int mvy[4] = {1, -1, 0, 0};

void getStartPoint(int *sx, int *sy) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (map[i][j] == 2) {
        *sx = j;
        *sy = i;
        return;
      }
    }
  }
}

void dfs(int sx, int sy, int count) {
  if (count >= ans)
    return;
  for (int i = 0; i < 4; i++) {
    int mx = sx + mvx[i];
    int my = sy + mvy[i];
    if ((mx < 0) || (mx >= w) || (my < 0) || (my >= h))
      continue;
    if (map[my][mx] == 1) {
      continue;
    }
    while (true) {
      if (map[my][mx] == 3) {
        ans = count;
        return;
      }
      mx += mvx[i];
      my += mvy[i];
      if ((mx < 0) || (mx >= w) || (my < 0) || (my >= h)) {
        break;
      }
      if (map[my][mx] == 1) {
        map[my][mx] = 0;
        dfs(mx - mvx[i], my - mvy[i], count + 1);
        map[my][mx] = 1;
        break;
      }
    }
  }
}

int main() {
  int sx, sy;
  while (true) {
    cin >> w >> h;
    if ((w == 0) && (h == 0))
      break;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> map[i][j];
      }
    }
    ans = 11;
    getStartPoint(&sx, &sy);
    dfs(sx, sy, 1);
    if (ans > 10)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
int w, h, ans;
int map[20][20];
int mvx[4] = {0, 0, 1, -1};
int mvy[4] = {1, -1, 0, 0};

void getStartPoint(int *sx, int *sy) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (map[i][j] == 2) {
        *sx = j;
        *sy = i;
        return;
      }
    }
  }
}

void dfs(int sx, int sy, int count) {
  for (int i = 0; i < 4; i++) {
    int mx = sx + mvx[i];
    int my = sy + mvy[i];
    if ((mx < 0) || (mx >= w) || (my < 0) || (my >= h))
      continue;
    if (map[my][mx] == 1) {
      continue;
    }
    bool flag = true;
    while (map[my][mx] != 1) {
      if (map[my][mx] == 3) {
        ans = min(ans, count);
        flag = false;
        break;
      }

      mx += mvx[i];
      my += mvy[i];
      if ((mx < 0) || (mx >= w) || (my < 0) || (my >= h)) {
        flag = false;
        break;
      }
    }
    if (flag) {
      map[my][mx] = 0;
      dfs(mx - mvx[i], my - mvy[i], count + 1);
      map[my][mx] = 1;
    }
  }
}

int main() {
  int sx, sy;
  while (true) {
    cin >> w >> h;
    if ((w == 0) && (h == 0))
      break;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> map[i][j];
      }
    }
    ans = 100000;
    getStartPoint(&sx, &sy);
    dfs(sx, sy, 1);
    if (ans > 10)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int w, h;
int G[100][100];
int sx, sy;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
int ans;

void dfs(int px, int py, int cnt) {
  if (cnt > 10)
    return;
  for (int i = 0; i < 4; i++) {

    int nx = px + dx[i], ny = py + dy[i];

    if (!(0 <= nx && nx < w && 0 <= ny && ny < h && G[ny][nx] != 1))
      continue;
    while (1) {
      if (G[ny][nx] == 3) {
        ans = min(cnt, ans);
      }
      nx += dx[i], ny += dy[i];
      if (!(0 <= nx && nx < w && 0 <= ny && ny < h))
        break;
      if (G[ny][nx] == 1) {
        G[ny][nx] = 0;
        dfs(nx - dx[i], ny - dy[i], cnt + 1);
        G[ny][nx] = 1;
        break;
      }
    }
  }
}

int main() {
  while (cin >> w >> h, w + h) {

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> G[i][j];
        if (G[i][j] == 2) {
          sx = j;
          sy = i;
        }
      }
    }

    ans = 11;
    dfs(sx, sy, 1);
    cout << (ans > 10 ? -1 : ans) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int w, h;
int G[100][100];
int sx, sy;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
int ans;

void dfs(int px, int py, int cnt) {
  for (int i = 0; i < 4; i++) {

    int nx = px + dx[i], ny = py + dy[i];

    if (!(0 <= nx && nx < w && 0 <= ny && ny < h && G[ny][nx] != 1))
      continue;
    while (1) {
      if (G[ny][nx] == 3) {
        ans = min(ans, cnt);
      }
      nx += dx[i], ny += dy[i];
      if (!(0 <= nx && nx < w && 0 <= ny && ny < h))
        break;
      if (G[ny][nx] == 1) {
        G[ny][nx] = 0;
        dfs(nx - dx[i], ny - dy[i], cnt + 1);
        G[ny][nx] = 1;
        break;
      }
    }
  }
}

int main() {
  while (cin >> w >> h, w + h) {

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> G[i][j];
        if (G[i][j] == 2) {
          sx = j;
          sy = i;
        }
      }
    }

    ans = 11;
    dfs(sx, sy, 1);
    cout << (ans > 10 ? -1 : ans) << endl;
  }
}
Problem p00725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int w, h;
bool isRange(int i, int j) { return 0 <= i && i < w && 0 <= j && j < h; }
int dw[] = {-1, 0, 1, 0};
int dh[] = {0, 1, 0, -1};
int dfs(pair<int, int> p, const pair<int, int> g, vector<vector<int>> &grid,
        int turn) {
  if (turn == 11)
    return 100;
  int res = 100;
  for (int i = 0; i < 4; i++) {
    if (isRange(p.first + dw[i], p.second + dh[i]) &&
        grid[p.first + dw[i]][p.second + dh[i]] == 1)
      continue;
    for (auto j = p; isRange(j.first, j.second);
         j.first += dw[i], j.second += dh[i]) {
      if (j == g) {
        return turn;
      }
      if (grid[j.first][j.second] == 1) {
        grid[j.first][j.second] = 0;
        j.first -= dw[i];
        j.second -= dh[i];
        res = min(res, dfs(j, g, grid, turn + 1));
        grid[j.first + dw[i]][j.second + dh[i]] = 1;
        break;
      }
    }
  }
  return res;
}
int main() {
  while (cin >> w >> h, w) {
    pair<int, int> s;
    pair<int, int> g;
    vector<vector<int>> grid(w, vector<int>(h, 0));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int buf;
        cin >> buf;
        grid[j][i] = buf;
        if (buf == 2)
          s = make_pair(j, i);
        if (buf == 3)
          g = make_pair(j, i);
      }
    }
    int res = dfs(s, g, grid, 1);
    if (res > 11)
      res = -1;
    cout << res << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int w, h;
bool isRange(int i, int j) { return 0 <= i && i < w && 0 <= j && j < h; }

int dw[] = {-1, 0, 1, 0};
int dh[] = {0, 1, 0, -1};
int dfs(pair<int, int> p, const pair<int, int> g, vector<vector<int>> &grid,
        int turn) {
  if (turn == 11)
    return 100;
  int res = 100;
  for (int i = 0; i < 4; i++) {
    for (auto j = p; isRange(j.first, j.second);
         j.first += dw[i], j.second += dh[i]) {
      if (j == g) {
        return turn;
      }
      if (grid[j.first][j.second] == 1) {
        grid[j.first][j.second] = 0;
        j.first -= dw[i];
        j.second -= dh[i];
        res = min(res, dfs(j, g, grid, turn + 1));
        grid[j.first + dw[i]][j.second + dh[i]] = 1;
        break;
      }
    }
  }
  return res;
}
int main() {
  while (cin >> w >> h, w) {
    pair<int, int> s;
    pair<int, int> g;
    vector<vector<int>> grid(w, vector<int>(w, 0));
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++) {
        int buf;
        cin >> buf;
        grid[i][j] = buf;
        if (buf == 2)
          s = make_pair(i, j);
        if (buf == 3)
          g = make_pair(i, j);
      }
    }
    int res = dfs(s, g, grid, 1);
    if (res > 11)
      res = -1;
    cout << res << endl;
  }
  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int w, h;
int sx, sy;
int res;
int table[30][30];
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

void dfs(int x, int y, int cnt) {
  if (cnt >= res)
    return;

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

    if (nx < 0 || w <= nx || ny < 0 || h <= ny)
      continue;
    else if (table[ny][nx] == 1)
      continue;
    else if (table[ny][nx] == 3) {
      res = cnt;
      return;
    }

    while (true) {
      nx += dx[dir];
      ny += dy[dir];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny)
        break;
      else if (table[ny][nx] == 1) {
        table[ny][nx] = 0;
        dfs(nx - dx[dir], ny - dy[dir], cnt + 1);
        table[ny][nx] = 1;
        break;
      } else if (table[ny][nx] == 3) {
        res = cnt;
        return;
      }
    }
  }
}

int main() {

  while (cin >> w >> h, w) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> table[i][j];
        if (table[i][j] == 2) {
          sx = j;
          sy = i;
        }
      }
    }

    res = 11;
    dfs(sx, sy, 1);
    if (res > 10)
      res = -1;
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int w, h;
int sx, sy;
int res;
int table[30][30];
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

void dfs(int x, int y, int cnt) {
  if (cnt >= res)
    return;

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

    if (nx < 0 || w <= nx || ny < 0 || h <= ny)
      continue; //?????????
    else if (table[ny][nx] == 1)
      continue; //???????§????????????????
    else if (table[ny][nx] == 3) {
      res = cnt;
      return;
    } //??´??????

    while (true) {
      nx += dx[dir];
      ny += dy[dir];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny)
        break; //?????????
      else if (table[ny][nx] == 1) {
        table[ny][nx] = 0;                        //?????????????£????
        dfs(nx - dx[dir], ny - dy[dir], cnt + 1); //?¬?????????????
        table[ny][nx] = 1; //?????????????£???????????????????
        break;
      } else if (table[ny][nx] == 3) {
        res = cnt; //??´??????
        return;
      }
    }
  }
}

int main() {

  while (cin >> w >> h, w) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> table[i][j];
        if (table[i][j] == 2) {
          sx = j;
          sy = i;
        }
      }
    }

    res = 100;
    dfs(sx, sy, 1);
    if (res > 10)
      res = -1;
    cout << res << endl;
  }

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

#include <iostream>
#define MAX 1000000000
using namespace std;
int w, h;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
int func(int y, int x, int now, int tab[][20]) {
  if (now > 10)
    return MAX;
  int ans = MAX, nx, ny;
  for (int i = 0; i < 4; i++) {
    nx = dx[i] + x;
    ny = dy[i] + y;
    if (nx >= 0 && nx < w && ny >= 0 && ny < h && tab[ny][nx] == 1)
      continue;
    while (nx >= 0 && nx < w && ny >= 0 && ny < h) {
      if (tab[ny][nx] == 1 || tab[ny][nx] == 3)
        break;
      nx += dx[i];
      ny += dy[i];
    }
    if (!(nx >= 0 && nx < w && ny >= 0 && ny < h)) {
      continue;
    } else if (tab[ny][nx] == 1) {
      tab[ny][nx] = 0;
      ans = min(ans, func(ny - dy[i], nx - dx[i], now + 1, tab));
      tab[ny][nx] = 1;
    } else if (tab[ny][nx] == 3) {
      ans = min(ans, now);
    }
  }
  return ans;
}
int main() {
  int sy, sx;
  while (cin >> w >> h, w || h) {
    int tab[20][20];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> tab[i][j];
        if (tab[i][j] == 2) {
          sy = i;
          sx = j;
        }
      }

    int ans = func(sy, sx, 1, tab);
    if (ans != MAX && ans < 11)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define MAX 1000000000
using namespace std;
int w, h;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
int func(int y, int x, int now, int tab[][20]) {
  int ans = MAX, nx, ny;
  for (int i = 0; i < 4; i++) {
    nx = dx[i] + x;
    ny = dy[i] + y;
    if (nx >= 0 && nx < w && ny >= 0 && ny < h && tab[ny][nx] == 1)
      continue;
    while (nx >= 0 && nx < w && ny >= 0 && ny < h) {
      if (tab[ny][nx] == 1 || tab[ny][nx] == 3)
        break;
      nx += dx[i];
      ny += dy[i];
    }
    if (!(nx >= 0 && nx < w && ny >= 0 && ny < h)) {
      continue;
    } else if (tab[ny][nx] == 1) {
      tab[ny][nx] = 0;
      ans = min(ans, func(ny - dy[i], nx - dx[i], now + 1, tab));
      tab[ny][nx] = 1;
    } else if (tab[ny][nx] == 3) {
      ans = min(ans, now);
    }
  }
  return ans;
}
int main() {
  int sy, sx;
  while (cin >> w >> h, w || h) {
    int tab[20][20];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> tab[i][j];
        if (tab[i][j] == 2) {
          sy = i;
          sx = j;
        }
      }

    int ans = func(sy, sx, 1, tab);
    if (ans != MAX && ans < 11)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}