Created
August 23, 2017 06:09
-
-
Save iamutkarshtiwari/dec734ad185856b47508685c7d879eae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* IMPORTANT: Multiple classes and nested static classes are supported */ | |
/* | |
* uncomment this if you want to read input. | |
//import for Scanner and other utility classes | |
*/ | |
// import java.util.*; | |
// imports for BufferedReader | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.util.StringTokenizer; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
class Coord { | |
int x; | |
int y; | |
char dir; | |
int turn; | |
Coord(int x, int y, int turn, char dir) { | |
this.x = x; | |
this.y = y; | |
this.dir = dir; | |
this.turn = turn; | |
} | |
} | |
class TestClass { | |
static int flag = -1; | |
static int minTurns = Integer.MAX_VALUE; | |
public static void main(String args[] ) throws Exception { | |
try { | |
BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); | |
StringTokenizer tk = new StringTokenizer(input.readLine()); | |
int N = Integer.parseInt(tk.nextToken()); | |
int M = Integer.parseInt(tk.nextToken()); | |
flag = -1; | |
minTurns = Integer.MAX_VALUE; | |
char array[][] = new char[N][M]; | |
int x = 0; | |
int y = 0; | |
for (int i = 0; i < N; i++) { | |
String str = input.readLine(); | |
char temp[] = str.toCharArray(); | |
for (int j = 0; j < M; j++) { | |
array[i][j] = temp[j]; | |
if (temp[j] == 'V') { | |
x = i; | |
y = j; | |
} | |
} | |
} | |
boolean visited[][] = new boolean[N][M]; | |
path(array, visited, x, y, 0, ' '); | |
if (flag == -1) { | |
System.out.println(flag); | |
} else { | |
System.out.println(minTurns); | |
} | |
} catch(Throwable e) { | |
e.printStackTrace(); | |
// System.out.print("he"); | |
} | |
} | |
public static boolean isValid(char array[][], boolean visited[][], int x, int y) { | |
if (x >= 0 && x < array.length && y >=0 && y < array[0].length && | |
visited[x][y] == false && array[x][y] != '*' ) { | |
return true; | |
} | |
return false; | |
} | |
public static void path(char array[][], boolean visited[][], int x, int y, int turn, char dir) { | |
Queue<Coord> queue = new LinkedList<Coord>(); | |
queue.add(new Coord(x, y, 0, ' ')); | |
visited[x][y] = true; | |
while (queue.peek() != null) { | |
Coord temp = queue.poll(); | |
if (array[temp.x][temp.y] == 'H') { | |
flag = 1; | |
if (temp.turn < minTurns) { | |
minTurns = temp.turn; | |
} | |
visited[temp.x][temp.y] = false; | |
continue; | |
} | |
if (isValid(array, visited, temp.x, temp.y+1)) { | |
visited[temp.x][temp.y+1] = true; | |
queue.add(new Coord(temp.x, temp.y+1, (temp.dir == 'R' || temp.dir == ' ') ? temp.turn: temp.turn+1, 'R')); | |
} | |
if (isValid(array, visited, temp.x, temp.y-1)) { | |
visited[temp.x][temp.y-1] = true; | |
queue.add(new Coord(temp.x, temp.y-1, (temp.dir == 'L' || temp.dir == ' ') ? temp.turn: temp.turn+1, 'L')); | |
} | |
if (isValid(array, visited, temp.x+1, temp.y)) { | |
visited[temp.x+1][temp.y] = true; | |
queue.add(new Coord(temp.x+1, temp.y, (temp.dir == 'D' || temp.dir == ' ') ? temp.turn: temp.turn+1, 'D')); | |
} | |
if (isValid(array, visited, temp.x-1, temp.y)) { | |
visited[temp.x-1][temp.y] = true; | |
queue.add(new Coord(temp.x-1, temp.y, (temp.dir == 'U' || temp.dir == ' ') ? temp.turn: temp.turn+1, 'U')); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment