Java 递归算法问题
我有一个街道网络,每条街道可以分叉成两条街道,所以同样地,两条街道可以汇集成一条街道。 每次发生收敛或发散时,都存在一个节点。然后是工作区跨度,放置在街道顶部 (因此工作区跨度可以悬停在多条街道上)。现在的想法是,我必须基本上扫描并找到工作区的起点, 并在通往工作区实际起点的可穿越路径上放置(至少)5个警告标志。每个警告标志必须相隔500英尺, 因此,总体而言,如果你正在接近一个工作区,你应该看到第一个警告标志,因为你离它2500英尺远,然后在2000英尺处看到另一个警告标志, 还有一个是1500等等 因此,总体而言,所有这些实体都作为对象存在:Java 递归算法问题,java,recursion,Java,Recursion,我有一个街道网络,每条街道可以分叉成两条街道,所以同样地,两条街道可以汇集成一条街道。 每次发生收敛或发散时,都存在一个节点。然后是工作区跨度,放置在街道顶部 (因此工作区跨度可以悬停在多条街道上)。现在的想法是,我必须基本上扫描并找到工作区的起点, 并在通往工作区实际起点的可穿越路径上放置(至少)5个警告标志。每个警告标志必须相隔500英尺, 因此,总体而言,如果你正在接近一个工作区,你应该看到第一个警告标志,因为你离它2500英尺远,然后在2000英尺处看到另一个警告标志, 还有一个是150
public static Double getLimitIncreasing(StreetVO street, Double offset) {
List<ConnectionVO> connections = street.getBeginNode().getConnections();
for ( ConnectionVO connect : connections ) {
StreetVO connectingStreet = connect.getOtherStreet(street);
if ( connectingStreet.getSpans().containsKey(Constants.WORK_ZONE) ) {
List<Double> connectionOffsets = getConnectionPoints(connectingStreet); // gets the points in footage where a workzone begins or ends on a given street, there could be multiple ones, and they are sorted in ascending
Double streetLength = connectingStreet.getStreetLength();
Double temp = 0.0;
if ( connectionOffsets.isEmpty() && (streetLength + offset) < 2500 ) { // if we don't find any connections but the street is too small we have to look at the connecting
// streets
temp = getLimitIncreasing(connectingStreet, (streetLength + offset), subdivision, logger, ptcDataModel); // look at the next connections
if ( temp < offset ) {
offset = temp;
}
} else if ( connectionOffsets.isEmpty() && (streetLength + offset) > 2500 ) {
continue;
} else if ( connectionOffsets.size() >= 1 ) {
offset = offset + (streetLength - connectionOffsets.get(connectionOffsets.size() - 1)); // gets the last connection, and subtract it from the length to get how far from the end of the street are we
}
}
}
if ( offset != 0 ) {
return offset;
} else {
return -1.0;
}
}
public class StreetVO{
NodeVO beginNode
NodeVO endNode
Double streetLength;
Map<String,Span> spans // There are other kinds of spans , that exist , workzones-dangerzones, etc... for this part of the problem i am only concerned about workzones, so passing the key value of workzone, will return to me a span object that lets me know whether a work zone on this current street exists , and whether it begins/ends on a different street along with the exact offset it begins/ends at.
}
public class NodeVO(){ //
List<ConnectionVO> connections;
}
public class Span(){ // a workzone can exist on two different street or the same street, and it should have a begin offset , and an end offset
Double beginOffset;
StreetVO workZoneBeginStreet;
StreetVO workZoneEndStreet;
Double endOffset;
}
public class ConnectionVO(){ // this object tells you which two streets connect to each other
StreetVO beginStreet;
StreetVO endStreet;
public getOtherStreet(StreetVO street){ // given one street it will return the other one it's connected to
if(street == beginStreet)return endStreet else return beginStreet
}
}
公共静态双偏移(StreetVO street,双偏移){
列表连接=street.getBeginNode().getConnections();
用于(连接VO connect:连接){
StreetVO connectingStreet=connect.getOtherStreet(street);
if(connectingStreet.getSpans().containsKey(Constants.WORK_ZONE)){
List connectionOffsets=getConnectionPoints(connectingStreet);//获取画面中工作区在给定街道上开始或结束的点,可能有多个点,并按升序排序
Double streetLength=连接Street.getStreetLength();
双温=0.0;
如果(connectionOffsets.isEmpty()&&(streetLength+offset)<2500){//如果我们找不到任何连接,但街道太小,我们必须查看连接点
//街道
temp=getLimitIncreating(连接街道,(街道长度+偏移量),细分,记录器,ptCDATA模型);//查看下一个连接
if(温度<偏移量){
偏移量=温度;
}
}else if(connectionOffsets.isEmpty()&&(街道长度+偏移量)>2500){
继续;
}else if(connectionOffsets.size()>=1){
offset=offset+(streetLength-connectionOffsets.get(connectionOffsets.size()-1));//获取最后一个连接,并从长度中减去它以获得我们离街道尽头有多远
}
}
}
如果(偏移量!=0){
返回偏移量;
}否则{
回报率-1.0;
}
}
公共级街道{
诺德沃贝金诺德酒店
NodeVO endNode
双倍街道长度;
映射跨距//还有其他类型的跨距,如工作区、危险区等。对于这部分问题,我只关心工作区,因此传递工作区的键值,将返回一个跨距对象,让我知道当前街道上是否存在工作区,以及它是否开始/结束于不同的街道以精确的偏移量开始/结束。
}
公共类NodeVO(){//
列出连接;
}
公共类Span(){//一个工作区可以存在于两条不同的街道或同一条街道上,它应该有一个开始偏移量和一个结束偏移量
双起始偏移;
StreetVO workZoneBeginStreet;
StreetVO工作区街道;
双内偏移;
}
公共类ConnectionVO(){//此对象告诉您哪两条街道相互连接
StreetVO beginStreet;
东街;;
public getOtherStreet(StreetVO street){//给定一条街道,它将返回它所连接的另一条街道
如果(street==beginStreet)返回endStreet,否则返回beginStreet
}
}
上面所附的示例场景有助于可视化问题和所需的解决方案。但正如您所看到的,在计算导航路径时可能会变得非常繁琐。递归是最好的方法吗?“但不能正常工作”.你能详细说明它正确/不正确的地方吗?你认为代码中的任何特定位置都可能是sou