step Subroutine

recursive subroutine step(array)

take all valid steps from any # elements in array

Arguments

Type IntentOptional Attributes Name
character(len=1), intent(inout), dimension(:,:), allocatable :: array

Called by

proc~~step~~CalledByGraph proc~step problem_21::step program~problem_21 problem_21 program~problem_21->proc~step

Source Code

    recursive subroutine step(array)
        !! take all valid steps from any # elements in array
        character(len=1),dimension(:,:),intent(inout),allocatable :: array
        integer :: i,j
        logical :: steped
        character(len=1),dimension(:,:),allocatable :: tmp
        tmp = array
        do i = 1, nrows
            do j = 1, ncols
                if (array(i,j)=='O') then
                    steped = .false.
                    ! try a step in each direction:
                    if (array(i-1,j)=='.') then
                        tmp(i-1,j)='O'; steped = .true.
                    end if
                    if (array(i+1,j)=='.') then
                        tmp(i+1,j)='O'; steped = .true.
                    end if
                    if (array(i,j-1)=='.') then
                        tmp(i,j-1)='O'; steped = .true.
                    end if
                    if (array(i,j+1)=='.') then
                        tmp(i,j+1)='O'; steped = .true.
                    end if
                    if (steped) tmp(i,j) = '.'
                end if
            end do
        end do
        call move_alloc(tmp,array)
    end subroutine step