【发布时间】:2015-10-24 00:05:47
【问题描述】:
我想找到从一点到另一点的路径。黑盒子是障碍物,我们不能去它,蓝色盒子是起点,黄色盒子是终点,我想从起点到终点。我在书中的算法的帮助下编写了这段代码,实际上现在我想让它动态化,这意味着对于 n x n 矩阵。所以我想要指导我应该采取哪些步骤才能使其能够在 n x n 矩阵上运行。而且我还想问这是在这种情况下找到最短路径的最佳解决方案还是其他什么?
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
boolean[][] boolMaze = new boolean[][] {
{ true, true, true, true, true },
{ false, true, false, true, true },
{ true, false, true, true, true },
{ false, true, true, false, false },
{ false, false, false, false, false },
{ true, true, true, true, true },
{ false, true, false, true, true },
{ true, false, true, true, true },
{ false, true, true, false, false },
{ false, false, false, false, false },
{ true, true, true, true, true },
{ false, true, false, true, true },
{ true, false, true, true, true },
{ false, true, true, false, false },
{ false, false, false, false, false },
{ true, true, true, true, true },
{ false, true, false, true, true },
{ true, false, true, true, true },
{ false, true, true, false, false },
{ false, false, false, false, false } };
Maze maze = new Maze(boolMaze);
List<Maze.Cell> path = maze.findPath(0, 0, 3, 2);
System.out.println("found path in maze: " + path);
}
}
class Maze {
private final boolean[][] maze;
public Maze(boolean[][] maze) {
this.maze = maze;
}
public int height() {
return maze.length;
}
public int width() {
return maze[0].length;
}
}
public List<Cell> findPath(int xStart, int yStart, int xGoal, int yGoal) {
LinkedList<Cell> path = new LinkedList(); // use a linked list, since we
// don't know how many
// elements it will contain
// straight away..
path.add(new Cell(xStart, yStart));
HashSet<Cell> visited = new HashSet(); // this set will store all
// visited cells. Make sure to
// add any cell you looked at.
// Don't work with cells which
// you have visited previously,
// by checking
// visited.contains(cell).
visited.add(path.getFirst());
// ///////////////////////////
if (xStart - 1 >= 0 && maze[xStart][yStart - 1]) {
Cell cell = new Cell(xStart, yStart - 1);
return path;
}
else if (yStart + 1 < height() && maze[xStart][yStart + 1]) {
Cell cell = new Cell(xStart, yStart + 1);
return path;
}
else if (xStart + 1 < width() && maze[xStart + 1][yStart]) {
Cell cell = new Cell(xStart + 1, yStart);
return path;
}
else if (xStart - 1 >= 0 && maze[xStart - 1][yStart]) {
Cell cell = new Cell(xStart - 1, yStart);
return path;
}
// //////////////////////////
// use your path finding algorithm here (note that you can use getLast()
// and getFirst() on your list.
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Cell))
return false;
Cell cell = (Cell) o;
if (x != cell.x)
return false;
return y == cell.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
class Cell implements Comparable<Cell> {
Cell(int x, int y) {
this.x = x;
this.y = y;
}
int x;
int y;
@Override
public int compareTo(Cell o) {
if (o.equals(x) && o.equals(y))
return 0;
return 1;
}
}
【问题讨论】:
-
这听起来很像家庭作业问题;我可能是错的。我知道您想要代码作为答案,但请向我们展示您的尝试。您确实说过您遇到了困难,所以请告诉我们您遇到的问题。
-
不要因为所有的反对票而气馁。只需按照@Venovani 的建议进行操作,编辑您的问题,我们将能够为您提供帮助
-
我付出了我的努力作为代码。顺便说一句,这不是我的作业。这是我最后一年项目的一部分。我想制作一个室内导航应用程序,我完成了桌面管理面板和应用程序 UI,但现在我被困在这里如何通过实现哪种算法来实际找到路径
-
删除你的答案,把代码放到它所属的问题中!
-
抱歉,我是堆栈溢出的新手。感谢您的真诚建议,我正在一次次收到东西。
标签: java path-finding