#include #include #include #define WALL 1 #define SPACE 0 #define BALL 2 #define MAX_SIZE 22 using namespace std; class State{ public: int h,w; int b[MAX_SIZE][MAX_SIZE]; bool operator< (const State &s) const { for(int i=0;is.b[i][j]) return false; } return false; } }; int w, h; int sR, sC, gR, gC; int board[MAX_SIZE][MAX_SIZE]; bool read(){ cin >> w >> h; if(w==0 && h==0) return false; for(int i=0;i> board[i][j]; if(board[i][j]==3){ board[i][j] = SPACE; gR = i, gC = j; } if(board[i][j]==2){ board[i][j] = BALL; sR = i, sC = j; } } } return true; } int isPass(int r, int c, State &s){ if(!(0<=r && r Q; map M; init.h = h; init.w = w; for(int i=0;i=10) continue; for(int i=0;icurrCost+1){ M[toPush] = currCost+1; Q.push(toPush); } } if(gR==nextR && gC==nextC) return currCost+1; } } return -1; } void work(){ cout << bfs() << endl; } int main(){ while(read()) work(); return 0; }