Java最短路径
我正在尝试用 Java 制作一个小型塔防游戏。我有一个由 Point2D.Double
数组组成的网格,名为:
FieldArr[h][v]
h
表示水平字段,v
垂直垂直字段
这使得像这样的网格
+ + + + + + +
S + X + + + +
+ + + X + + +
+ X + + + + F
+ + X + + + +
S代表开始,F代表完成,X代表塔
现在我想计算最短路线,但我不知道如何开始这个
塔有以下位置变量: 水平编号和垂直编号。
对于绘画我这样做:
public void paint(Graphics2D g2) {
int Xpos = HorizontalNr * playfield.getSquarewidth() + playfield.GetinitialXPos();
int Ypos = VerticalNr * playfield.getSquarewidth() + playfield.GetinitialYPos();
g2.fillRect(Xpos, Ypos, 50, 50);
}
有人有任何建议,关于我应该如何制作我的敌人类别,这样我就不会遇到算法的任何问题吗? 和/或有关于如何计算最短路径的提示?
已经谢谢了 猕猴桃
I'm trying to make an small tower defender game in Java. I have a grid that is made up of a Point2D.Double
array named:
FieldArr[h][v]
h
represents horizontal fields, v
the vertical vertical fields
this makes a grid like this
+ + + + + + +
S + X + + + +
+ + + X + + +
+ X + + + + F
+ + X + + + +
S represents start, F represents Finish, X represents Towers
now I want to calculate the shortest route for but i don't have any clue how to start on this
Towers have the following vars for location:
HorizontalNr and VerticalNr.
for paint I do then:
public void paint(Graphics2D g2) {
int Xpos = HorizontalNr * playfield.getSquarewidth() + playfield.GetinitialXPos();
int Ypos = VerticalNr * playfield.getSquarewidth() + playfield.GetinitialYPos();
g2.fillRect(Xpos, Ypos, 50, 50);
}
Anyone have any tips, on how I should make my enemy class, so I won't get in any problem with the algorithm?
and/or have tips on how to calculate the shortest path?
already thanks
grt kiwi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最短路径问题已经被研究了很多,并且有很多关于这个问题的文献。最好只使用现有的算法,而不是尝试自己发明算法。
例如,一个简单而高效的算法是Dijkstra算法。来自维基百科:
The shortest path problem has been studied a lot and a lot of literature exists on this problem. It's probably best to just using an existing algorithm rather than trying to invent an algorithm yourself.
For example, a simple and efficient algorithm is Dijkstra's algorithm. From Wikipedia:
如果您想避开塔楼,那么您正在寻找从开始到结束的欧几里得路径,这可以通过最短路径方法 ala djkstra 来解决。
if you're looking to avoid the towers then your looking for the euclidean path from start to finish, which is solvable with a shortest path approach ala djkstra.
正如马克所说,这就是最短路径问题,可以轻松高效地解决。
请注意,由于您的问题没有加权,因此您可以在此处使用 BFS ,即非常容易实现,并且还保证找到未加权图的最佳路径。
BFS 的伪代码:
(*) 从地图中提取路径很简单:只需遵循
current <-parent.get(current)
直到得到null
,这提取您将使用的确切路径的方式。请注意,[在大多数情况下]更快的解决方案将是 A* 与 曼哈顿距离启发式,但实现起来要复杂得多。
As Mark said, this is Shortest Path Problem, which can be solved easily and efficiently.
Note, that since your problem is not weighted, you can use a BFS here, which is pretty easy to implement and also guarantees finding sortest path for unweighted graphs.
Pseudo code for BFS:
(*) Extracting the path from the map is simple: just follow the
current <- parent.get(current)
until you getnull
, this way you extract the exact path you will use.Note that even faster solution [in most cases] will be A* with the manhattan distance heuristic, but it is much more complex to implement it.