Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class cpm {
- protected $start = 1;
- protected $end = [8];
- protected $possiblePaths = [];
- protected $map = [
- [1, 2, 6],
- [1, 3, 10],
- [2, 3, 6],
- [2, 5, 12],
- [3, 4, 5],
- [3, 5, 8],
- [4, 6, 8],
- [5, 6, 7],
- [5, 7, 8],
- [6, 7, 6],
- [7, 8, 7],
- ];
- public function countPosiblePaths() {
- $this->getPosiblePaths($this->start);
- $this->printPosiblePaths();
- }
- protected function printPosiblePaths() {
- arsort($this->possiblePaths);
- foreach($this->possiblePaths as $path => $cost) {
- $this->printPosiblePath($path , $cost);
- }
- }
- protected function printPosiblePath($path , $cost) {
- echo sprintf("%s = %s\n", implode(' -> ', str_split($path)) , $cost);
- }
- protected function getPosiblePaths($current, $path = '', $sum = 0) {
- $path .= $current;
- if (in_array($current, $this->end)) {
- $this->possiblePaths[$path] = $sum;
- return;
- }
- foreach($this->map as $stepMap) {
- if ($stepMap[0] == $current) {
- $this->getPosiblePaths($stepMap[1], $path, $sum + $stepMap[2]);
- }
- }
- }
- }
- $cpm = new cpm();
- $cpm->countPosiblePaths();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement