Jump to content

Backpack problem


writer

Recommended Posts

  • 2 weeks later...
<?php
define ("N", 3);                       // define constant N

$item = array (                                             // define the items
               array ('size' => 2, 'val' => 6),
               array ('size' => 4, 'val' => 2),
               array ('size' => 3, 'val' => 4)
              );

function knack($cap, $start)                         
{ 
global $item;
    
    for($i=$start, $max=0; $i<N; $i++) 
{ 
	$space=$cap-$item[$i]['size'];
	if($space >= 0) 
	{ 
		$t = knack($space, $i+1)+$item[$i]['val']; 
		if($t>$max) 
		{
			$max=$t; 
		}
	}
} 
return $max; 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/99495-backpack-problem/#findComment-522992
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.