Hosuk Saurus
InterviewTime limit1sMemory limit512 MB
A cow moves on a grid where allowed directions cycle with the move number (any, vertical only, horizontal only); find the minimum total cell shock from start to exit.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Dynamic programming, BFS
- Solved
- No attempts yet
Problem
Moo... Hosuk Saurus, a foolish cow, has no flexibility at all. He insists on escaping the maze stubbornly, exactly according to his own iron rule. The maze is an by grid, and each cell has a shock value that he takes the moment he enters it. If he enters the same room several times, he takes the same shock value each time. A smart cow would of course escape the maze while taking the least shock, or better yet would have used its head to avoid falling into the maze in the first place, but Hosuk Saurus has none of that!
His iron rule concerns how he moves. The directions he can move in differ for every move.

- On the -th move, he can move to one of the cells adjacent up, down, left, or right.
- On the -th move, he can move to one of the cells adjacent up or down.
- On the -th move, he can move to one of the cells adjacent left or right.
- If there is a wall where he wants to move, he cannot move there.
- The first move is move 1, followed by move 2, move 3, and so on.
Help Hosuk Saurus, who follows his iron rule but hates pain, and find the minimum total shock to reach the exit!
Input
The first line gives the grid dimensions and .
The second line gives the start and end positions , , , , separated by spaces. The start is at row , column , and the end is at row , column . The start and end are always different.
From the third line, lines give the map. Each line contains integers. The -th number on line is the shock value of the cell at row , column . If a shock value is , that cell is a wall.
The shock values of the start and end are guaranteed to be 0.
Output
On the first line, print the minimum total shock Hosuk Saurus takes while escaping. If he cannot escape, print .
Constraints
- 1 ≤ , ≤ 100
- 1 ≤ ≤
- 1 ≤ ≤
- -1 ≤ shock value of each cell ≤ 300