Skip to main content
12 of 20
fix all test cases
640KB
  • 11.9k
  • 2
  • 35
  • 57

PHP, 385 335 307 297 bytes

function c(&$m,$x,$r=-1,$l=-1,$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:-1,$m[$r-$x]?$r-$x:-1,$m[$r+$x]?$r+$x:-1,$r%$x<$x-1?$r+1:-1]as$o=>$n)if($n>=0&&$m[$n]!=M&&$m[$n]!=V){echo$v[$o];c($m,$x,$n,$r,$o);}if($l>=0)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=-1, $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 : -1,              // Left
        $m[ $r - $x ] ? $r - $x : -1,     // Up
        $m[ $r + $x ] ? $r + $x : -1,     // Down
        $r % $x < $x-1 ? $r+1 : -1        // Right
    ];

    // consider valid directions for next move
    foreach ( $dirs as $o => $n )
        // if not a Wall or Mountain and not Visited
        if ( $n>=0 && $m[$n] != 'M' && $m[$n] != '.' ) {
            // 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 >= 0 )
        echo $v[[3,2,1,0][ $d ]];   // display the reverse direction

}

Basic Case

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

DDDDDRUUUUURDDRUURDDRUURDDDLDLLLDRRRRURUUUURDDDDDLRRU

Locked Up

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

DDRUURDD

No Treasure:

 (no output)

Back Alley

DDDDDRUUUUURDDDDDRUUUUURDDDDDRUUUUURDDDDDRUUUUURRRRRLLLLLDDRRRRRLLLLLDDRRRRR

World of giant treasure

LLLLLLUURDRURDRURDRURDDDLLLLLLLDDRURDRURDRURDRURUUUURDDDDDLRRUUUUURDDDDDRUUUUU

No way to move

  (no output)

1x1

  (no output)

Hedge Maze (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

640KB
  • 11.9k
  • 2
  • 35
  • 57