-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_short_path_depth_island.php
101 lines (89 loc) · 2.07 KB
/
maze_short_path_depth_island.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
//深度搜索染色岛屿,标记相邻相关属性
$map = [
[1, 2, 1, 0, 0, 0, 0, 0, 2, 3],
[3, 0, 2, 0, 1, 2, 1, 0, 1, 2],
[4, 0, 1, 0, 1, 2, 3, 2, 0, 1],
[3, 2, 0, 0, 0, 1, 2, 4, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 5, 3, 0],
[0, 1, 2, 1, 0, 1, 5, 4, 3, 0],
[0, 1, 2, 3, 1, 3, 6, 2, 1, 0],
[0, 0, 3, 4, 8, 9, 7, 5, 0, 0],
[0, 0, 0, 3, 7, 8, 6, 0, 1, 2],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
];
function showMap() {
global $map;
for ($i = 0; $i < count($map); $i++) {
for ($j = 0; $j < count($map[0]); $j++) {
echo $map[$i][$j] . "\t";
}
echo "\n";
}
}
//顺时针 右、下、左、上
$next = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
$book;
$min = 9999;
class Node {
public $x, $y;
public $color;
public function Node($x = 0, $y = 0, $color = 0) {
$this->x = $x;
$this->y = $y;
$this->color = $color;
}
}
class Queue {
public $head = 0;
public $tail = 0;
public $data = null;
public function in($x, $y, $color) {
$this->data[$this->tail] = new Node($x, $y, $color);
$this->data[$this->tail]->f = $this->head;
$this->tail++;
}
public function out() {
$this->head++;
}
}
$queue = new Queue();
$color = 0;
for ($x = 0; $x < count($map); $x++) {
for ($y = 0; $y < count($map[0]); $y++) {
if ($map[$x][$y] > 0) {
$color--;
$book[$x][$y] = 1;
dfs($x, $y, $color);
}
}
}
function dfs($x, $y, $color) {
global $map, $next;
if ($map[$x][$y] <= 0) {
return;
}
$map[$x][$y] = $color;
for ($i = 0; $i < 4; $i++) {
$nextX = $x + $next[$i][0];
$nextY = $y + $next[$i][1];
if ($nextX > 9 || $nextX < 0 || $nextY > 9 || $nextY < 0) {
continue;
}
if (
!isset($book[$nextX][$nextY])
||
$book[$nextX][$nextY] == 0
) {
$book[$nextX][$nextY] = 1;
dfs($nextX, $nextY, $color);
}
}
}
showMap();
echo -$color;