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
def printExistPath ( sx , sy , last ) : NEW_LINE INDENT if ( len ( sx ) == 0 or len ( sy ) == 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT x = sx [ - 1 ] ; NEW_LINE y = sy [ - 1 ] NEW_LINE sx . pop ( ) ; NEW_LINE sy . pop ( ) ; NEW_LINE printExistPath ( sx , sy , last ) ; NEW_LINE if ( len ( sx ) == last - 1 ) : NEW_LINE INDENT print ( " ( " + str ( x ) + " , ▁ " + str ( y ) + " ) " , end = ' ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ( " + str ( x ) + " , ▁ " + str ( y ) + " ) ▁ - > ▁ " , end = ' ' ) NEW_LINE DEDENT DEDENT def storePath ( srcX , srcY , destX , destY , sx , sy ) : NEW_LINE INDENT if ( srcX > destX or srcY > destY ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT sx . append ( srcX ) ; NEW_LINE sy . append ( srcY ) ; NEW_LINE if ( srcX == destX and srcY == destY ) : NEW_LINE INDENT printExistPath ( sx , sy , len ( sx ) ) ; NEW_LINE return True ; NEW_LINE DEDENT if ( storePath ( ( 2 * srcX ) + srcY , srcY , destX , destY , sx , sy ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT if ( storePath ( srcX , ( 2 * srcY ) + srcX , destX , destY , sx , sy ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT sx . pop ( ) ; NEW_LINE sy . pop ( ) ; NEW_LINE return False ; NEW_LINE DEDENT def isPathExist ( srcX , srcY , destX , destY ) : NEW_LINE INDENT sx = [ ] NEW_LINE sy = [ ] NEW_LINE return storePath ( srcX , srcY , destX , destY , sx , sy ) ; NEW_LINE DEDENT def printPath ( srcX , srcY , destX , destY ) : NEW_LINE INDENT if ( not isPathExist ( srcX , srcY , destX , destY ) ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT srcX = 5 NEW_LINE srcY = 8 ; NEW_LINE destX = 83 NEW_LINE destY = 21 ; NEW_LINE printPath ( srcX , srcY , destX , destY ) ; NEW_LINE DEDENT