0

So, I've written a code to find all possible paths for M x N matrix

function printRobotPaths($path,$m,$n,$x,$y) {
        if (($x == $m-1) && ($y == $n-1)) {
            return $path;
        }
        if ($x == $m-1) {
            return printRobotPaths($path.'D',$m,$n,$x,$y+1);
        }
        if ($y == $n-1) {
            return printRobotPaths($path.'R',$m,$n,$x+1,$y);
        }
        printRobotPaths($path.'D',$m,$n,$x,$y+1);
        printRobotPaths($path.'R',$m,$n,$x+1,$y);
    }
$m = 3;
$n = 3;
$x = 0;
$y = 0;


$data = array(
    array(1,2,3),
    array(4,5,6),
    array(7,8,9)
);

$path = printRobotPaths('',$m,$n,0,0);
var_dump($path);

but the problem is when I'm returning $path then it var_dump() result is NULL but for same if I just print $path inside the function it will print correct answer

I wanted to return all possible 6 ways from it.

Mohit Bumb
  • 2,466
  • 5
  • 33
  • 52
  • 3
    `printRobotPaths(...)` doesn't return anything – B001ᛦ Aug 29 '19 at 14:07
  • if (($x == $m-1) && ($y == $n-1)) { return $path; } – Mohit Bumb Aug 29 '19 at 14:08
  • I'm confused what is supposed to be returned if none of the `if` statements are true. Which of the *two* recursive calls at the end do you expect to return…? – deceze Aug 29 '19 at 14:12
  • when I echo $path.' - ' in first if condition it prints : "DDRR - DRDR - DRRD - RDDR - RDRD - RRDD " I want to return this string but don't know how @deceze – Mohit Bumb Aug 29 '19 at 14:14

0 Answers0