【发布时间】:2021-01-16 17:33:09
【问题描述】:
问题描述
我正在尝试获得一种算法,该算法将找到骑士可以在棋盘中移动并访问所有方格而不重复任何方格的可能移动序列的路径。如下图所示,这是可能的
我的方法
为了尝试实现这一点,我已按照以下步骤操作
- 用
allSquares['a1', 'a2', 'a3', ..., 'h7', 'h8']创建了一个数组 - 创建了另一个
visitedSquares数组。最初这是空的[] - 为每个正方形创建了一个
paths数组的函数。这表示骑士可以从其他方格移动到的方格
{
"a8": ["c7", "b6"],
"a7": ["c6", "b5","c8"],
"a6": ["c5","b4","c7","b8"],
...
"h3": ["f2","g1","f4","g5"],
"h2": ["f1","f3","g4"],
"h1": ["f2","g3"]
}
- 创建了一个
getNextNode()函数来返回访问节点的最大成本 - 最后我尝试通过以下步骤解决最长路径
while (this.squaresNotVisited.length > 0) {
const nextTerm = this.getNextNode();
const currentCost = this.pathCosts[nextTerm[0]];
const nextPaths: string[] = this.paths[nextTerm[0]];
nextPaths.forEach(square => {
if (this.pathCosts[square] < currentCost + 1) {
this.pathCosts[square] = currentCost + 1;
}
});
this.squaresVisited = [...this.squaresVisited, nextTerm[0]];
this.squaresNotVisited = this.allSquares.filter(
x => !this.squaresVisited.includes(x)
);
}
下面是完整的 Javascript 代码
class AppComponent {
board = {
columns: ["a", "b", "c", "d", "e", "f", "g", "h"],
rows: [8, 7, 6, 5, 4, 3, 2, 1]
};
allSquares = this.board.columns.reduce(
(prev, next) => [...prev, ...this.board.rows.map(x => next + x)],
[]
);
currentKnightPosition = "h5";
squaresVisited = [];
squaresNotVisited = [...this.allSquares];
nextPossibleKnightPosition = currentKnightPosition => {
const row = this.board.columns.indexOf(currentKnightPosition[0]);
const column = Number(currentKnightPosition[1]) - 1;
return [
[column - 1, row - 2],
[column - 1, row + 2],
[column - 2, row - 1],
[column - 2, row + 1],
[column + 1, row - 2],
[column + 1, row + 2],
[column + 2, row - 1],
[column + 2, row + 1]
]
.filter(
([row, column]) => column >= 0 && column < 8 && row >= 0 && row < 8
)
.map(
([row, column]) =>
this.board.columns[column] + this.board.rows[8 - row - 1]
);
};
paths = this.allSquares.reduce(
(prev, next) => ({
...prev,
[next]: this.nextPossibleKnightPosition(next)
}),
{}
);
isNextSquare = square =>
this.nextPossibleKnightPosition(this.currentKnightPosition).includes(
square
);
costs = { [this.currentKnightPosition]: 0 };
pathCosts = {
...this.allSquares.reduce(
(prev, next) => ({ ...prev, [next]: -Infinity }),
{}
),
[this.currentKnightPosition]: 0
};
getNextTerm = () => {
let nonVisted = Object.entries(this.pathCosts).filter(
([x, y]) => !this.squaresVisited.includes(x)
);
const maxPath = Math.max(...Object.values(nonVisted.map(([, x]) => x)));
return nonVisted.find(([, x]) => x === maxPath);
};
costsCalc = () => {
while (this.squaresNotVisited.length > 0) {
const nextTerm = this.getNextTerm();
const currentCost = this.pathCosts[nextTerm[0]];
const nextPaths = this.paths[nextTerm[0]];
nextPaths.forEach(square => {
if (this.pathCosts[square] < currentCost + 1) {
this.pathCosts[square] = currentCost + 1;
}
});
this.squaresVisited = [...this.squaresVisited, nextTerm[0]];
this.squaresNotVisited = this.allSquares.filter(
x => !this.squaresVisited.includes(x)
);
}
};
ngOnInit() {
this.costsCalc();
console.log(Math.max(...Object.values(this.pathCosts)))
}
}
const app = new AppComponent();
app.ngOnInit()
问题
该方法返回最长路径是 51 ......这是不正确的,因为正方形的数量是 64。我被困在错误是在我的代码中还是错误是在我使用的方法中。下面也是demo on stackblitz
【问题讨论】:
-
这篇维基百科文章提到了一个简单的启发式(Warndorff 规则):en.wikipedia.org/wiki/Knight%27s_tour#Warnsdorff's_rule。如果您的棋盘尺寸是 8x8,那么您总能找到解决方案,我不确定更大的棋盘。您还可以查看此站点以获取更多伪代码:bradfieldcs.com/algos/graphs/knights-tour
-
如果我这样做
console.log(this.pathCosts)然后我看到没有 1,2,3 等和其他多个,对吗? -
@Surt 路径是一个对象{ "a8": [ "c7", "b6" ], "a7": [ "c6", "b5", "c8" ], "a6" : [ "c5", "b4", "c7", "b8" ],
-
如果我在方格 a8,那么可用路径是 c7 和 b6,与 a7 相同。路径将是 c6、b5、c8...
标签: javascript algorithm