PROGRAM Path_Counter
!-----------------------------------------------------------------------
! Program to count "northeasterly" paths from a given starting point
! in a network of streets to a specified ending point.
!   StartRow, StartColumn : coordinates of starting point
!   EndRow, EndColumn     : coordinates of ending point
!   Number_of_Paths       : recursive function to count paths
!
! Input:  StartRow, StartColumn, EndRow, EndColumn
! Output: Number of northeast paths
!-----------------------------------------------------------------------

  IMPLICIT NONE
  INTEGER :: StartRow, StartColumn, EndRow, EndColumn

  WRITE (*, '(1X, A)', ADVANCE = "NO") &
        "Enter the starting coordinates (row then column): "
  READ *, StartRow, StartColumn
  WRITE (*, '(1X, A)', ADVANCE = "NO") &
        "Enter the ending coordinates (row then column): "
  READ *, EndRow, EndColumn

  PRINT *, "There are", &
            Number_of_Paths(EndRow - StartRow, &
                            EndColumn - StartColumn),  " paths"

CONTAINS

  ! Number_of_Paths ----------------------------------------------------
  ! A recursive function to calculate the number of northeasterly paths
  ! in a network of streets. Identifiers used are:
  !   NumRows,  NumColumns : number of rows and columns from starting
  !                          postion to ending position
  !   Num_Paths            : number of paths
  !
  ! Accepts:  Number_of_Rows, Number_of_Columns
  ! Returns:  Num_Paths
  !---------------------------------------------------------------------
  
  RECURSIVE FUNCTION &
     Number_of_Paths(NumRows, NumColumns) RESULT (Num_Paths)
  
    INTEGER, INTENT(IN) :: NumRows, NumColumns
    INTEGER :: Num_Paths
  
    IF ((NumRows == 0) .OR. (NumColumns == 0)) THEN
       Num_Paths = 1
    ELSE
       Num_Paths = Number_of_Paths(NumRows - 1, NumColumns) &
                 + Number_of_Paths(NumRows, NumColumns - 1)
    END IF
  
  END FUNCTION Number_of_Paths

END PROGRAM Path_Counter
