Java 递归算法问题

Java 递归算法问题,java,recursion,Java,Recursion,我有一个街道网络,每条街道可以分叉成两条街道,所以同样地,两条街道可以汇集成一条街道。 每次发生收敛或发散时,都存在一个节点。然后是工作区跨度,放置在街道顶部 (因此工作区跨度可以悬停在多条街道上)。现在的想法是,我必须基本上扫描并找到工作区的起点, 并在通往工作区实际起点的可穿越路径上放置(至少)5个警告标志。每个警告标志必须相隔500英尺, 因此,总体而言,如果你正在接近一个工作区,你应该看到第一个警告标志,因为你离它2500英尺远,然后在2000英尺处看到另一个警告标志, 还有一个是150

我有一个街道网络,每条街道可以分叉成两条街道,所以同样地,两条街道可以汇集成一条街道。 每次发生收敛或发散时,都存在一个节点。然后是工作区跨度,放置在街道顶部 (因此工作区跨度可以悬停在多条街道上)。现在的想法是,我必须基本上扫描并找到工作区的起点, 并在通往工作区实际起点的可穿越路径上放置(至少)5个警告标志。每个警告标志必须相隔500英尺, 因此,总体而言,如果你正在接近一个工作区,你应该看到第一个警告标志,因为你离它2500英尺远,然后在2000英尺处看到另一个警告标志, 还有一个是1500等等

因此,总体而言,所有这些实体都作为对象存在:

  • Street对象在节点处开始和结束,街道的另一个属性是它们的长度
  • 工作区跨度具有“开始”和“结束”属性,用于标记开始和结束的确切镜头 (例如:工作区跨度从1号街的500英尺开始,到9号街的3600英尺结束

  • 节点对象,节点具有连接对象,连接对象将两条街道连接在一起

  • 作为节点一部分的连接对象,并告诉您哪两条街道相互连接
  • 警告标志对象,我在某些英尺处创建
  • 所以总体来说,我的方法是穿过街道,检查我离工作区开始/结束有多远。 所以我首先检查我所在的街道,如果没有工作区开始/结束,然后检查连接, 确定是否存在工作区开始/结束。如果连接街道上存在工作区开始/结束, 然后我确定它有多远,如果连接的街道有2000英尺长,工作区从1000英尺开始, 这意味着我需要在我目前正在检查的街道上放置三个警告标志。 但正如你们所看到的,不同的可能性会使事情变得相当复杂。 鉴于我一次只能处理一条街道,这是我提出的部分解决方案,但它仍然不完整,因为我不确定如何确定所需的最大警告标志数量。下面所附的场景说明了这一点,但基本上我将如何处理工作区连接到n两条街道,连接第三条街道。我如何返回所需的最高数量的标志? 案例:

    //调用此方法从左侧扫描街道网络,以查看是否存在任何工作区 //最初,我们将偏移量传递为零,并且给定的街道

    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