@@ -54,62 +54,55 @@ BoxbotFinder.prototype.search = function (algorithm, from, to) {
54
54
return this [ algorithm . toLowerCase ( ) ] ( from , to )
55
55
}
56
56
57
- /**
58
- * 深度优先搜索
59
- *
60
- * @param {[int] } from
61
- * @param {[int] } to
62
- */
63
- BoxbotFinder . prototype . dfs = function ( from , to ) {
64
- return this . deep_first_search ( to , [ from ] )
65
- }
66
-
67
57
/**
68
58
* @param {[int] } distance 目标距离
69
- * @returns {[{weight: int, position: [ int] }] }
59
+ * @returns {[{weight: int, x: int, y: int }] }
70
60
*/
71
- BoxbotFinder . prototype . createPositions = function ( distance ) {
72
- var positions = [
73
- { position : [ 0 , 1 ] } , { position : [ - 1 , 0 ] } , { position : [ 0 , - 1 ] } , { position : [ 1 , 0 ] }
61
+ BoxbotFinder . prototype . createOffsets = function ( distance ) {
62
+ var offsets = [
63
+ { x : 0 , y : 1 } , { x : - 1 , y : 0 } , { x : 0 , y : - 1 } , { x : 1 , y : 0 }
74
64
] . map ( function ( item ) {
75
- item . weight = item . position [ 0 ] * distance [ 0 ] + item . position [ 1 ] * distance [ 1 ]
65
+ item . weight = item . x * distance [ 0 ] + item . y * distance [ 1 ]
76
66
return item
77
67
} )
78
68
79
- positions . sort ( function ( a , b ) {
69
+ offsets . sort ( function ( a , b ) {
80
70
return b . weight - a . weight
81
71
} )
82
72
83
- return positions
73
+ return offsets
84
74
}
85
75
86
76
/**
87
77
* 递归实现的深度优先搜索算法
88
78
*
89
- * @param target
90
79
* @param path
80
+ * @param target
91
81
* @param visited
92
82
* @returns {[[int]] }
93
83
*/
94
- BoxbotFinder . prototype . deep_first_search = function ( target , path , visited ) {
95
- visited = visited || { }
84
+ BoxbotFinder . prototype . dfs = function ( path , target , visited ) {
85
+ if ( ! visited ) {
86
+ visited = { }
87
+ path = [ path ]
88
+ }
96
89
97
90
var current = path [ path . length - 1 ]
98
91
if ( current [ 0 ] == target [ 0 ] && current [ 1 ] == target [ 1 ] ) {
99
92
path . shift ( )
100
93
return path
101
94
}
102
95
103
- var positions = this . createPositions ( [ target [ 0 ] - current [ 0 ] , target [ 1 ] - current [ 1 ] ] )
104
- for ( var i = 0 ; i < positions . length ; i += 1 ) {
105
- var next = [ positions [ i ] . position [ 0 ] + current [ 0 ] , positions [ i ] . position [ 1 ] + current [ 1 ] ]
96
+ var offsets = this . createOffsets ( [ target [ 0 ] - current [ 0 ] , target [ 1 ] - current [ 1 ] ] )
97
+ for ( var i = 0 ; i < offsets . length ; i += 1 ) {
98
+ var next = [ offsets [ i ] . x + current [ 0 ] , offsets [ i ] . y + current [ 1 ] ]
106
99
var positionKey = next [ 0 ] + '-' + next [ 1 ]
107
100
108
101
if ( this . isAvailable ( next ) && ! visited [ positionKey ] ) {
109
102
path . push ( next )
110
103
visited [ positionKey ] = next
111
104
112
- var result = this . deep_first_search ( target , path , visited )
105
+ var result = this . dfs ( path , target , visited )
113
106
if ( result ) {
114
107
return result
115
108
}
@@ -128,15 +121,20 @@ BoxbotFinder.prototype.deep_first_search = function (target, path, visited) {
128
121
BoxbotFinder . prototype . bfs = function ( from , to ) {
129
122
var offsets = [ [ 0 , 1 ] , [ - 1 , 0 ] , [ 0 , - 1 ] , [ 1 , 0 ] ] ;
130
123
var queue = [ new FinderNode ( null , from ) ]
124
+
131
125
while ( true ) {
132
126
var current = queue . shift ( )
127
+
133
128
if ( current . position [ 0 ] == to [ 0 ] && current . position [ 1 ] == to [ 1 ] ) {
134
129
return current . getPath ( )
135
130
}
131
+
136
132
for ( var i = 0 ; i < offsets . length ; i += 1 ) {
137
133
var position = [ current . position [ 0 ] + offsets [ i ] [ 0 ] , current . position [ 1 ] + offsets [ i ] [ 1 ] ]
134
+
138
135
if ( this . isAvailable ( position ) && ! current . inParents ( position ) ) {
139
136
var node = new FinderNode ( current , position )
137
+
140
138
current . children . push ( node )
141
139
queue . push ( node )
142
140
}
0 commit comments