# [PHP], <del>385</del> <del>335</del> <del>307</del> 286 bytes

or ***265 bytes*** [if output is numeric][1] `22311322` instead of `DDRUURDD`.

<!-- language-all: lang-php -->

    function c(&$m,$x,$r=-1,$l=0,$d=0){$v=[L,U,D,R];if($r<0)$r=array_search(P,$m);$m[$l=$r]=V;if(!in_array(T,$m))die;foreach([$r%$x?$r-1:0,$m[$r-$x]?$r-$x:0,$m[$r+$x]?$r+$x:0,$r%$x<$x-1?$r+1:0]as$o=>$n)if($n&&$m[$n]!=M&&$m[$n]!=V){echo$v[$o];c($m,$x,$n,$r,$o);}if($l)echo$v[[3,2,1,0][$d]];}



Uses a recursive depth-first search to crawl the map until all treasures are found.

Input is a one-dimensional array map, and number of columns `s( $m, 13 );`, output list to `STDOUT`.

**Ungolfed**

This could stand a bit more golfing (and possibly a bit more cleverness).

    function c( &$m, $x, $r=-1, $l=0, $d=0 ) {

        // table of direction names
        $v = [ 'L', 'U', 'D', 'R' ];
    
    	// locate starting position on first move
    	if( $r < 0 )						
    		$r = array_search( 'P', $m );

    	// move to new sqaure
    	$l = $r;
    
    	// mark square with . to show it has been visited
    	$m[ $r ] = '.';
    
    	// end if no more treasures
    	if ( ! in_array( 'T', $m ) )
    		exit;
    
    	// generate list of adjacent squares
    	$dirs = [
    		$r % $x ? $r-1 : false,              // Left
    		$m[ $r - $x ] ? $r - $x : false,     // Up
    		$m[ $r + $x ] ? $r + $x : false,     // Down
    		$r % $x < $x-1 ? $r+1 : false        // Right
    	];

    	// consider valid directions for next move
    	foreach ( $dirs as $o => $n )
    		if ( $n && $m[$n] != 'M' && $m[$n] != '.' ) {
    			// if not a Mountain and not Visited display the direction
    			echo $v[$o];
    			// and recursively keep crawling
    			c( $m, $x, $n, $r, $o );
    		}
    
    	// if this is a dead end, go back to previous square
    	if ( $l )
    		echo $v[[3,2,1,0][ $d ]];	// display the reverse direction
    
    }

[Basic Case][TIO-jtg7wf2u]

[PHP]: https://php.net/
[TIO-jtg7wf2u]: https://tio.run/##rZJRa8IwEMef56eIcKsNXqGZb4uZL3ucIKPzJQQpta4Fm5TTSYf42V2iDve6zYfjwj//3@WOS1u1x/GkrVpWEjlaUNk62tb2PU65PK4@bLGtnWVFHEGD0CGQSgTCWqUIS5XyPeyUfsE3fMZXI@tVDDROuXflRPnnYlPmVFTxDKHhEhrtQSCj5sHZr@3i5IqzcM2XdSlXjsrcAxroHroJUCIe/UsepAQ6Mzmlb2V4VoZnJQBj6BIRJE@ZfANOPYHloSsbRYGxpq@m1@Oc78uicrDT4Iws4suM1ldDcFweArrmF48e4QMKTI2GpTHycIRGMMV0724wG@CA/S48xf5FZX@ipj8iu0mH2U3mMrLXCwsQyMTIf70v "PHP – Try It Online"


`DDDDDRUUUUURDDRUURDDRUURDDDLDLLLDRRRRURUUUURDDDDDLRRU`

[Locked Up][2]

`DDRUURDD`

[No Treasure:][3]

` `  &nbsp;(no output)

[Back Alley][4]

`DDDDDRUUUUURDDDDDRUUUUURDDDDDRUUUUURDDDDDRUUUUURRRRRLLLLLDDRRRRRLLLLLDDRRRRR`

[No way to move][5]

` `&nbsp; (no output)

[1x1][6]

` `&nbsp; (no output)

[Hedge Maze][7] (an original creation)

    +-+-+-+-+-+-+-+-+-+-+-+-+-+
    |P|M| |T|M| | | |M| |M|M|T|
    +-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |M| |M| | |M|T|M|T| | | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+
    |T| | |M| |M| |T|M|M|M| |M|
    +-+-+-+-+-+-+-+-+-+-+-+-+-+
    |M|M| | | |M| |M|M| | | |M|
    +-+-+-+-+-+-+-+-+-+-+-+-+-+
    |T| | |M| |M| | | | |M| |M|
    +-+-+-+-+-+-+-+-+-+-+-+-+-+
    |M|M| |T| | |M|M|M|M|M| |T|
    +-+-+-+-+-+-+-+-+-+-+-+-+-+

`DDRRUURLDDDDLLRRDRRUULRUURURRDDLDDRRRURRUULLUDRRRUDLDDDDR`


  [1]: https://tio.run/##rZLRaoMwFIav51Ok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