Straightest path
Practice
4.4 (28 votes)
Algorithms
Dijkstra's algorithm
Easy
Grammar Verified
Graphs
Shortest path algorithm
Problem
80% Success 10041 Attempts 30 Points 4s Time Limit 256MB Memory 1024 KB Max Code

You are playing a game on a grid of size \(N \times M\). The game has the following rules:

  • The grid contains cells that the player can move to. These are denoted by a period (.)
  • The grid contains cells that the player cannot move to. These are denoted by an asterisk (*)
  • The player starts on the cell marked with a V.
  • The player has to reach the cell marked with an H.

Write a program to find the path which has the minimum number of changes in direction. Print the number of times that the player needs to turn on the path

Input format

  • First line: N and M
  • Next N lines: M characters (denoting the cells of the grid)

Output format

Print the minimum number of times that the player needs to turn on the required path. If no path exists, print -1.

Constraints

\(1 ≤ N, M ≤ 10^{3}\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
6 votes
Tags:
AlgorithmsApprovedDijkstra's algorithmEasyGraphsHiringRecruitShortest Path Algorithmapproved
Points:30
13 votes
Tags:
AlgorithmsDijkstra's algorithmEasyFloyd Warshall AlgorithmGraphsShortest Path Algorithm
Points:30
33 votes
Tags:
Dijkstra's algorithmEasyGraphsOpenShortest Path Algorithm