- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请帮助我理解我的代码做错了什么。我试图使用 BFS 获得最短路径来解决问题,但它要么给我 -1 要么 2。它应该给我 6 作为答案。我究竟做错了什么?这就是问题所在:
给定一个棋盘,找到马从给定来源到达给定目的地所采取的最短距离(最小步数)。
例如,N = 8(8 x 8 板),源 = (7, 0) 目标 = (0, 7)
所需的最少步骤数为 6
我的代码如下:
class Point {
int x, y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
class knightShortestPath {
int N = 8;
public static boolean visited[][];
public boolean isPositionValid(int x, int y){
if( x < 0 || y < 0 || x > this.N || y > this.N){
return false;
}
return true;
}
public void createChessBoard(int N) {
this.N = N;
visited = new boolean[this.N][this.N];
for (int i = 0; i < this.N; i++) {
for (int j = 0; j < this.N; j++) {
visited[i][j] = false;
}
}
}
public int BFS(Point source, Point destination) {
int row[] = {2, 2, -2, -2, 1, 1, -1, -1};
int col[] = {1, -1, 1, -1, 2, -2, 2, -2};
Queue<Point> queue = new LinkedList<>();
queue.offer(source);
visited[source.x][source.y] = true;
int minimumNumSteps = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Point pt = queue.poll();
if (pt.x == destination.x && pt.y == destination.y) {
return minimumNumSteps;
}
for (int j = 0; j < size; j++) {
Point next = new Point(pt.x + row[i], pt.y + col[j]);
if (isPositionValid(pt.x + row[i], pt.y + col[j]) && !visited[i][j]) {
visited[i][j] = true;
queue.offer(next);
}
}
}
minimumNumSteps++;
}
return minimumNumSteps;
}
public static void main(String[] args) {
knightShortestPath position = new knightShortestPath();
position.createChessBoard(8);
Point src = new Point(0,7);
Point dest = new Point(7,0);
System.out.println("The minimum number of steps are: " + position.BFS(src, dest)); //answer is 6
}
}
最佳答案
第一件事:我不知道你怎么会得到负值。用 0 初始化后,您永远不会减少 minimumNumSteps
。可能会溢出吗?我觉得很奇怪..
除此之外,我还发现两个问题:
queue.size()
。相反,您想要做的是迭代当前节点的所有子节点。 所以:
while(!queue.isEmpty()) {
Point pt = queue.poll();
// check if it's target
// ...
for (int i = 0; i < row.length; i++) {
// ...
for (int j = 0; j < col.length; j++) {
// ...
}
}
}
另外注意:当队列为空并且你还没有达到目标时,没有解决办法。目前,您返回的一些值可能会被错误地解释。
关于java - 骑士的最短路径(BFS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52812615/
我尝试运行稍微修改过的 horsemanjs 的示例代码: var Horseman = require('node-horseman'); var horseman = new Horseman()
我在 Isometric camera with THREE.js 中找到了有关如何创建(轴测)等距相机的示例,但如何创建轴测斜线? 最佳答案 您可以渲染带有倾斜的场景 cabinet perspec
我正在尝试在 horseman 的 evalute 函数中使用 promises。一个简单的例子: var Horseman = require('node-horseman'); var horse
我试图在调试期间查看 List<> 的内容。不幸的是我看不到它们,因为我在变量窗口中收到以下消息: corvalue.GetExactTypeSafe(out type). The object is
Rider IDE 通知我以下内容效率低下 transform.Translate(moveDirection * speed * Time.smoothDeltaTime); 并想将
是否有一种简单的方法可以在 Visual Studio 之外使用旧的 EF 来搭建脚手架迁移?如果可能的话,我想通过 Rider IDE 来完成。 最佳答案 对于 EF Core,您可以使用 http
在 Rider 中,当在断点处暂停时,有没有办法在调试器堆栈帧中实际显示“外部代码”? 在 Visual Studio 中,这可以轻松完成,但在 Rider 中似乎不可能。而且,是的,我启用了“exa
我试图通过更改其环境变量来修改 Linux 上 JetBrains Rider 中 .NET 项目的设置配置文件。但是,当我点击文件夹图标时,我无法点击添加、删除或修改任何环境变量。 我注意到在 Ri
只是试用 Rider 并遇到了这个问题,如果我只是构建 Xamarin Android 项目 - 它构建得非常好,但是如果我尝试运行它,它会在部署步骤失败并出现以下错误: ▼ Project Not
当我在 Jetbrains Rider EAP 21 中创建解决方案时,我在解决方案资源管理器窗口中收到“(缺少包)”错误。 然后,我尝试构建项目并获得 [MSB4057] 错误。 尽管如此,我可以通
我是一名优秀的程序员,十分优秀!