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 routes, print -1 instead.
Each line should not have any character other than 
this number.

Sample Input

2 1
3 2
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
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0

Output for the Sample Input

1
4
-1
4
10
-1



Sample input:
2 1
3 2
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
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0


Sample output:
1
4
-1
4
10
-1

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
drc = [(0, -1), (1, 0), (0, 1), (-1, 0)]


def in_board(r, c):
    return 0 <= r < H and 0 <= c < W


while True:
    W, H = map(int, input().split())
    if not (W | H):
        break
    origin_board = [[int(x) for x in input().split()] for _ in range(H)]
    sr, sc, gr, gc = 0, 0, 0, 0
    for r in range(H):
        for c in range(W):
            if origin_board[r][c] == 2:
                sr, sc = r, c
                origin_board[sr][sc] = 0
            elif origin_board[r][c] == 3:
                gr, gc = r, c
    memo = dict()
    que = collections.deque([(sr, sc, origin_board, 0)])
    while que:
        r, c, board, cnt = que.popleft()
        if (r, c) == (gr, gc):
            print(cnt)
            break
        for i, (dr, dc) in enumerate(drc):
            nr, nc = r + dr, c + dc
            if (r, c, i) in memo:
                nr, nc = memo[r, c, i]
            if in_board(nr, nc) and board[nr][nc] != 1:
                next_board = copy.deepcopy(board)
                while in_board(nr, nc) and board[nr][nc] == 0:
                    nr += dr
                    nc += dc
                if in_board(nr, nc) and cnt < 10:
                    if next_board[nr][nc] == 1:
                        next_board[nr][nc] = 0
                        nr, nc = nr - dr, nc - dc
                    memo[r, c, i] = nr, nc
                    que.append((nr, nc, next_board, cnt + 1))

    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
drc = [(0, -1), (1, 0), (0, 1), (-1, 0)]


def in_board(r, c):
    return 0 <= r < H and 0 <= c < W


while True:
    W, H = map(int, input().split())
    if not (W | H):
        break
    origin_board = [[int(x) for x in input().split()] for _ in range(H)]
    sr, sc, gr, gc = 0, 0, 0, 0
    for r in range(H):
        for c in range(W):
            if origin_board[r][c] == 2:
                sr, sc = r, c
                origin_board[sr][sc] = 0
            elif origin_board[r][c] == 3:
                gr, gc = r, c
    memo = dict()
    que = collections.deque([(sr, sc, origin_board, 0)])
    while que:
        r, c, board, cnt = que.popleft()
        if (r, c) == (gr, gc):
            print(cnt)
            break
        if cnt == 10:
            continue
        for i, (dr, dc) in enumerate(drc):
            nr, nc = r + dr, c + dc
            if (r, c, i) in memo:
                nr, nc = memo[r, c, i]
            if in_board(nr, nc) and board[nr][nc] != 1:
                next_board = copy.deepcopy(board)
                while in_board(nr, nc) and board[nr][nc] == 0:
                    nr += dr
                    nc += dc
                if in_board(nr, nc) and cnt < 10:
                    if next_board[nr][nc] == 1:
                        next_board[nr][nc] = 0
                        nr, nc = nr - dr, nc - dc
                    memo[r, c, i] = nr, nc
                    que.append((nr, nc, next_board, cnt + 1))

    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
drc = [(0, -1), (1, 0), (0, 1), (-1, 0)]


def in_board(r, c):
    return 0 <= r < H and 0 <= c < W


def bfs(origin_board):
    sr, sc, gr, gc = 0, 0, 0, 0
    for r in range(H):
        for c in range(W):
            if origin_board[r][c] == 2:
                sr, sc = r, c
            elif origin_board[r][c] == 3:
                gr, gc = r, c
    origin_board[sr][sc] = origin_board[gr][gc] = 0
    que = collections.deque([(sr, sc, origin_board, 0)])
    while que:
        r, c, board, cnt = que.popleft()
        if cnt == 10:
            continue
        for dr, dc in drc:
            nr, nc = r + dr, c + dc
            if in_board(nr, nc) and board[nr][nc] != 1:
                next_board = copy.deepcopy(board)
                while in_board(nr, nc) and board[nr][nc] == 0:
                    if (nr, nc) == (gr, gc):
                        return cnt + 1
                    nr += dr
                    nc += dc
                if in_board(nr, nc) and cnt < 9:
                    next_board[nr][nc] = 0
                    que.append((nr - dr, nc - dc, next_board, cnt + 1))
    return -1


while True:
    W, H = map(int, input().split())
    if not (W | H):
        break
    origin_board = [[int(x) for x in input().split()] for _ in range(H)]
    print(bfs(origin_board))

Submission status: Time Limit Exceeded

Solution code:
#Problem with ligoal and masu
def move(x,y,lo,ls,count = 0):
    if(not([x,y-1]  in lo and [x,y+1] in lo)):
        moveup(x,y,lo,ls,count)
        movedown(x,y,lo,ls,count)
    if (not ([x-1, y] in lo and [x+1,y] in lo)):
        moveleft(x,y,lo,ls,count)
        moveright(x,y,lo,ls,count)

def moveup(x,y,lo,ls,count):
    global saisyou
    if(y == 0):
        return
    else:
        while True:
            y -= 1
            if(y <= -1):
                return
            if([x,y] in ligoal):
                if(saisyou > count):
                    saisyou = count +1
                return
            if([x,y] in lo):
                lo.remove([x,y])
                ls.append([x,y])
                move(x,y+1,lo,ls,count+1)
                ls.remove([x,y])
                lo.append([x,y])
                return
def movedown(x,y,lo,ls,count):
    global saisyou
    if(y == masu[1]-1):
        return
    else:
        while True:
            y += 1
            if(y >= masu[1]):
                return
            if([x,y] in ligoal):
                if(saisyou > count):
                    saisyou = count + 1
                return
            if([x,y] in lo):
                lo.remove([x,y])
                ls.append([x,y])
                move(x,y-1,lo,ls,count +1)
                ls.remove([x,y])
                lo.append([x,y])
                return
def moveleft(x,y,lo,ls,count):
    global saisyou
    if(x == 0):
        return
    else:
        while True:
            x -= 1
            if(x <= -1):
                return
            if([x,y] in ligoal):
                if(saisyou > count):
                    saisyou = count +1
                return
            if([x,y] in lo):
                lo.remove([x,y])
                ls.append([x,y])
                move(x+1,y,lo,ls,count + 1)
                ls.remove([x,y])
                lo.append([x,y])
                return
def moveright(x,y,lo,ls,count):
    global saisyou
    if(x == masu[0]-1):
        return
    else:
        while True:
            x += 1
            if(x >= masu[0]):
                return
            if([x,y] in ligoal):
                if(saisyou > count):
                    saisyou = count + 1
                return
            if([x,y] in lo):
                lo.remove([x,y])
                ls.append([x,y])
                move(x-1,y,lo,ls,count + 1)
                ls.remove([x,y])
                lo.append([x,y])
                return

while True:
    li = []
    liobst = []
    ligoal = []
    listart = []
    lino = []
    masu = []
    masu.extend(map(int,raw_input().split()))
    if([0,0] in masu):
        break
    for i in xrange(masu[1]):
        li.append(map(int,raw_input().split()))
    nox = -1
    for i in li:
        nox += 1
        noy = -1
        for j in i:
            noy += 1
            if(j == 1):
                liobst.append([noy,nox])
            elif(j == 2):
                listart.append([noy, nox])
            elif(j == 3):
                ligoal.append([noy, nox])
            else:
                lino.append([noy,nox])
    global saisyou
    saisyou = 11
    move(listart[0][0], listart[0][1],liobst,lino)
    if (saisyou >= 11):
        print -1
    else:
        print saisyou

Submission status: Time Limit Exceeded

Solution code:
def searchu(li,pos,count):
    global saisyou
    x = pos[0]
    y = pos[1]
    xlen = len(li)
    ylen = len(li[0])
    while True:
        if(x == 0):
            return
        if(x != xlen-1):
            if(li[x+1][y]== 1 and li[x-1][y]== 1):
                return
        x -= 1
        if(li[x][y] == 1):
            li[x][y] = 0
            search(li,[x+1,y],count+1)
            li[x][y] = 1
            return
        if(li[x][y] == 3):
            if(saisyou > count +1):
                saisyou = count+1
            return

def searchd(li,pos,count):
    global saisyou
    x = pos[0]
    y = pos[1]
    xlen = len(li)
    ylen = len(li[0])
    while True:
        if(x == xlen-1):
            return
        if(x != 0):
            if(li[x+1][y]== 1 and li[x-1][y]== 1):
                return
        x += 1
        if(li[x][y] == 1):
            li[x][y] = 0
            search(li,[x-1,y],count+1)
            li[x][y] = 1
            return
        if(li[x][y] == 3):
            if (saisyou > count + 1):
                saisyou = count+1
            return

def searchl(li,pos,count):
    global saisyou
    x = pos[0]
    y = pos[1]
    xlen = len(li)
    ylen = len(li[0])
    while True:
        if(y == 0):
            return
        if(y != ylen-1):
            if(li[x][y+1]== 1 and li[x][y-1]== 1):
                return
        y -= 1
        if(li[x][y] == 1):
            li[x][y] = 0
            search(li,[x,y+1],count+1)
            li[x][y] = 1
            return
        if(li[x][y] == 3):
            if (saisyou > count + 1):
                saisyou = count+1
            return

def searchr(li,pos,count):
    global saisyou
    x = pos[0]
    y = pos[1]
    xlen = len(li)
    ylen = len(li[0])
    while True:
        if(y == ylen-1):
            return
        if(y != 0):
            if(li[x][y+1]== 1 and li[x][y-1]== 1):
                return
        y += 1
        if(li[x][y] == 1):
            li[x][y] = 0
            search(li,[x,y-1],count+1)
            li[x][y] = 1
            return
        if(li[x][y] == 3):
            if (saisyou > count + 1):
                saisyou = count+1
            return
def search(li,pos,count = 0):
    searchu(li,pos,count)
    searchd(li,pos,count)
    searchl(li,pos,count)
    searchr(li,pos,count)

while True:
    li = []
    st = []
    masu = map(int,raw_input().split())
    if(masu == [0,0]):
        break
    for i in xrange(masu[1]):
        li.append(map(int,raw_input().split()))
    num = -1
    for i in li:
        num +=1
        numm = -1
        for j in i:
            numm += 1
            if(j == 2):
                st.extend([num,numm])
                li[num][numm] = 0
    global saisyou
    saisyou = 11
    search(li,st)
    print li
    if(saisyou >= 11):
        print -1
    else:
        print saisyou

Submission status: Time Limit Exceeded

Solution code:
def move(data,rest_n,i,j):
    d1,d2,d3,d4 = [[data[i][j] for j in range(len(data[i]))] for i in range(len(data))],[[data[i][j] for j in range(len(data[i]))] for i in range(len(data))], [[data[i][j] for j in range(len(data[i]))] for i in range(len(data))], [[data[i][j] for j in range(len(data[i]))] for i in range(len(data))]
    if not rest_n:
        return(-1)
    a,b,c,d = r_move(d1,rest_n,i,j) , u_move(d2,rest_n,i,j) , d_move(d3,rest_n,i,j) , l_move(d4,rest_n,i,j)
    if a == b == c == d == -1:
        return(-1)
    else:
        return(min([t for t in [a,b,c,d] if t >= 0]))

def l_move(data,rest_n,i,j):
    tmp = data
    if j == 0 or tmp[i][j-1] == 1:
        return(-1)
    while(j):
        if tmp[i][j-1] == 0:
            tmp[i][j] = 0
            j -= 1
            tmp[i][j] = 2
        elif tmp[i][j-1] == 3:
            return(11-rest_n)
        else:
            tmp[i][j-1] = 0
            return(move(tmp,rest_n-1,i,j))
    return(-1)

def u_move(data,rest_n,i,j):
    tmp = data
    if i == 0 or tmp[i-1][j] == 1:
        return(-1)
    while(i):
        if tmp[i-1][j] == 0:
            tmp[i][j] = 0
            i -= 1
            tmp[i][j] = 2
        elif tmp[i-1][j] == 3:
            return(11-rest_n)
        else:
            tmp[i-1][j] = 0
            return(move(tmp,rest_n-1,i,j))
    return(-1)

def r_move(data,rest_n,i,j):
    tmp = data
    global y
    if j == y-1 or tmp[i][j+1] == 1:
        return(-1)
    while(j < y-1):
        if tmp[i][j+1] == 0:
            tmp[i][j] = 0
            j += 1
            tmp[i][j] = 2
        elif tmp[i][j+1] == 3:
            return(11-rest_n)
        else:
            tmp[i][j+1] = 0
            return(move(tmp,rest_n-1,i,j))
    return(-1)

def d_move(data,rest_n,i,j):
    tmp = data
    global x
    if i == x-1 or tmp[i+1][j] == 1:
        return(-1)
    while(i < x-1):
        if tmp[i+1][j] == 0:
            tmp[i][j] = 0
            i += 1
            tmp[i][j] = 2
        elif tmp[i+1][j] == 3:
            return(11-rest_n)
        else:
            tmp[i+1][j] = 0
            return(move(tmp,rest_n-1,i,j))
    return(-1)

while(True):
    y,x = map(int,input().split())
    if x == 0:
        break
    data = []
    for i in range(x):
        data.append(list(map(int, input().split())))
    for i in range(x):
        for j in range(y):
            if data[i][j] == 2:
                start_x, start_y = i,j
    print(move(data,10,start_x,start_y))

Submission status: Time Limit Exceeded

Solution code:
def move(data,rest_n,i,j):
    if not rest_n:
        return(-1)
    a,b,c,d = r_move(data,rest_n,i,j) , u_move(data,rest_n,i,j) , d_move(data,rest_n,i,j) , l_move(data,rest_n,i,j)
    if a == b == c == d == -1:
        return(-1)
    else:
        return(min([t for t in [a,b,c,d] if t >= 0]))

def l_move(data,rest_n,i,j):
    tmp = [[data[p][q] for q in range(len(data[i]))] for p in range(len(data))]
    if j == 0 or tmp[i][j-1] == 1:
        return(-1)
    while(j):
        if tmp[i][j-1] == 0:
            tmp[i][j] = 0
            j -= 1
            tmp[i][j] = 2
        elif tmp[i][j-1] == 3:
            return(11-rest_n)
        else:
            tmp[i][j-1] = 0
            return(move(tmp,rest_n-1,i,j))
    return(-1)

def u_move(data,rest_n,i,j):
    tmp = data
    if i == 0 or tmp[i-1][j] == 1:
        return(-1)
    while(i):
        if tmp[i-1][j] == 0:
            tmp[i][j] = 0
            i -= 1
            tmp[i][j] = 2
        elif tmp[i-1][j] == 3:
            return(11-rest_n)
        else:
            tmp[i-1][j] = 0
            return(move(tmp,rest_n-1,i,j))
    return(-1)

def r_move(data,rest_n,i,j):
    tmp = data
    global y
    if j == y-1 or tmp[i][j+1] == 1:
        return(-1)
    while(j < y-1):
        if tmp[i][j+1] == 0:
            tmp[i][j] = 0
            j += 1
            tmp[i][j] = 2
        elif tmp[i][j+1] == 3:
            return(11-rest_n)
        else:
            tmp[i][j+1] = 0
            return(move(tmp,rest_n-1,i,j))
    return(-1)

def d_move(data,rest_n,i,j):
    tmp = data
    global x
    if i == x-1 or tmp[i+1][j] == 1:
        return(-1)
    while(i < x-1):
        if tmp[i+1][j] == 0:
            tmp[i][j] = 0
            i += 1
            tmp[i][j] = 2
        elif tmp[i+1][j] == 3:
            return(11-rest_n)
        else:
            tmp[i+1][j] = 0
            return(move(tmp,rest_n-1,i,j))
    return(-1)

while(True):
    y,x = map(int,input().split())
    if x == 0:
        break
    data = []
    for i in range(x):
        data.append(list(map(int, input().split())))
    for i in range(x):
        for j in range(y):
            if data[i][j] == 2:
                start_x, start_y = i,j
    print(move(data,10,start_x,start_y))

Submission status: Time Limit Exceeded

Solution code:
from queue import Queue as q

def l_move(data):
    global flag
    field,n,i,j = [[data[0][p][q] for q in range(len(data[0][0]))] for p in range(len(data[0]))],data[1],data[2],data[3]
    if j == 0 or field[i][j-1] == 1 or n == 0:
        return(None)
    while(j):
        if field[i][j-1] == 0:
            field[i][j] = 0
            j -= 1
            field[i][j] = 2
        elif field[i][j-1] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i][j-1] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

def u_move(data):
    global flag
    field,n,i,j = [[data[0][p][q] for q in range(len(data[0][0]))] for p in range(len(data[0]))],data[1],data[2],data[3]
    if i == 0 or field[i-1][j] == 1 or n == 0:
        return(None)
    while(i):
        if field[i-1][j] == 0:
            field[i][j] = 0
            i -= 1
            field[i][j] = 2
        elif field[i-1][j] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i-1][j] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

def r_move(data):
    global flag
    field,n,i,j = [[data[0][p][q] for q in range(len(data[0][0]))] for p in range(len(data[0]))],data[1],data[2],data[3]
    global y
    if j == y-1 or field[i][j+1] == 1 or n == 0:
        return(None)
    while(j < y-1):
        if field[i][j+1] == 0:
            field[i][j] = 0
            j += 1
            field[i][j] = 2
        elif field[i][j+1] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i][j+1] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

def d_move(data):
    global flag
    field,n,i,j = [[data[0][p][q] for q in range(len(data[0][0]))] for p in range(len(data[0]))],data[1],data[2],data[3]
    global x
    if i == x-1 or field[i+1][j] == 1 or n == 0:
        return(None)
    while(i < x-1):
        if field[i+1][j] == 0:
            field[i][j] = 0
            i += 1
            field[i][j] = 2
        elif field[i+1][j] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i+1][j] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

while(True):
    flag = False
    y,x = map(int,input().split())
    lis = q()
    if x == 0:
        break
    field = []
    for i in range(x):
        field.append(list(map(int, input().split())))
    for i in range(x):
        for j in range(y):
            if field[i][j] == 2:
                start_x, start_y = i,j
    data = [field,10,start_x,start_y]
    lis.put(data)
    while(lis.qsize()):
        d = lis.get()
        tmp = l_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
        tmp = u_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
        tmp = d_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
        tmp = r_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
    if not flag:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
from queue import Queue as q

def l_move(data):
    global flag
    field,n,i,j = data[0],data[1],data[2],data[3]
    if j == 0 or field[i][j-1] == 1 or n == 0:
        return(None)
    while(j):
        if field[i][j-1] == 0:
            field[i][j] = 0
            j -= 1
            field[i][j] = 2
        elif field[i][j-1] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i][j-1] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

def u_move(data):
    global flag
    field,n,i,j = data[0],data[1],data[2],data[3]
    if i == 0 or field[i-1][j] == 1 or n == 0:
        return(None)
    while(i):
        if field[i-1][j] == 0:
            field[i][j] = 0
            i -= 1
            field[i][j] = 2
        elif field[i-1][j] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i-1][j] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

def r_move(data):
    global flag
    field,n,i,j = data[0],data[1],data[2],data[3]
    global y
    if j == y-1 or field[i][j+1] == 1 or n == 0:
        return(None)
    while(j < y-1):
        if field[i][j+1] == 0:
            field[i][j] = 0
            j += 1
            field[i][j] = 2
        elif field[i][j+1] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i][j+1] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

def d_move(data):
    global flag
    field,n,i,j = data[0],data[1],data[2],data[3]
    global x
    if i == x-1 or field[i+1][j] == 1 or n == 0:
        return(None)
    while(i < x-1):
        if field[i+1][j] == 0:
            field[i][j] = 0
            i += 1
            field[i][j] = 2
        elif field[i+1][j] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i+1][j] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

while(True):
    flag = False
    y,x = map(int,input().split())
    lis = q()
    if x == 0:
        break
    field = []
    for i in range(x):
        field.append(list(map(int, input().split())))
    for i in range(x):
        for j in range(y):
            if field[i][j] == 2:
                start_x, start_y = i,j
    data = [field,10,start_x,start_y]
    lis.put(data)
    while(lis.qsize()):
        d = lis.get()
        tmp = l_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
        tmp = u_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
        tmp = d_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
        tmp = r_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
    if not flag:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
from queue import Queue as q
from copy import deepcopy as cp

def l_move(data):
    global flag
    field,n,i,j = cp(data[0]),data[1],data[2],data[3]
    if j == 0 or field[i][j-1] == 1 or n == 0:
        return(None)
    while(j):
        if field[i][j-1] == 0:
            field[i][j] = 0
            j -= 1
            field[i][j] = 2
        elif field[i][j-1] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i][j-1] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

def u_move(data):
    global flag
    field,n,i,j = cp(data[0]),data[1],data[2],data[3]
    if i == 0 or field[i-1][j] == 1 or n == 0:
        return(None)
    while(i):
        if field[i-1][j] == 0:
            field[i][j] = 0
            i -= 1
            field[i][j] = 2
        elif field[i-1][j] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i-1][j] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

def r_move(data):
    global flag
    field,n,i,j = cp(data[0]),data[1],data[2],data[3]
    global y
    if j == y-1 or field[i][j+1] == 1 or n == 0:
        return(None)
    while(j < y-1):
        if field[i][j+1] == 0:
            field[i][j] = 0
            j += 1
            field[i][j] = 2
        elif field[i][j+1] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i][j+1] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

def d_move(data):
    global flag
    field,n,i,j = cp(data[0]),data[1],data[2],data[3]
    global x
    if i == x-1 or field[i+1][j] == 1 or n == 0:
        return(None)
    while(i < x-1):
        if field[i+1][j] == 0:
            field[i][j] = 0
            i += 1
            field[i][j] = 2
        elif field[i+1][j] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i+1][j] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

while(True):
    flag = False
    y,x = map(int,input().split())
    lis = q()
    if x == 0:
        break
    field = []
    for i in range(x):
        field.append(list(map(int, input().split())))
    for i in range(x):
        for j in range(y):
            if field[i][j] == 2:
                start_x, start_y = i,j
    data = [field,10,start_x,start_y]
    lis.put(data)
    while(lis.qsize()):
        d = lis.get()
        tmp = l_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
        tmp = u_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
        tmp = d_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
        tmp = r_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
    if not flag:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
from queue import Queue as q
from copy import deepcopy as cp

def l_move(data):
    global flag
    field,n,i,j = data[0],data[1],data[2],data[3]
    if j == 0 or field[i][j-1] == 1 or n == 0:
        return(None)
    field = cp(field)
    while(j):
        if field[i][j-1] == 0:
            field[i][j] = 0
            j -= 1
            field[i][j] = 2
        elif field[i][j-1] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i][j-1] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

def u_move(data):
    global flag
    field,n,i,j = data[0],data[1],data[2],data[3]
    if i == 0 or field[i-1][j] == 1 or n == 0:
        return(None)
    field = cp(field)
    while(i):
        if field[i-1][j] == 0:
            field[i][j] = 0
            i -= 1
            field[i][j] = 2
        elif field[i-1][j] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i-1][j] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

def r_move(data):
    global flag
    field,n,i,j = data[0],data[1],data[2],data[3]
    global y
    if j == y-1 or field[i][j+1] == 1 or n == 0:
        return(None)
    field = cp(field)
    while(j < y-1):
        if field[i][j+1] == 0:
            field[i][j] = 0
            j += 1
            field[i][j] = 2
        elif field[i][j+1] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i][j+1] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

def d_move(data):
    global flag
    field,n,i,j = data[0],data[1],data[2],data[3]
    global x
    if i == x-1 or field[i+1][j] == 1 or n == 0:
        return(None)
    field = cp(field)
    while(i < x-1):
        if field[i+1][j] == 0:
            field[i][j] = 0
            i += 1
            field[i][j] = 2
        elif field[i+1][j] == 3:
            flag = True
            print(11-n)
            return(None)
        else:
            field[i+1][j] = 0
            n -= 1
            return([field,n,i,j])
    return(None)

while(True):
    flag = False
    y,x = map(int,input().split())
    lis = q()
    if x == 0:
        break
    field = []
    for i in range(x):
        field.append(list(map(int, input().split())))
    for i in range(x):
        for j in range(y):
            if field[i][j] == 2:
                start_x, start_y = i,j
    data = [field,10,start_x,start_y]
    lis.put(data)
    while(lis.qsize()):
        d = lis.get()
        tmp = l_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
        tmp = u_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
        tmp = d_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
        tmp = r_move(d)
        if flag:
            break
        if tmp != None:
            lis.put(tmp)
    if not flag:
        print(-1)