After eating too much fruit in Farmer John's kitchen, Bessie the cow is getting some very strange dreams! In her most recent dream, she is trapped in a maze in the shape of an N×M grid of tiles (1≤N,M≤1,000). She starts on the top-left tile and wants to get to the bottom-right tile. When she is standing on a tile, she can potentially move to the adjacent tiles in any of the four cardinal directions.
But wait! Each tile has a color, and each color has a different property! Bessie's head hurts just thinking about it:
If a tile is red, then it is impassable.
If a tile is pink, then it can be walked on normally.
If a tile is orange, then it can be walked on normally, but will make Bessie smell like oranges.
If a tile is blue, then it contains piranhas that will only let Bessie pass if she smells like oranges.
If a tile is purple, then Bessie will slide to the next tile in that direction (unless she is unable to cross it). If this tile is also a purple tile, then Bessie will continue to slide until she lands on a non-purple tile or hits an impassable tile. Sliding through a tile counts as a move. Purple tiles will also remove Bessie's smell.
(If you're confused about purple tiles, the example will illustrate their use.)
Please help Bessie get from the top-left to the bottom-right in as few moves as possible.
奶牛Bessie被困在了N*M的网格图迷宫中,她位于左上角(1,1),出口在右下角(N,M)。Bessie只能上下左右行走。
每块地砖都有一个颜色:
如果是红色,那么不可通行。
如果是粉色,那么可以通行。
如果是橙色,那么可以通行,但是会给Bessie带上橘子的气味。
如果是蓝色,那么当且仅当Bessie带着橘子的气味时,才可以通行。
如果是紫色,那么Bessie会保持原有方向滑过去,如果之后仍然是紫色,那么会继续滑。当滑到不是紫色的地砖上或者不可通行的时候,才会停下来。并且这会消除Bessie身上的气味。每一步滑行和走路一样,都需要耗费一单位时间。
请输出Bessie逃到出口所需的最短时间。