Print path from given Source to Destination in 2 | Function to print the path ; Base condition ; Pop stores elements ; Recursive call for printing stack In reverse order ; Function to store the path into The stack , if path exist ; Base condition ; Push current elements ; Condition to check whether reach to the Destination or not ; Increment ' x ' ordinate of source by ( 2 * x + y ) Keeping ' y ' constant ; Increment ' y ' ordinate of source by ( 2 * y + x ) Keeping ' x ' constant ; Pop current elements form stack ; If no path exists ; Utility function to check whether path exist or not ; To store x co - ordinate ; To store y co - ordinate ; Function to find the path ; Print - 1 , if path doesn 't exist ; Driver code ; Function call
function printExistPath ( sx , sy , last ) { if ( sx . length == 0 sy . length == 0 ) { return ; } let x = sx [ sx . length - 1 ] ; let y = sy [ sy . length - 1 ] ; sx . pop ( ) ; sy . pop ( ) ; printExistPath ( sx , sy , last ) ; if ( x == 83 ) { document . write ( " " + x + " " + y + " " ) ; } else { document . write ( " " + x + " " + y + " " ) ; } } function storePath ( srcX , srcY , destX , destY , sx , sy ) { if ( srcX > destX srcY > destY ) { return false ; } sx . push ( srcX ) ; sy . push ( srcY ) ; if ( srcX == destX && srcY == destY ) { printExistPath ( sx , sy , sx . length ) ; return true ; } if ( storePath ( ( 2 * srcX ) + srcY , srcY , destX , destY , sx , sy ) ) { return true ; } if ( storePath ( srcX , ( 2 * srcY ) + srcX , destX , destY , sx , sy ) ) { return true ; } sx . pop ( ) ; sy . pop ( ) ; return false ; } function isPathExist ( srcX , srcY , destX , destY ) { let sx = [ ] ; let sy = [ ] ; return storePath ( srcX , srcY , destX , destY , sx , sy ) ; } function printPath ( srcX , srcY , destX , destY ) { if ( ! isPathExist ( srcX , srcY , destX , destY ) ) { document . write ( " " ) ; } } let srcX = 5 , srcY = 8 ; let destX = 83 , destY = 21 ; printPath ( srcX , srcY , destX , destY ) ;