OD C卷 - 路口最短时间问题

路口最短时间问题 (200)

  • 街道是棋盘型的,(十字路口)每格距离相等,车辆通过每格的街道时间均为time_per_road;
  • 十字路口有交通灯,此处车辆可直行、左转、右转,直行和左转需要等待相应T时间;右转无需等待;
  • 现给出n*m个街口的交通灯等待时间,及街口的起止坐标,计算车辆从起始到终止的最短时间;
  • 终点路口的等待时间不计入;
    输入描述:
    每个十字路口的等待时间(二维数组),街道通过时间,起始点,终止点
    如:
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]],60,0,0,2,2
    输出描述:
    从起始点到终点的最短时间
    245
    说明,以上行走路径为(0,0)->(0,1)->(1,1)->(1,2)->(2,2) 2个右转,一个左转,共耗时60+0+60+5+60+0+60=245

思路:

  • 为确定直行、右转、左转的方向,所以需要分情况讨论进入起始十字路口的方向;

    • 分别以上、右、下、左四个方向进入起始点,并设定进入的时间为0,入队,然后开始BFS;
    • 每种方向情况下,计算出可以走的直行、右转、左转方向,再开始行走到下一个位置,若时间更少,则放入队列;
      在这里插入图片描述
  • 期间时间的累加,非右转时,红灯等待时间 + 街道通行时间;右转只有街道通行时间;

  • 使用result 二维数组表示到达每个十字路口的最小时间(包括四个方向)

  • 最后取终点位置四个方向的最小值;


import queue

# 上、右、下、左
directions = [[-1, 0], [0, 1], [1, 0], [0, -1]]


def calcTime(lights, time_per_road, row_start, col_start, row_end, col_end):
    # 二维数组的行、列数
    col = col_end + 1
    row = row_end + 1

    # result 表示 以哪种方式转弯,通过街道后进入该十字路口
    # [
    #   [1, 2, 3],
    #   [4, 5, 6],
    #   [7, 8, 9]
    # ]  每个数字表示该十字路口的等待时间,即 lights
    # result 则把对应的数字替换为[inf, inf, inf, inf]
    # 表示分别以四种方式转弯,通过街道到达该位置的最短时间
    result = [[[float('inf') for i in range(4)] for j in range(col)] for k in range(row)]

    # 队列
    pq = queue.Queue()

    # 进入起始十字路口 可以有四种方式,针对具体每种方式,才可以确定从该路口通过时
    # 的直行、右转、左转
    for i in range(4):
        # 进入十字路口的 每种方式入队 i -> 0, 1, 2, 3 对应directions的索引
        pq.put([0, [row_start, col_start, i, 0]]) # 以第i种方式转弯,然后通过街道后,进入到起始点位置,时间初始化为0

        # 起始点,每种方式的进入,时间都设置为0
        result[row_start][col_start][i] = 0

    # 从初始点开始走
    while True:
        if pq.qsize() <= 0:
            break
        else:
            point_t = pq.get() # 出队
            print("point", point_t)
            point = point_t[1] # [0, 0, 0, 0]

            # 当前方式转弯 进入该点的时间,若比 result 中已有的对应时间大,则跳过
            if point[3] > result[point[0]][point[1]][point[2]]:
                continue

            # 当前以point[2]转弯方式,通过街道进入该point十字路口,通过该路口时
            # 直行、右转、左转方向计算
            for i in range(4):
                # 确定方向的索引
                cur_direct = (point[2] + i) % 4
                # if not (directions[i][0] == 1 and directions[i][1] == 0): # 方向 (1, 0) 不能走
                if not (cur_direct == (point[2] + 2) % 4): # 其他方向都是可以走
                    # 新位置点
                    new_x = point[0] + directions[cur_direct][0]
                    new_y = point[1] + directions[cur_direct][1]

                    # 判断新位置点是否有效
                    if 0 <= new_x < row and 0 <= new_y < col:
                        # 通过中间街道 耗时
                        new_time = point[3] + time_per_road

                        # cur_direct 不是右转时,还要累加红灯等待时间
                        # if not (directions[i][0] == 0 and directions[i][1] == 1):
                        if not (cur_direct == (point[2] + 1) % 4):
                            # 加街口的等待时间
                            new_time += lights[point[0]][point[1]] # 通过的上一个十字路口 红灯等待

                        # 以cur_direct 转弯/直行,并通过街道到达新位置的时间为new_time
                        # 以更小的时间更新result
                        if new_time < result[new_x][new_y][cur_direct]:
                            result[new_x][new_y][cur_direct] = new_time # 取更小的时间
                            pq.put([new_time, [new_x, new_y, cur_direct, new_time]])
    print(result)

    return min(result[row_end][col_end])


lights = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
time_per_road = 60
row_start = 0
col_start = 0
row_end = 2
col_end = 2

min_time = calcTime(lights, time_per_road, row_start, col_start, row_end, col_end)
print(min_time)

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

laufing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值