Problem:
Problem D:
Curling 2.0
On Planet MM-21, after their Olympic games this year, curling is getting popular.
But the rules are somewhat different from ours.
The game is played on an ice game board on which a square mesh is marked.
They use only a single stone.
The purpose of the game is to lead the stone from the start
to the goal with the minimum number of moves.
Fig. D-1 shows an example of a game board.
Some squares may be occupied with blocks.
There are two special squares namely the start and the goal,
which are not occupied with blocks.
(These two squares are distinct.)
Once the stone begins to move, it will proceed until it hits a block.
In order to bring the stone to the goal,
you may have to stop the stone by hitting it against a block,
and throw again.
Fig. D-1: Example of board (S: start, G: goal)
The movement of the stone obeys the following rules:
At the beginning, the stone stands still at the start square.
The movements of the stone are restricted to x and y directions.
Diagonal moves are prohibited.
When the stone stands still, you can make it moving by throwing it.
You may throw it to any direction
unless it is blocked immediately(Fig. D-2(a)).
Once thrown, the stone keeps moving to the same direction until
one of the following occurs:
The stone hits a block (Fig. D-2(b), (c)).
The stone stops at the square next to the block it hit.
The block disappears.
The stone gets out of the board.
The game ends in failure.
The stone reaches the goal square.
The stone stops there and the game ends in success.
You cannot throw the stone more than 10 times in a game.
If the stone does not reach the goal in 10 moves, the game ends in failure.
Fig. D-2: Stone movements
Under the rules, we would like to know whether the stone at the start
can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. D-1, 4 moves are required
to bring the stone from the start to the goal.
The route is shown in Fig. D-3(a).
Notice when the stone reaches the goal, the board configuration has changed
as in Fig. D-3(b).
Fig. D-3: The solution for Fig. D-1 and the final board configuration
Input
The input is a sequence of datasets.
The end of the input is indicated by a line
containing two zeros separated by a space.
The number of datasets never exceeds 100.
Each dataset is formatted as follows.
the width(=w) and the height(=h) of the board
First row of the board
...
h-th row of the board
The width and the height of the board satisfy:
 2 <=
w
<= 20, 1 <=
h
<= 20.
Each line consists of
w
decimal numbers delimited by a space.
The number describes the status of the corresponding square.
0
vacant square
1
block
2
start position
3
goal position
The dataset for Fig. D-1 is as follows:
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
Output
For each dataset, print a line having a decimal integer indicating the
minimum number of moves along a route from the start to the goal.
If there are no such route
...(truncated)

Submitted solutions:

code_snippet: #include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cstring> // memset
using namespace std;
typedef long long ll;

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


int grid[29][29];
int dist[29][29];
int now;
int H,W,ans;
void dfs(int x,int y){
  if(now>10)return;
  for(int d=0;d<4;d++){
    int nx=x,ny=y;
    while(1){
      nx+=dx[d],ny+=dy[d];
      if(0>nx||0>ny||nx>=H||ny>=W)break;
      if(grid[nx][ny]==1){
        if(nx-dx[d]==x&&ny-dy[d]==y) break;
        grid[nx][ny]=0;now++;
        dfs(nx-dx[d],ny-dy[d]);
        grid[nx][ny]=1;now--;
        break;
      }
      if(grid[nx][ny]==3){
        now++;
        ans=min(ans,now);
        now--;
        break;
      }
    }
  }
}

void solve(){
  cin>>W>>H;
  if(H==0&&W==0)exit(0);
  for(int i=0;i<H;i++) fill(dist[i],dist[i]+W,1e9);
  ans=1e9;
  now=0;
  int si,sj;
  for(int i=0;i<H;i++) for(int j=0;j<W;j++){
    cin>>grid[i][j];
    if(grid[i][j]==2) si=i,sj=j;
  }
  dist[si][sj]=0;
  dfs(si,sj);
  if(ans>10)ans=-1;
  cout << ans<<endl;




}


int main(){
ios::sync_with_stdio(false);cin.tie(0);
//=======
  while(1) solve();
}
accuracy: 1
time_ms: 30
memory_kb: 3136
final_rank: 18

code_snippet: #include <iostream>
using namespace std;

int strow, stcol;
int w, h;

const int DIRECT[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

int grid[20][20];

int find_path(int row, int col, int step)
{
  if (step > 10) return 11;

  int min_path = 11;
  for (int i = 0; i < 4; i++) {
    int drow = row + DIRECT[i][0], dcol = col + DIRECT[i][1];
    while (0 <= drow && drow < h && 0 <= dcol && dcol < w) {
      if (grid[drow][dcol] != 0) break;
      drow += DIRECT[i][0];
      dcol += DIRECT[i][1];
    }

    if (drow < 0 || drow >= h || dcol < 0 || dcol >= w) continue;
    else if (grid[drow][dcol] == 3) return step;
    else if (drow == row + DIRECT[i][0] && dcol == col + DIRECT[i][1])
      continue;
    grid[drow][dcol] = 0;
    int tmp = find_path(drow - DIRECT[i][0], dcol - DIRECT[i][1], step + 1);
    grid[drow][dcol] = 1;
    if (tmp < min_path) min_path = tmp;
  }
  return min_path;
}

int main()
{
  while (cin >> w >> h && !(w == 0 && h == 0)) {
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> grid[i][j];
        if (grid[i][j] == 2) {
          strow = i;
          stcol = j;
          grid[i][j] = 0;
        }
      }

    int tmp = find_path(strow, stcol, 1);
    if (tmp <= 10) cout << tmp << endl;
    else cout << -1 << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3368
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <queue>
using namespace std;

int const DIRECTION[][2] = {-1, 0, 0, 1, 1, 0, 0, -1};

struct qelem
{
  int row, col;
  int cnt;
  vector<vector<char> > grid;
};

int w, h;
queue<qelem> que;

int main()
{
  while (cin >> w >> h && !(w == 0 && h == 0)) {
    qelem step;
    step.grid = vector<vector<char> >(h, vector<char>(w));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> step.grid[i][j];
        if (step.grid[i][j] == '2') {
          step.row = i;
          step.col = j;
          step.grid[i][j] = '0';
        }
      }
    }

    while (!que.empty()) que.pop();
    bool yes = false;
    step.cnt = 0;
    que.push(step);
    while (!que.empty()) {
      step = que.front();
      que.pop();

      if (step.cnt >= 10) break;

      for (int i = 0; i < 4; i++) {
        int dr = step.row + DIRECTION[i][0], dc = step.col + DIRECTION[i][1];
        while (0 <= dr && dr < h && 0 <= dc && dc < w) {
          if (step.grid[dr][dc] != '0') break;
          dr += DIRECTION[i][0];
          dc += DIRECTION[i][1];
        }

        if (dr < 0 || dr >= h || dc < 0 || dc >= w) continue;
        else if (step.grid[dr][dc] == '3') {
          yes = true;
          step.cnt++;
          break;
        } else if (dr == step.row + DIRECTION[i][0] &&
            dc == step.col + DIRECTION[i][1]) continue;
        qelem tmp = step;
        tmp.row = dr - DIRECTION[i][0];
        tmp.col = dc - DIRECTION[i][1];
        tmp.grid[dr][dc] = '0';
        tmp.cnt++;
        que.push(tmp);

        // for (int i = 0; i < h; i++) {
        // for (int j = 0; j < w; j++) cout << step.grid[i][j] << ' ';
        // cout << "|";
        // for (int j = 0; j < w; j++) {
        // if (tmp.grid[i][j] != step.grid[i][j]) cout << " #";
        // else cout << ' ' << tmp.grid[i][j];
        // }
        // cout << endl;
        // }
        // cout << "cnt= " << step.cnt << endl;
      }
      if (yes) break;
    }
    if (yes) cout << step.cnt << endl;
    else cout << -1 << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 88512
final_rank: 20

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

bool solve() {
    int H, W;
    cin >> W >> H;
    if (H == 0 && W == 0) return false;
    vector<int> A(H);
    int sr = -1, sc = -1, tr = -1, tc = -1;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            int a;
            cin >> a;
            if (a == 1) {
                A[i] ^= 1 << j;
            } else if (a == 2) {
                sr = i, sc = j;
            } else if (a == 3) {
                tr = i, tc = j;
            }
        }
    }
    int ans = 1 << 30;
    int D[5] = {0, 1, 0, -1, 0};
    auto dfs = [&] (auto dfs, int r, int c, int t, vector<int> X) -> void {
        if (t == 10) return;
        for (int d = 0; d < 4; d++) {
            int dr = D[d], dc = D[d + 1];
            int nr = r + dr, nc = c + dc;
            while (0 <= nr && nr < H && 0 <= nc && nc < W && !(X[nr] >> nc & 1)) {
                if (nr == tr && nc == tc) ans = min(ans, t + 1);
                nr += dr, nc += dc;
            }
            if (0 <= nr && nr < H && 0 <= nc && nc < W) {
                auto Y = X;
                Y[nr] ^= 1 << nc;
                nr -= dr, nc -= dc;
                if (r != nr || c != nc) {
                    dfs(dfs, nr, nc, t + 1, Y);
                }
            }
        }
    };
    dfs(dfs, sr, sc, 0, A);
    if (ans == 1 << 30) ans = -1;
    cout << ans << '\n';
    return true;
}

int main() {
    while (solve()) {}
}
accuracy: 1
time_ms: 40
memory_kb: 3456
final_rank: 19

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

#define int long long
using ll = long long;
ll INF = 2e18;
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define fi first
#define se second
#define endl "\n" // コメントアウトで随時出力

/*=========================================mint=======================================*/
const int mintMod = MOD2; // ここを変更
class mint
{
    long long x;

public:
    mint(long long x = 0) : x((x % mintMod + mintMod) % mintMod) {}
    long long val() const { return x; }
    mint operator-() const
    {
        return mint(-x);
    }
    mint &operator+=(const mint &a)
    {
        if ((x += a.x) >= mintMod)
            x -= mintMod;
        return *this;
    }
    mint &operator-=(const mint &a)
    {
        if ((x += mintMod - a.x) >= mintMod)
            x -= mintMod;
        return *this;
    }
    mint &operator*=(const mint &a)
    {
        (x *= a.x) %= mintMod;
        return *this;
    }
    mint operator+(const mint &a) const
    {
        mint res(*this);
        return res += a;
    }
    mint operator-(const mint &a) const
    {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint &a) const
    {
        mint res(*this);
        return res *= a;
    }
    mint pow(ll t) const
    {
        if (!t)
            return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1)
            a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const
    {
        return pow(mintMod - 2);
    }
    mint &operator/=(const mint &a)
    {
        return (*this) *= a.inv();
    }
    mint operator/(const mint &a) const
    {
        mint res(*this);
        return res /= a;
    }

    friend ostream &operator<<(ostream &os, const mint &m)
    {
        os << m.x;
        return os;
    }
    friend istream &operator>>(istream &is, mint &m)
    {
        long long v;
        is >> v;
        m = mint(v);
        return is;
    }
};
/*========================================================================================*/

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

template <typename T>
using vc = vector<T>; // prioriy_queueに必要なのでここにこれ書いてます
template <typename T>
using vv = vc<vc<T>>;

// 提出の際はコメントアウト
#define debug_x(x) cout << #x << " = " << (x) << endl
#define debug_vc(v)                    \
    {                                  \
        cout << #v << endl;            \
        ll n = size(v);                \
        rep(i, n) cout << v[i] << ' '; \
        cout << endl;                  \
    } // 一次元配列を出力する
#define debug_vv(v)                                        \
    {                                                      \
        cout << #v << endl;                                \
        ll n = size(v);                                    \
        rep(i, n)                                          \
        {                                                  \
            rep(j, size(v[i])) { cout << v[i][j] << ' '; } \
            cout << endl;                                  \
        }                                                  \
    } // 二次元配列を出力する

// #define debug_x(x) \
//     do             \
//     {              \
//     } while (0)
// #define debug_vc(v) \
//     do              \
//     {               \
//     } while (0)
// #define debug_vv(v) \
//     do              \
//     {               \
//     } while (0)

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = 1; i <= (n); ++i)
#define drep(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define nfor(i, s, n) for (ll i = s; i < n; i++)        // i=s,s+1...n-1 ノーマルfor
#define dfor(i, s, n) for (ll i = (s) - 1; i >= n; i--) // s-1スタートでnまで落ちる

using ld = long double;
using bl = bool;

template <class T>
using pq = priority_queue<T, vc<T>>; // 大きい順
template <class T>
using pq_g = priority_queue<T, vc<T>, greater<T>>; // 小さい順

using vl = vc<ll>;
using vvl = vv<ll>;
using vvvl = vv<vl>;
using vvvvl = vv<vvl>;
using vs = vc<string>;
using vvs = vv<string>;
using vb = vc<bl>;
using vvb = vv<bl>;
using vvvb = vv<vb>;
using vld = vc<ld>;
using vvld = vv<ld>;
using vvvld = vv<vld>;
using P = pair<ll, ll>;
using vP = vc<P>;
using vvP = vc<vP>;
using vvvP = vv<vP>;
using vmint = vc<mint>;
using vvmint = vv<mint>;
using vvvmint = vv<vmint>;

#define pb push_back
#define eb emplace_back

#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define YN                     \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    } // if(a==b)YN;
#define dame cout << -1 << endl

string dir = "DRUL";   // 第4象限の場合
vl di = {1, 0, -1, 0}; // vl di={1,1,0,-1,-1,-1,0,1};
vl dj = {0, 1, 0, -1}; // vl dj={0,1,1,1,0,-1,-1,-1};

bool out_grid(ll i, ll j, ll h, ll w)
{ // trueならcontinue
    return (!(0 <= i && i < h && 0 <= j && j < w));
}

ll w, h;
ll ans;

void dfs(ll i, ll j, vvl &b, ll cnt)
{
    if (cnt == 10)
        return;
    rep(k, 4)
    {
        if(out_grid(i+di[k],j+dj[k],h,w)||b[i+di[k]][j+dj[k]]==1)continue;
        ll ni = i;
        ll nj = j;
        // bl out = false;
        while (true)
        {
            ni = ni + di[k];
            nj = nj + dj[k];
            if (out_grid(ni, nj, h, w)){
                break;
            }
            if (b[ni][nj] == 3)
            {
                ans = min(ans, cnt + 1);
                break;
            }
            if(b[ni][nj]==1){
                b[ni][nj] = 0;
                dfs(ni - di[k],nj - dj[k],b,cnt+1);
                b[ni][nj] = 1;
                break;
            }
        }
    }
}

void solve()
{
    ans = INF;
    vvl b(h, vl(w));
    rep(i, h)
    {
        rep(j, w)
        {
            cin >> b[i][j];
        }
    }
    ll si, sj;
    rep(i, h)
    {
        rep(j, w)
        {
            if (b[i][j] == 2)
            {
                si = i;
                sj = j;
            }
        }
    }
    dfs(si, sj, b, 0);
    if(ans==INF)ans = -1;
    cout << ans << endl;
    return;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    while (true)
    {
        cin >> w >> h;
        if(w==0)break;
        solve();
    }
    return 0;
}

/*
shift+alt+Fで成形
f2で変数名変更

スニペット
basic
 m:atcoder以外用
 ma:ABC/ARC用
 mahc:AHC用
math
 floor:床関数
 ceil:天井関数
 comv:二項係数
 modpow: a^b mod m
 GCD:最大公約数
 extGCD:拡張ユークリッド互除法
 PFD:素因数分解
 isprime:エラトステネスの篩
 matrixpow:行列累乗
data_structure
 seg:セグメント木
technic
 Compress:座標圧縮
 runlength:ランレングス圧縮
graph
 warshall:ワーシャルフロイド法
 dfs:深さ優先探索
 bfs:幅優先探索
 LCA:最近共通祖先
*/
accuracy: 1
time_ms: 20
memory_kb: 3236
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n); i >= 0; i--)
#define range(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
using ll = long long;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "[";
    for (int i = 0; i < v.size(); i++) {
        os << v[i] << (i == (v.size() - 1) ? "" : ", ");
    }
    os << "]";
    return os;
}

template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2> p) {
    os << "{" << p.first << ", " << p.second << "}";
    return os;
}

int wall[22][22];
vector<pair<int, int>> dij = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

int dfs(int i, int j, int cnt) {
    int res = 11;
    if (cnt == 10) return res;

    int si = i, sj = j;
    for (auto [di, dj] : dij) {
        i = si;
        j = sj;
        if (wall[i + di][j + dj] == 1) continue;
        while (wall[i][j] == 0) {
            i += di;
            j += dj;
        }
        if (wall[i][j] == 3) {
            res = min(res, cnt + 1);
            continue;
        }
        if (wall[i][j] == 1) {
            wall[i][j] = 0;
            res = min(res, dfs(i - di, j - dj, cnt + 1));
            wall[i][j] = 1;
        }
    }
    return res;
}

bool solve() {
    int w, h;
    cin >> w >> h;
    if (w == 0) return false;
    rep(i, h + 2) rep(j, w + 2) wall[i][j] = -1;
    int si, sj;
    rep(i, h) rep(j, w) {
        cin >> wall[i + 1][j + 1];
        if (wall[i + 1][j + 1] == 2) {
            si = i + 1;
            sj = j + 1;
            wall[si][sj] = 0;
        }
    }
    int ans = dfs(si, sj, 0);

    if (ans == 11) {
        cout << -1 << endl;
    } else {
        cout << ans << endl;
    }
    return true;
}

int main() {
    while (solve()) {
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3332
final_rank: 4

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

using ll = long long;

int main(){
    while(1){
        int h, w; cin >> w >> h;
        if(w == 0) break;
        vector g(h, vector(w, 0));
        int sy, sx;
        for(int i = 0; i < h; ++i){
            for(int j = 0; j < w; ++j){
                cin >> g[i][j];
                if(g[i][j] == 2) sy = i, sx = j;
            }
        }
        int dy[4] = {0, 1, 0, -1};
        int dx[4] = {1, 0, -1, 0};
        int ans = 100;
        auto dfs = [&](auto &self, int y, int x, int k) -> void {
            if(k == 10){
                return;
            }
            // cerr << "# (y, x) = (" << y << ", " << x << "), k = " << k << endl;
            // for(int i = 0; i < h; ++i){
            //     cerr << "#";
            //     for(int j = 0; j < w; ++j){
            //         cerr << " " << g[i][j];
            //     }
            //     cerr << endl;
            // }
            for(int d = 0; d < 4; ++d){
                int ny = y, nx = x, nny = y + dy[d], nnx = x + dx[d];
                int cnt = 0;
                while(0 <= nny and nny < h and 0 <= nnx and nnx < w){
                    if(g[ny][nx] == 3) break;
                    if(g[nny][nnx] == 1) break;
                    ++cnt;
                    ny = nny, nx = nnx;
                    nny += dy[d], nnx += dx[d];
                }
                if(g[ny][nx] == 3){
                    ans = min(ans, k + 1);
                    continue;
                }
                if(nny < 0 or nny >= h or nnx < 0 or nnx >= w or cnt == 0){
                    continue;
                }
                g[nny][nnx] = 0;
                self(self, ny, nx, k + 1);
                g[nny][nnx] = 1;
            }
        };
        dfs(dfs, sy, sx, 0);
        if(ans > 10) cout << -1 << endl;
        else cout << ans << endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3188
final_rank: 10

code_snippet: #include <iostream>
#include <vector>

using namespace std;
int w, h, ans = 0;

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

void dfs(int turn, int x, int y, vector<vector<int>>& board){
    if(turn > 10) return;
    for(int dir=0; dir<4; dir++){
        int nx = x, ny = y;
        int px = nx + dx[dir], py = ny + dy[dir];
        if(px < 0 || px >= h || py < 0 || py >= w) continue;
        if(board[px][py] == 1) continue;

        while(1){
            nx += dx[dir], ny += dy[dir];
            if(nx < 0 || nx >= h || ny < 0 || ny >= w) {
                break;
            }
            else if(board[nx][ny] == 3){
                ans = min(ans, turn);
                break;
            }
            else if(board[nx][ny] == 1){
                board[nx][ny] = 0;
                dfs(turn+1, nx-dx[dir], ny-dy[dir], board);
                board[nx][ny] = 1;
                break;
            }
        }
    }    
}


int main() {
    while (true) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;  // 入力の終わり
        ans = 30;
        vector<vector<int>> board(h, vector<int>(w));

        int x = 0, y = 0;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                cin >> board[i][j];
                if(board[i][j] == 2){
                    x = i, y = j;
                }
            }
        }

        dfs(1, x, y, board);
        if(ans == 30){
            cout << -1 << endl;
        }
        else{
            cout << ans << endl;
        }
    }

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3304
final_rank: 14

code_snippet: #include <algorithm>
#include <climits>
#include <cstdio>

#define MAX_N 20

int H, W, TI, TJ;

int G[MAX_N][MAX_N];

int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

enum Tile {
  EMPTY,
  BLOCK,
  START,
  END,
};

void dfs(int i, int j, int ct, int &ans) {
  if (ct > 10) {
    return;
  }

  for (int k = 0; k < 4; k++) {
    int ni = i, nj = j;
    int di = dir[k][0], dj = dir[k][1];
    while (true) {
      ni += di;
      nj += dj;
      if (ni < 0 || ni >= H || nj < 0 || nj >= W) {
        break;
      }
      if (ni == TI && nj == TJ) {
        ans = std::min(ans, ct);
        return;
      }
      if (G[ni][nj] == Tile::BLOCK) {
        if (ni - di != i || nj - dj != j) {
          G[ni][nj] = Tile::EMPTY;
          dfs(ni - di, nj - dj, ct + 1, ans);
          G[ni][nj] = Tile::BLOCK;
        }
        break;
      }
    }
  }
}

int main() {
  while (scanf("%d %d", &W, &H) == 2 && H != 0 && W != 0) {
    int si, sj;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        scanf("%d", &G[i][j]);
        if (G[i][j] == Tile::START) {
          si = i;
          sj = j;
        } else if (G[i][j] == Tile::END) {
          TI = i;
          TJ = j;
        }
      }
    }

    int ans = INT_MAX;
    dfs(si, sj, 1, ans);
    printf("%d\n", ans == INT_MAX ? -1 : ans);
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2944
final_rank: 2

code_snippet: #include <algorithm>
#include <climits>
#include <cstdio>

#define MAX_N 20

int H, W;

int G[MAX_N][MAX_N];

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

P dir[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

enum Tile {
  EMPTY,
  BLOCK,
  START,
  END,
};

void dfs(int i, int j, int ct, int ti, int tj, int &ans) {
  if (ct > 10) {
    return;
  }

  for (int k = 0; k < 4; k++) {
    int ni = i, nj = j;
    int di = dir[k].first, dj = dir[k].second;
    while (true) {
      ni += di;
      nj += dj;
      if (ni < 0 || ni >= H || nj < 0 || nj >= W) {
        break;
      }
      if (ni == ti && nj == tj) {
        ans = std::min(ans, ct);
        return;
      }
      if (G[ni][nj] == Tile::BLOCK) {
        if (ni - di != i || nj - dj != j) {
          G[ni][nj] = Tile::EMPTY;
          dfs(ni - di, nj - dj, ct + 1, ti, tj, ans);
          G[ni][nj] = Tile::BLOCK;
        }
        break;
      }
    }
  }
}

int main() {
  while (scanf("%d %d", &W, &H) == 2 && H != 0 && W != 0) {
    int si, sj, ti, tj;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        scanf("%d", &G[i][j]);
        if (G[i][j] == Tile::START) {
          si = i;
          sj = j;
        } else if (G[i][j] == Tile::END) {
          ti = i;
          tj = j;
        }
      }
    }

    int ans = INT_MAX;
    dfs(si, sj, 1, ti, tj, ans);
    printf("%d\n", ans == INT_MAX ? -1 : ans);
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2724
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
void rec(pair<int,int> pos, vector<vector<int>>& p, set<int>& ans, int cur,int& n, int& m, pair<int,int>& fi){
    if (cur>9){
        return;
    }
    if (pos==fi){
        ans.insert(cur);
        return;
    }
    if (pos.first+1!=n and p[pos.first+1][pos.second]!=1){
        pair<int,int> pos1=pos;
        bool ost=true;
        while (p[pos1.first][pos1.second]!=1){
            pos1.first++;
            if (pos1==fi){
                ans.insert(cur+1);
                return;
            }
            if (pos1.first==n){
                ost=false;
                break;
            }
        }
        pos1.first--;
        if (pos1==fi){
            ans.insert(cur+1);
            return;
        }
        if (ost){
            p[pos1.first+1][pos1.second]=0;
            rec(pos1,p,ans,cur+1,n,m,fi);
            p[pos1.first+1][pos1.second]=1;
        }
    }
    if (pos.first-1!=-1 and p[pos.first-1][pos.second]!=1){
        pair<int,int> pos1=pos;
        bool ost=true;
        while (p[pos1.first][pos1.second]!=1){
            pos1.first--;
            if (pos1==fi){
                ans.insert(cur+1);
                return;
            }
            if (pos1.first==-1){
                ost=false;
                break;
            }
        }
        pos1.first++;
        if (pos1==fi){
            ans.insert(cur+1);
            return;
        }
        if (ost){
            p[pos1.first-1][pos1.second]=0;
            rec(pos1,p,ans,cur+1,n,m,fi);
            p[pos1.first-1][pos1.second]=1;
        }
    }
    if (pos.second+1!=m and p[pos.first][pos.second+1]!=1){
        pair<int,int> pos1=pos;
        bool ost=true;
        while (p[pos1.first][pos1.second]!=1){
            pos1.second++;
            if (pos1==fi){
                ans.insert(cur+1);
                return;
            }
            if (pos1.second==m){
                ost=false;
                break;
            }
        }
        pos1.second--;
        if (pos1==fi){
            ans.insert(cur+1);
            return;
        }
        if (ost){
            p[pos1.first][pos1.second+1]=0;
            rec(pos1,p,ans,cur+1,n,m,fi);
            p[pos1.first][pos1.second+1]=1;
        }
    }
    if (pos.second-1!=-1 and p[pos.first][pos.second-1]!=1){
        pair<int,int> pos1=pos;
        bool ost=true;
        while (p[pos1.first][pos1.second]!=1){
            pos1.second--;
            if (pos1==fi){
                ans.insert(cur+1);
                return;
            }
            if (pos1.second==-1){
                ost=false;
                break;
            }
        }
        pos1.second++;
        if (pos1==fi){
            ans.insert(cur+1);
            return;
        }
        if (ost){
            p[pos1.first][pos1.second-1]=0;
            rec(pos1,p,ans,cur+1,n,m,fi);
            p[pos1.first][pos1.second-1]=1;
        }
    }
}
int main() {
    int n,m;cin>>m>>n;
    while (n!=0 and m!=0){
        pair<int,int> st;
        pair<int,int> fi;
        vector<vector<int>> p(n,vector<int> (m));
        set<int> ans;
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                cin>>p[i][j];
                if (p[i][j]==2){
                    st={i,j};
                }
                if (p[i][j]==3){
                    fi={i,j};
                }
            }
        }
        rec(st,p,ans,0,n,m,fi);
        if (ans.empty()){
            cout<<-1<<endl;
        }
        else{
            cout<<*ans.begin()<<endl;
        }
        cin>>m>>n;
    }

}
accuracy: 1
time_ms: 10
memory_kb: 3368
final_rank: 7

code_snippet: #include <iostream>
#include <vector>

using namespace std;

void solve(int w, int h) {
    vector ban(h, vector<int>(w));
    for (auto &l: ban) {
        for (auto &i: l) cin >> i;
    }
    pair<int, int> s;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (ban[i][j] == 2) {
                s.first = i, s.second = j;
                break;
            }
        }
    }
    vector broken(h, vector<bool>(w));
    const vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
    constexpr int inf = 1 << 29;
    auto dfs = [&](auto &self, int x, int y, int cnt) -> int{
        if (cnt == 10) return inf;
        int ret = inf;
        int ox = x, oy = y;
        for (int k = 0; k < 4; k++) {
            bool first = true;
            x = ox, y = oy;
            while (true) {
                int nx = x + dx[k], ny = y + dy[k];
                if (!(0 <= nx && nx < h && 0 <= ny && ny < w)) break;
                if (ban[nx][ny] == 3) {
                    ret = min(ret, cnt + 1);
                    break;
                }
                if (ban[nx][ny] == 1 && !broken[nx][ny]) {
                    if (first) break;
                    broken[nx][ny] = true;
                    ret = min(ret, self(self, x, y, cnt + 1));
                    broken[nx][ny] = false;
                    break;
                }
                x = nx, y = ny;
                first = false;
            }
        }
        return ret;
    };
    int ans = dfs(dfs, s.first, s.second, 0);
    cout << (ans == inf? -1: ans) << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    do {
        solve(n, m);
        cin >> n >> m;
    }while(n != 0);
}
accuracy: 1
time_ms: 20
memory_kb: 3488
final_rank: 17

code_snippet: #include <bits/stdc++.h>
    using namespace std;
    
    template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
    template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
    #define rep(i,n) for (int i = 0; i < (n); ++i)

    typedef long long ll;
    typedef unsigned long long ull;
    using P=pair<ll,ll>;
    using T=tuple<int,int,int>;
    const int INF=1001001001;
    const ll INFL=1e18;
    const int mod=998244353;

	int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
	int dfs(int i,int j,int t,vector<vector<int>>& a){
		if(t>=10){
			return INF;
		}
		int h=a.size(),w=a[0].size();
		int ans=INF;
		rep(k,4){
			int ni=i+dx[k],nj=j+dy[k];
			while(ni>=0&&ni<h&&nj>=0&&nj<w&&a[ni][nj]!=1&&a[ni][nj]!=3){
				ni+=dx[k];nj+=dy[k];
			}
			if(ni<0||ni>=h||nj<0||nj>=w){continue;}
			if(a[ni][nj]==3)chmin(ans,1);
			if(a[ni][nj]==1){
				if(ni==i+dx[k]&&nj==j+dy[k])continue;
				a[ni][nj]=0;
				chmin(ans,1+dfs(ni-dx[k],nj-dy[k],t+1,a));
				a[ni][nj]=1;
			}
		}
		return ans;
	}	

    bool solve(){
		int w,h;
		cin>>w>>h;
		if(!w&&!h)return false;
		
		vector<vector<int>>a(h,vector<int>(w));
		rep(i,h){
			rep(j,w){
				cin>>a[i][j];
			}
		}
		int ans=INF;
		rep(i,h){
			rep(j,w){
				if(a[i][j]==2){
					chmin(ans,dfs(i,j,0,a));
				}
			}
		}
		if(ans==INF)ans=-1;
		cout<<ans<<endl;
		return true;
    }

    int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

		while(1){
			if(!solve())break;
		}
		
        return 0;
    }
accuracy: 1
time_ms: 10
memory_kb: 3456
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pi;
typedef vector<ll> vi;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(A) A.begin(),A.end()
int main(){
    while(1){
        ll H,W;cin>>W>>H;
        if(!H)return 0;
        vector<vector<ll>>A(H,vector<ll>(W));
        REP(i,H)REP(j,W)cin>>A[i][j];
        ll ans=11,cnt=0;
        vector<pi>move={pi(0,1),pi(0,-1),pi(1,0),pi(-1,0)};
        function<void(ll,ll)>dfs=[&](ll x,ll y){
            if(ans<=cnt+1)return;
            cnt++;
            for(auto[dx,dy]:move)if(min(x+dx,y+dy)>=0&&min(H-x-dx,W-y-dy)>0&&A[x+dx][y+dy]!=1){
                ll _x=x+dx,_y=y+dy;
                while(min({_x+1,_y+1,H-_x,W-_y})>0&&A[_x][_y]!=1&&A[_x][_y]!=3)_x+=dx,_y+=dy;
                if(min({_x+1,_y+1,H-_x,W-_y})<=0)continue;
                if(A[_x][_y]==3){
                    ans=min(ans,cnt);
                    cnt--;
                    return;
                }
                A[_x][_y]=0;
                dfs(_x-dx,_y-dy);
                A[_x][_y]=1;
            }
            cnt--;
        };
        REP(i,H)REP(j,W)if(A[i][j]==2)dfs(i,j);
        if(ans>10)ans=-1;
        cout<<ans<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3336
final_rank: 5

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
using ll = long long;
#define MOD 1000000007
#define Mod 998244353
const int MAX = 1000000005;
const long long INF = 1000000000000000005LL;
using namespace std;
//using namespace atcoder;

int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
int w, h, gi, gj;
void dfs(int i, int j, int cnt, vector<vector<int>>& dist, vector<vector<int>>& G) {
    if (cnt == 10) return;
    for (int k = 0; k < 4; k++) {
        int ni = i + di[k], nj = j + dj[k];
        if ((ni < 0 || ni >= h || nj < 0 || nj >= w) || G[ni][nj] == 1) continue;
        int x = i, y = j;
        while (true) {
            int nx = x + di[k], ny = y + dj[k];
            if ((nx < 0 || nx >= h || ny < 0 || ny >= w) || G[nx][ny] == 1) break;
            x = nx; y = ny;
            if (x == gi && y == gj) {
                if (dist[x][y] == -1) dist[x][y] = cnt + 1;
                else dist[x][y] = min(dist[x][y], cnt + 1);
            }
        }

        ni = x + di[k];
        nj = y + dj[k];
        if (!(ni < 0 || ni >= h || nj < 0 || nj >= w)) {
            G[ni][nj] = 0;
            if (dist[x][y] == -1) dist[x][y] = cnt + 1;
            else dist[x][y] = min(dist[x][y], cnt + 1);
            dfs(x, y, cnt + 1, dist, G);
            G[ni][nj] = 1;
        }
    }
}

bool solve() {
    cin >> w >> h;
    if (w == 0 && h == 0) return false;
    vector<vector<int>> G(h, vector<int>(w));
    int si, sj;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> G[i][j];
            if (G[i][j] == 2) {si = i; sj = j;}
            if (G[i][j] == 3) {gi = i; gj = j;}
        }
    }
    vector<vector<int>> dist(h, vector<int>(w, -1));
    dist[si][sj] = 0;
    dfs(si, sj, 0, dist, G);
    cout << dist[gi][gj] << endl;
    return true;
}

int main() {
    ios::sync_with_stdio(0); cin.tie();
    while (solve()) {}
}
accuracy: 1
time_ms: 20
memory_kb: 3416
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
template<typename T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template<typename T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void solve();
int w,h,a[20][20];
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int t = 1;
  //cin >> t;
  while (1) {
    cin>>w>>h;
    if(w==0)break;
    rep(i,h)rep(j,w)cin>>a[i][j];
    solve();
  }
}
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int sx,sy,tx,ty;
int dfs(int x0,int y0,int now){
  if(10<=now)return 11;
  int ret=11;
  rep(dir,4){
    int x=x0,y=y0;
    {
      int xx=x+dx[dir],yy=y+dy[dir];
      if(!(0<=xx&&xx<h&&0<=yy&&yy<w))continue;
      if(a[xx][yy]==1)continue;
    }
    while(1){
      int xx=x+dx[dir],yy=y+dy[dir];
      if(!(0<=xx&&xx<h&&0<=yy&&yy<w)){
        break;
      }
      if(a[xx][yy]==1){
        a[xx][yy]=0;
        chmin(ret,dfs(x,y,now+1)+1);
        a[xx][yy]=1;
        break;
      }
      if(xx==tx&&yy==ty)return 1;
      x=xx,y=yy;
    }
  }
  return ret;
}
void solve() {
  rep(i,h)rep(j,w){
    if(a[i][j]==2)sx=i,sy=j,a[i][j]=0;
    if(a[i][j]==3)tx=i,ty=j,a[i][j]=0;
  }
  int ans=dfs(sx,sy,0);
  if(10<ans)ans=-1;
  cout<<ans<<'\n';
}
accuracy: 1
time_ms: 10
memory_kb: 3364
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rrep(i,start,end) for (long long i = start;i >= (long long)(end);i--)
#define repn(i,end) for(long long i = 0; i <= (long long)(end); i++)
#define reps(i,start,end) for(long long i = start; i < (long long)(end); i++)
#define repsn(i,start,end) for(long long i = start; i <= (long long)(end); i++)
#define each(p,a) for(auto &p:a)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<long long> vll;
typedef vector<pair<long long ,long long>> vpll;
typedef vector<vector<long long>> vvll;
typedef set<ll> sll;
typedef map<long long , long long> mpll;
typedef pair<long long ,long long> pll;
typedef tuple<long long , long long , long long> tpl3;
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)
#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)
#define all(a)  (a).begin(),(a).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define sz(x) (int)x.size()
// << std::fixed << std::setprecision(10)
const ll INF = 1LL << 60;
const ld EPS = 1e-9;
 
inline ll lfloor(ll x,ll m){return (x - ((x % m+ m)%m))/m;}
inline ll positive_mod(ll a,ll m){return (a % m + m)%m;}
inline ll popcnt(ull a){ return __builtin_popcountll(a);}
template<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}
template<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}
template<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}
template<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?" ":"");}return os;}
template<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << "{" << p.first << "," << p.second << "}";return os;}
template<class... T>void input(T&... a){(cin >> ... >> a);}
void print(){cout << endl;}
template<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}
template<class T> void pspace(const T& a){ cout << a << ' ';}
void perr(){cerr << endl;}
template<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}
void yes(bool i = true){ return print(i?"yes":"no"); }
void Yes(bool i = true){ return print(i?"Yes":"No"); }
void YES(bool i = true){ return print(i?"YES":"NO"); }
template <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}
template <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}
template <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}
template <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}
//grid探索用
vector<ll> _ta = {0,0,1,-1,1,1,-1,-1};
vector<ll> _yo = {1,-1,0,0,1,-1,1,-1};
bool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}
  
ll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}
ll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} 
const ll MOD9 = 998244353LL;
const ll MOD10 = 1000000007LL;

// for AOJ or ICPC or etc..
// 全部が0だったらtrueを返す
template<class Tp> bool zero (const Tp &x) {return x == 0;}
template<class Tp, class... Args> bool zero (const Tp &x, const Args& ...args) {return zero(x) and zero(args...);}


// 変数をちゃんと全部受け取る！
void solve(ll w,ll h){
  ll sy,sx,gy,gx;
  vvll g(h,vll(w));
  rep(i,h){
    rep(j,w){
      cin >> g[i][j];
      if(g[i][j] == 2){
        sy = i;sx = j;
      }
      if(g[i][j] == 3){
        gy = i;gx = j;
      }
    }
  }
  ll ans = INF;
  auto dfs = [&](auto dfs, ll cnt ,ll y,ll x, vvll &state)->void{
    if(cnt == 10){
      return ;
    }
    // ll num = state[y][x];
    rep(i,4){
      if(!isin(y + _ta[i],x + _yo[i],h,w) or g[y + _ta[i]][x + _yo[i]] == 1){
        continue;
      }
      ll ny = y;
      ll nx = x;
      while(isin(ny,nx,h,w) && g[ny][nx] != 1){
        ny += _ta[i];
        nx += _yo[i];
        if(ny == gy && nx == gx){
          chmin(ans,cnt + 1);
        }
      }
      if(!isin(ny,nx,h,w)){
        continue;
        // ny -= _ta[i];
        // nx -= _yo[i];
        // ll past = state[ny][nx];
        // chmin(state[ny][nx],cnt+1);
        // dfs(dfs,cnt+1,ny,nx,state);
        // state[ny][nx] = past;
      }else{
        assert(g[ny][nx] == 1);
        //消す
        g[ny][nx] = 0;
        ny -= _ta[i];
        nx -= _yo[i];
        ll past = state[ny][nx];
        chmin(state[ny][nx],cnt+1);
        dfs(dfs,cnt+1,ny,nx,state);
        state[ny][nx] = past;
        g[ny + _ta[i]][nx + _yo[i]] = 1;
      }
    }
  };
  vector<vector<ll>> state(h,vll(w,INF));
  state[sy][sx] = 0;
  dfs(dfs,0,sy,sx,state);
  if(ans == INF){
    cout << -1 << endl;
  }else{
    cout << ans << endl;
  }
}
 
int main(){
  ios::sync_with_stdio(false);cin.tie(nullptr);
  while(true){
    LL(w,h);//変数数調整
    if(zero(w,h))break;
    solve(w,h);
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3256
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for(int i = a; i < n; i++)
#define rrep(i, a, n) for(int i = a; i >= n; i--)
#define inr(l, x, r) (l <= x && x < r)
#define ll long long
#define ld long double
//#define int long long

constexpr int IINF = 1001001001;
constexpr ll INF = 9e18;

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

int main(){
    while(1){
        int w, h; cin >> w >> h;
        if(w == 0 && h == 0) return 0;
        vector<vector<int>> board(h, vector<int>(w));
        // xは縦！！！！！！！！！！！！！！！！！！！！！
        int sx, sy;
        rep(i, 0, h)rep(j, 0, w){
            cin >> board[i][j];
            if(board[i][j] == 2){
                sx = i;
                sy = j;
                board[i][j] = 0;
            }
        }
        vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
        auto solve = [&](auto solve, int x, int y, int turn)->int{
            // cout << x << ' ' << y << ' ' << turn << endl;
            if(turn == 10) return IINF;
            int res = IINF;
            rep(i, 0, 4){
                int nx = x, ny = y;
                if(!inr(0, nx+dx[i], h) || !inr(0, ny+dy[i], w) || board[nx+dx[i]][ny+dy[i]] == 1) continue;
                // while(1){
                //     nx += dx[i]; ny += dy[i];
                //     if(!inr(0, nx, h) || !inr(0, ny, w)) break;
                //     if(board[nx][ny] == 3) return turn+1;
                //     if(board[nx][ny] == 1){
                //         board[nx][ny] = 0;
                //         chmin(res, solve(solve, nx-dx[i], ny-dy[i], turn+1));
                //         board[nx][ny] = 1;
                //         break;
                //     }
                // }
                while(inr(0, nx, h) && inr(0, ny, w) && board[nx][ny] == 0) nx += dx[i], ny += dy[i];
                if(!inr(0, nx, h) || !inr(0, ny, w)) continue;
                if(board[nx][ny] == 3) return turn+1;
                if(board[nx][ny] == 1){
                    board[nx][ny] = 0;
                    chmin(res, solve(solve, nx-dx[i], ny-dy[i], turn+1));
                    board[nx][ny] = 1;
                }
            }
            return res;
        };
        int ans = solve(solve, sx, sy, 0);
        if(ans == IINF) cout << -1 << endl;
        else cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3220
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for(int i = a; i < n; i++)
#define rrep(i, a, n) for(int i = a; i >= n; i--)
#define inr(l, x, r) (l <= x && x < r)
#define ll long long
#define ld long double
//#define int long long

constexpr int IINF = 1001001001;
constexpr ll INF = 9e18;

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

int main(){
    while(1){
        int w, h; cin >> w >> h;
        if(w == 0 && h == 0) return 0;
        vector<vector<int>> board(h, vector<int>(w));
        // xは縦！！！！！！！！！！！！！！！！！！！！！
        int sx, sy;
        rep(i, 0, h)rep(j, 0, w){
            cin >> board[i][j];
            if(board[i][j] == 2){
                sx = i;
                sy = j;
            }
        }
        vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
        auto solve = [&](auto solve, int x, int y, int turn)->int{
            // cout << x << ' ' << y << ' ' << turn << endl;
            if(turn == 10) return IINF;
            int res = IINF;
            rep(i, 0, 4){
                int nx = x, ny = y;
                if(!inr(0, nx+dx[i], h) || !inr(0, ny+dy[i], w) || board[nx+dx[i]][ny+dy[i]] == 1) continue;
                while(1){
                    nx += dx[i]; ny += dy[i];
                    if(!inr(0, nx, h) || !inr(0, ny, w)) break;
                    if(board[nx][ny] == 3) return turn+1;
                    if(board[nx][ny] == 1){
                        board[nx][ny] = 0;
                        chmin(res, solve(solve, nx-dx[i], ny-dy[i], turn+1));
                        board[nx][ny] = 1;
                        break;
                    }
                }
            }
            return res;
        };
        int ans = solve(solve, sx, sy, 0);
        if(ans == IINF) cout << -1 << endl;
        else cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3388
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) a.begin(),a.end()
#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)
const long long mod = 1000000007;
const long long INF = 1e18;

int w,h;
int sx,sy,gx,gy;
int ans = 100;
vector<vector<int>> stage (22,vector<int> (22));

void find(int x, int y, int c) {
    if (c == 10) {
        return;
    }
    if (stage[x-1][y] != 1) {
        int i = 1;
        while (true) {
            if (stage[x-i][y] == 0) {
                i++;
                continue;
            }
            if (stage[x-i][y] == 3) {
                ans = min(ans,c+1);
                return;
            }
            if (stage[x-i][y] == -1) {
                break;
            }
            if (stage[x-i][y] == 1) {
                stage[x-i][y] = 0;
                find(x-i+1,y,c+1);
                stage[x-i][y] = 1;
                break;
            }
        }
    }
    if (stage[x+1][y] != 1) {
        int i = 1;
        while (true) {
            if (stage[x+i][y] == 0) {
                i++;
                continue;
            }
            if (stage[x+i][y] == 3) {
                ans = min(ans,c+1);
                return;
            }
            if (stage[x+i][y] == -1) {
                break;
            }
            if (stage[x+i][y] == 1) {
                stage[x+i][y] = 0;
                find(x+i-1,y,c+1);
                stage[x+i][y] = 1;
                break;
            }
        }
    }
    if (stage[x][y-1] != 1) {
        int i = 1;
        while (true) {
            if (stage[x][y-i] == 0) {
                i++;
                continue;
            }
            if (stage[x][y-i] == 3) {
                ans = min(ans,c+1);
                return;
            }
            if (stage[x][y-i] == -1) {
                break;
            }
            if (stage[x][y-i] == 1) {
                stage[x][y-i] = 0;
                find(x,y-i+1,c+1);
                stage[x][y-i] = 1;
                break;
            }
        }
    }
    if (stage[x][y+1] != 1) {
        int i = 1;
        while (true) {
            if (stage[x][y+i] == 0) {
                i++;
                continue;
            }
            if (stage[x][y+i] == 3) {
                ans = min(ans,c+1);
                return;
            }
            if (stage[x][y+i] == -1) {
                break;
            }
            if (stage[x][y+i] == 1) {
                stage[x][y+i] = 0;
                find(x,y+i-1,c+1);
                stage[x][y+i] = 1;
                break;
            }
        }
    }
}

void solve() {
    ans = 100;
    rep(i,h) {
        rep(j,w) {
            cin >> stage[i+1][j+1];
            if (stage[i+1][j+1] == 2) {
                sx = i+1;
                sy = j+1;
                stage[i+1][j+1] = 0;
            }
            if (stage[i+1][j+1] == 3) {
                gx = i+1;
                gy = j+1;
            }
        }
    }

    find(sx,sy,0);

    if (ans == 100) {
        cout << -1 << endl;
    }
    else {
        cout << ans << endl;
    }
    return;
}

int main() {
    while (true) {
        cin >> w >> h;
        if (w == 0) {
            return 0;
        }
        rep(i,h) {
            stage[i+1][0] = -1;
            stage[i+1][w+1] = -1;
        }
        rep(i,w) {
            stage[0][i+1] = -1;
            stage[h+1][i+1] = -1;
        }
        solve();
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3300
final_rank: 3