All Tracks Algorithms Graphs Shortest Path Algorithms Problem

Straightest path
Tag(s):

Algorithms, Dijkstra's algorithm, Easy-Medium, Graph, Shortest path problem

Problem
Editorial
Analytics

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}\)

SAMPLE INPUT
5 4
V...
***.
....
.***
.H.*
SAMPLE OUTPUT
4
Explanation

For the given sample case, Wizard will take first turn at cell \((1, 4)\) , second turn at cell \((3, 4)\) ,third turn at cell \((3, 1)\), and fourth turn at cell \((5, 1)\).

Time Limit: 4,0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

August Circuits '17

OTHER PROBLEMS OF THIS CHALLENGE
Уведомления
View All Notifications

?