move Subroutine

recursive subroutine move(i, j, direction, distance, save_path)

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: i
integer, intent(in) :: j
integer, intent(in) :: direction
integer, intent(inout), dimension(:,:) :: distance
logical, intent(in) :: save_path

to save the path coordinates


Calls

proc~~move~~CallsGraph proc~move problem_10::move proc~move->proc~move proc~pipe_info problem_10::pipe_info proc~move->proc~pipe_info

Called by

proc~~move~~CalledByGraph proc~move problem_10::move proc~move->proc~move program~problem_10 problem_10 program~problem_10->proc~move

Source Code

    recursive subroutine move(i,j,direction,distance,save_path)
        integer,intent(in) :: i,j,direction
        integer,dimension(:,:),intent(inout) :: distance
        logical,intent(in) :: save_path !! to save the path coordinates

        integer :: inew, jnew, imove
        logical :: valid_move

        select case(direction)
        case(1); inew = i-1; jnew = j   ! north
        case(2); inew = i+1; jnew = j   ! south
        case(3); inew = i;   jnew = j+1 ! east
        case(4); inew = i;   jnew = j-1 ! west
        end select
        if (visited(inew,jnew) .or. array(inew,jnew)=='.') return

        ! can we move in this direction?
        valid_move = .false.
        associate (current_pipe     => array(i,j),      &
                   current_distance => distance(i,j),   &
                   move_to          => array(inew,jnew) )
            select case (current_pipe)
            case('S')
                ! don't know what the first pip is, so have to try them all
                select case(direction)
                case(1); valid_move = index(pipe_info(move_to),'S')>0  ! north
                case(2); valid_move = index(pipe_info(move_to),'N')>0  ! south
                case(3); valid_move = index(pipe_info(move_to),'W')>0 ! east
                case(4); valid_move = index(pipe_info(move_to),'E')>0 ! west
                end select
            case('|')
                select case(direction)
                case(1); valid_move = index(pipe_info(move_to),'S')>0  ! north
                case(2); valid_move = index(pipe_info(move_to),'N')>0  ! south
                end select
            case('-')
                select case(direction)
                case(3); valid_move = index(pipe_info(move_to),'W')>0 ! east
                case(4); valid_move = index(pipe_info(move_to),'E')>0 ! west
                end select
            case('L')
                select case(direction)
                case(1);  valid_move = index(pipe_info(move_to),'S')>0 ! north
                case(3);  valid_move = index(pipe_info(move_to),'W')>0 ! east
                end select
            case('J')
                select case(direction)
                case(1);  valid_move = index(pipe_info(move_to),'S')>0 ! north
                case(4);  valid_move = index(pipe_info(move_to),'E')>0! west
                end select
            case('7')
                select case(direction)
                case(2);  valid_move = index(pipe_info(move_to),'N')>0 ! south
                case(4);  valid_move = index(pipe_info(move_to),'E')>0 ! west
                end select
            case('F')
                select case(direction)
                case(2);  valid_move = index(pipe_info(move_to),'N')>0 ! south
                case(3);  valid_move = index(pipe_info(move_to),'W')>0 ! east
                end select
            end select

            if (valid_move) then
                distance(inew,jnew) = current_distance + 1
                visited(inew,jnew) = .true.
                do imove = 1, 4
                    call move(inew,jnew,imove,distance,save_path)
                end do
                if (save_path) then
                    x = [x, real(inew,wp)] ! save cordinates of point on the path
                    y = [y, real(jnew,wp)]
                end if
            end if

        end associate

    end subroutine move