A board on the meter garlic guest of history (BFS) can't all through the bosses don't know where the problem o see
The class Node (object) :
Def __init__ (self, x, y, c) :
Self. X=x
The self. The y=y
C=c
of the self.Def BFS () :
Color=1
Color_x=None
Color_y=None
The queue=[[map_s [0] [0], 0]]
Seen=[[0, 0]]
While the queue:
Try:
Now=queue. Pop (0)
Except BaseException:
The return - 1
If now [0]. X==m - 1 and now [0]==y m - 1:
Return now [1]
# determine whether can use magic
If now [0]. X!=color_x and now [0]. Y!=color_y:
Color=1
Look_list=[]
# whether except through the point of all empty
For I in range (4) :
Lin_tx=now [0]. X + dis [I] [0]
Lin_ty=now [0]. Y + dis [I] [1]
If lin_tx & gt;=0 and lin_tx & lt; M and lin_ty & gt;=0 and lin_ty & lt; M and [lin_tx lin_ty] not seen in:
Look_list. Append (map_s [lin_tx] [lin_ty]. C)
For I in range (4) :
Tx=now [0]. X + dis [I] [0]
Ty=now [0]. Y + dis [I] [1]
If not 0 in look_list and 1 not in look_list:
If tx>=0 and tx=0 and tySeen. Append ([the tx and ty])
Map_s [tx] [ty]. X=tx
Map_s [tx] [ty] y=ty
Map_s [tx] [ty]. C=now [0]. C
Queue. Insert (0, [map_s [tx] [ty], now [1] + 2])
Color_x=tx
Color_y=ty
Color=0
If tx>=0 and tx=0 and tySeen. Append ([the tx and ty])
If map_s [tx] [ty]. C==now [0]. C:
Queue. Append ([map_s [tx] [ty], now [1]])
Elif map_s [tx] [ty]!=now [0]. C:
Queue. Append ([map_s [tx] [ty], now [1] + 1])
The return - 1
If __name__=="__main__" :
M, n=map (int, input (). The split ())
Map_s=[[Node (None, None, None) for I in range (m)] for I in range (m)]
For I in range (n) :
Xx, yy, cc=map (int, input (). The split ())
Map_s xx - [1] [r]. Yy - 1 x=xx - 1
Map_s xx - [1] [r]. Yy - 1 y=yy - 1
Map_s xx - [1] [r]. Yy - 1 c=cc
Dis=[[0, 1], [0, 1], [- 1, 0], [1, 0]]
Ans=BFS ()
If ans!=1:
Print (ans)
The else:
Print (1)