ClarkeRubber

UNSW ProgComp: Problem 5 - 2005

Jun 9th, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 KB | None | 0 0
  1. <?php
  2. $input = <<<END
  3. 5 6
  4. 1 2 3 2 1 0
  5. 2 3 4 6 10 4
  6. 3 5 7 8 9 6
  7. 2 3 4 7 8 7
  8. 1 2 3 6 2 1
  9. END;
  10. $input = trim($input);
  11. $input = explode("\n", $input);
  12. foreach($input as $key => $value){
  13.     $input[$key] = trim($value);
  14. }
  15. foreach($input as $key => $value){
  16.     if($key == 0){
  17.         $table_info = explode(" ", $value);
  18.     }else{
  19.         $table[$key-1] = explode(" ", $value);
  20.     }
  21. }
  22. $greatest = array(0, 0, 0);
  23. for($y = 0; $y < $table_info[0]; $y++){
  24.     $incrementing = 1;
  25.     $last = null;
  26.     $has_incremented = 0;
  27.     for($x = 0; $x < $table_info[1]; $x++){
  28.         if($incrementing == 1 && $last < $table[$y][$x] && $x != ($table_info[1]-1)){
  29.             $last = $table[$y][$x];
  30.             $has_incremented = 1;
  31.             if($last > $greatest[0]){
  32.                 $greatest = array($last, $x, $y);
  33.             }
  34.         }elseif($has_incremented == 0){
  35.             $out = $y+1;
  36.             die("row $out is not a hill vector");
  37.         }elseif($last == $table[$y][$x]){
  38.             $out = $y+1;
  39.             die("row $out is not a hill vector");
  40.         }else{
  41.             $incrementing = 0;
  42.         }
  43.         if($incrementing == 0 && $last > $table[$y][$x]){
  44.             $last = $table[$y][$x];
  45.         }elseif($incrementing == 0){
  46.             $out = $y+1;
  47.             die("row $y is not a hill vector");
  48.         }
  49.     }
  50. }
  51. for($x = 0; $x < $table_info[1]; $x++){
  52.     $incrementing = 1;
  53.     $last = null;
  54.     $has_incremented = 0;
  55.     for($y = 0; $y < $table_info[0]; $y++){
  56.         if($incrementing == 1 && $last < $table[$y][$x] && $y != ($table_info[0])){
  57.             $last = $table[$y][$x];
  58.             $has_incremented = 1;
  59.             if($last > $greatest[0]){
  60.                 $greatest = array($last, $x, $y);
  61.             }
  62.         }elseif($has_incremented == 0){
  63.             $out = $x+1;
  64.             die("column $out is not a hill vector");
  65.         }elseif($last == $table[$y][$x]){
  66.             $out = $x+1;
  67.             die("column $out is not a hill vector");
  68.         }else{
  69.             $incrementing = 0;
  70.         }
  71.         if($incrementing == 0 && $last > $table[$y][$x]){
  72.             $last = $table[$y][$x];
  73.         }elseif($incrementing == 0){
  74.             $out = $x+1;
  75.             die("column $out is not a hill vector");
  76.         }
  77.     }
  78. }
  79. $column = $greatest[1]+1;
  80. $row = $greatest[2]+1;
  81. echo "peak found at row $row column $column";
Advertisement
Add Comment
Please, Sign In to add comment