All of the current answers seem to neglect input checking, as some are susceptible to infinite loops.
To prevent this, do some error checking before you attempt to process the input. Here are the two versions I could come up with, the first uses a straightforward loop implementation, the second uses math.
Note: There may still be bugs, I haven't tested them 100%.
function escape( $max_height, $slip, $wall_height, $num_walls)
{
// Check for infinite loop
if( ($max_height - $slip) <= 0)
{
// Return a value to let caller know an error has occurred
return -1;
}
$jumps = 0;
while( $num_walls > 0)
{
$current_height = 0;
do
{
$current_height += $max_height;
$jumps++;
} while( ($wall_height - $current_height) >= 0);
$num_walls--;
}
$jumps = ($wall_height % $max_height == 0) ? ($jumps - 1) : $jumps;
return $jumps;
}
And now, without loops:
function escape2( $max_height, $slip, $wall_height, $num_walls)
{
// Check for valid input
if( ($max_height - $slip) <= 0)
{
return -1;
}
if( $wall_height <= 0)
{
return 0;
}
$jumps = ceil( abs( $wall_height - ($max_height - $slip)));
return ($jumps == 0) ? $num_walls : ($jumps * $num_walls);
}
Test them out, send me some bug reports. :)