8000 调整代码 · HashCoding/boxbot@5bb89f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bb89f5

Browse files
committed
调整代码
1 parent 4cbcb2f commit 5bb89f5

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

boxbot-finder.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,62 +54,55 @@ BoxbotFinder.prototype.search = function (algorithm, from, to) {
5454
return this[algorithm.toLowerCase()](from, to)
5555
}
5656

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-
6757
/**
6858
* @param {[int]} distance 目标距离
69-
* @returns {[{weight: int, position: [int]}]}
59+
* @returns {[{weight: int, x: int, y: int}]}
7060
*/
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}
7464
].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]
7666
return item
7767
})
7868

79-
positions.sort(function (a, b) {
69+
offsets.sort(function (a, b) {
8070
return b.weight - a.weight
8171
})
8272

83-
return positions
73+
return offsets
8474
}
8575

8676
/**
8777
* 递归实现的深度优先搜索算法
8878
*
89-
* @param target
9079
* @param path
80+
* @param target
9181
* @param visited
9282
* @returns {[[int]]}
9383
*/
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+
}
9689

9790
var current = path[path.length - 1]
9891
if (current[0] == target[0] && current[1] == target[1]) {
9992
path.shift()
10093
return path
10194
}
10295

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]]
10699
var positionKey = next[0] + '-' + next[1]
107100

108101
if (this.isAvailable(next) && !visited[positionKey]) {
109102
path.push(next)
110103
visited[positionKey] = next
111104

112-
var result = this.deep_first_search(target, path, visited)
105+
var result = this.dfs(path, target, visited)
113106
if (result) {
114107
return result
115108
}
@@ -128,15 +121,20 @@ BoxbotFinder.prototype.deep_first_search = function (target, path, visited) {
128121
BoxbotFinder.prototype.bfs = function (from, to) {
129122
var offsets = [[0, 1], [-1, 0], [0, -1], [1, 0]];
130123
var queue = [new FinderNode(null, from)]
124+
131125
while (true) {
132126
var current = queue.shift()
127+
133128
if (current.position[0] == to[0] && current.position[1] == to[1]) {
134129
return current.getPath()
135130
}
131+
136132
for (var i = 0; i < offsets.length; i += 1) {
137133
var position = [current.position[0] + offsets[i][0], current.position[1] + offsets[i][1]]
134+
138135
if (this.isAvailable(position) && !current.inParents(position)) {
139136
var node = new FinderNode(current, position)
137+
140138
current.children.push(node)
141139
queue.push(node)
142140
}

0 commit comments

Comments
 (0)
0