-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart01.php
37 lines (33 loc) · 1.1 KB
/
part01.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
global $data;
$costs = array_fill(0, count($data), array_fill(0, count($data[0]), PHP_INT_MAX));
$costs[0][0] = 0;
$changed = true;
while ($changed) {
$changed = false;
foreach ($costs as $y => &$row) {
foreach ($row as $x => &$ele) {
$base = $data[$x][$y];
if ($y > 0 && $costs[$y-1][$x] + $base < $ele) {
$changed = true;
$ele = $costs[$y-1][$x] + $base;
}
if ($y < count($costs) - 1 && $costs[$y+1][$x] + $base < $ele) {
$changed = true;
$ele = $costs[$y+1][$x] + $base;
}
if ($x > 0 && $costs[$y][$x-1] + $base < $ele) {
$changed = true;
$ele = $costs[$y][$x-1] + $base;
}
if ($x < count($row) - 1 && $costs[$y][$x+1] + $base < $ele) {
$changed = true;
$ele = $costs[$y][$x+1] + $base;
}
}
}
}
$cost = $costs[count($costs)-1][count($costs[0])-1];
?>
The cost is <?php echo $cost; ?>.
<?php /* vim: set expandtab tabstop=4 smarttab shiftwidth=4: */ ?>