Advertisement
Guest User

CPM PHP 2

a guest
Dec 15th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. <?php
  2.  
  3. class cpm {
  4.  
  5. protected $start = 1;
  6. protected $end = [8];
  7.  
  8. protected $possiblePaths = [];
  9.  
  10. protected $map = [
  11. [1, 2, 6],
  12. [1, 3, 10],
  13. [2, 3, 6],
  14. [2, 5, 12],
  15. [3, 4, 5],
  16. [3, 5, 8],
  17. [4, 6, 8],
  18. [5, 6, 7],
  19. [5, 7, 8],
  20. [6, 7, 6],
  21. [7, 8, 7],
  22. ];
  23.  
  24. public function countPosiblePaths() {
  25. $this->getPosiblePaths($this->start);
  26. $this->printPosiblePaths();
  27. }
  28.  
  29. protected function printPosiblePaths() {
  30. arsort($this->possiblePaths);
  31.  
  32. foreach($this->possiblePaths as $path => $cost) {
  33. $this->printPosiblePath($path , $cost);
  34. }
  35. }
  36.  
  37. protected function printPosiblePath($path , $cost) {
  38. echo sprintf("%s = %s\n", implode(' -> ', str_split($path)) , $cost);
  39.  
  40. }
  41.  
  42.  
  43. protected function getPosiblePaths($current, $path = '', $sum = 0) {
  44.  
  45. $path .= $current;
  46.  
  47.  
  48. if (in_array($current, $this->end)) {
  49. $this->possiblePaths[$path] = $sum;
  50. return;
  51. }
  52.  
  53. foreach($this->map as $stepMap) {
  54. if ($stepMap[0] == $current) {
  55. $this->getPosiblePaths($stepMap[1], $path, $sum + $stepMap[2]);
  56. }
  57. }
  58. }
  59.  
  60. }
  61.  
  62.  
  63. $cpm = new cpm();
  64. $cpm->countPosiblePaths();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement