We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My Java 8 solution, passing all test-cases, for a Max Score of 30:
The core idea is to implement a Breadth-First-Search (BFS) of a Queue of Points, expanding the search in all four directions (Up, Down, Left, Right) - and sliding along each direction - till we reach either a boundary or an obstacle ('X'), and add each "unvisited" point into the Queue of Points. And all throughout, we keep track of visited (boolean) & visited (distances) for points visited along the BFS. We keep doing this either till the Queue is EMPTY (in which case we have NOT been able to reach the Goal), or till we reach the Goal Point.
This is guaranteed to give us optimal solution, in O(n^2) time.
Ignore the "vDbg" statements, as those are just for diagnostic / debug purposes, to understand the logic or debug better. You can enable / disable it via the "vDbg" flag. If enabled, it might slow execution time, resulting in failing some test-cases due to TLEs. I
classResult{// Only given for Diagnostic / Debug purposes.// Enabling this might result in slower execution.// Hence, it might fail test-cases with TLEs.privatestaticfinalbooleanvDbg=false;privatestaticclassPoint{introwIdx,colIdx;Point(introwIdx,intcolIdx){this.rowIdx=rowIdx;this.colIdx=colIdx;}@OverridepublicStringtoString(){return"("+rowIdx+","+colIdx+")";}}/* * Complete the 'minimumMoves' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. STRING_ARRAY grid * 2. INTEGER startX * 3. INTEGER startY * 4. INTEGER goalX * 5. INTEGER goalY */publicstaticintminimumMoves(List<String>grid,intstartX,intstartY,intgoalX,intgoalY){// Write your code hereintn=grid.size();// "visited[i][j]" indicates whether point (i,j) has been visited in BFSboolean[][]visited=newboolean[n][n];// "numMoves[i][j]" indicates the number of moves it takes to get to point (i,j)int[][]numMoves=newint[n][n];// Queue of Points, that's populated & de-populated during BFSQueue<Point>pointsQ=newArrayDeque<>();// Initialize the Start Conditions for Breadth-First-Search (BFS)pointsQ.add(newPoint(startX,startY));visited[startX][startY]=true;numMoves[startX][startY]=0;// Direction Offsets along Rows / Columns// We can go four Directions: Up, Down, Left, Rightint[]dirRowOffset={1,-1,0,0};int[]dirColOffset={0,0,1,-1};// Perform the Breadth-First-Search (BFS) using the Points Queue.while(!pointsQ.isEmpty()){PointbfsPoint=pointsQ.poll();intcurNumMoves=numMoves[bfsPoint.rowIdx][bfsPoint.colIdx];if(vDbg){System.err.println("");System.err.println("");System.err.println("pointsQ.size() = >"+pointsQ.size()+"< | "+"bfsPoint = >"+bfsPoint+"< | "+"curNumMoves = >"+curNumMoves+"<");System.err.println("pointsQ = >"+pointsQ+"<");}if(bfsPoint.rowIdx==goalX&&bfsPoint.colIdx==goalY){if(vDbg){System.err.println("Success! Reached Goal in >"+curNumMoves+"< moves!");}returncurNumMoves;}// Slide in all 4 directionsfor(intdirIdx=0;dirIdx<4;dirIdx++){intnextRowIdx=bfsPoint.rowIdx+dirRowOffset[dirIdx];intnextColIdx=bfsPoint.colIdx+dirColOffset[dirIdx];if(vDbg){System.err.println("Starting along DIR: "+"dirIdx = >"+dirIdx+"< | "+"nextRowIdx = >"+nextRowIdx+"< | "+"nextColIdx = >"+nextColIdx+"<");}// Slide in the SAME direction until boundary or blockedwhile(nextRowIdx>=0&&nextRowIdx<n&&nextColIdx>=0&&nextColIdx<n&&grid.get(nextRowIdx).charAt(nextColIdx)=='.'){if(vDbg){System.err.println("Sliding along DIR: "+"dirIdx = >"+dirIdx+"< | "+"nextRowIdx = >"+nextRowIdx+"< | "+"nextColIdx = >"+nextColIdx+"< | "+"visited = >"+visited[nextRowIdx][nextColIdx]+"<");}if(!visited[nextRowIdx][nextColIdx]){PointnextPoint=newPoint(nextRowIdx,nextColIdx);visited[nextRowIdx][nextColIdx]=true;numMoves[nextRowIdx][nextColIdx]=(curNumMoves+1);pointsQ.add(nextPoint);if(vDbg){System.err.println("Adding new UNVISITED Point "+nextPoint+" into the Queue, "+"for further BFS.");}}// Slide further in the SAME directionnextRowIdx+=dirRowOffset[dirIdx];nextColIdx+=dirColOffset[dirIdx];}}}return-1;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Castle on the Grid
You are viewing a single comment's thread. Return to all comments →
My Java 8 solution, passing all test-cases, for a Max Score of 30:
The core idea is to implement a Breadth-First-Search (BFS) of a Queue of Points, expanding the search in all four directions (Up, Down, Left, Right) - and sliding along each direction - till we reach either a boundary or an obstacle ('X'), and add each "unvisited" point into the Queue of Points. And all throughout, we keep track of visited (boolean) & visited (distances) for points visited along the BFS. We keep doing this either till the Queue is EMPTY (in which case we have NOT been able to reach the Goal), or till we reach the Goal Point.
This is guaranteed to give us optimal solution, in
O(n^2)
time.Ignore the "vDbg" statements, as those are just for diagnostic / debug purposes, to understand the logic or debug better. You can enable / disable it via the "vDbg" flag. If enabled, it might slow execution time, resulting in failing some test-cases due to TLEs. I