P1518测试点3、6、8超时了
查看原帖
P1518测试点3、6、8超时了
52067
HITACorz楼主2024/4/9 16:16

这个超时是因为他们不会相遇吗?

cow_pos = []
John_pos = []
list_map = []
next_pos = [
    [-1, 0],  # up
    [0, 1],  # right
    [1, 0],  # down
    [0, -1]  # left
]
def main():
    # construst map and get the pos
    for i in range(10):
        row = input()
        if 'F' in row:
            j = row.find('F')
            John_pos.append([i, j])
        if 'C' in row:
            j = row.find('C')
            cow_pos.append([i, j])
        list_map.append(row)
    print(cal_time())

def cal_time():
    del_time = 0
    next_dir_cow = [0]
    next_dir_John = [0]
    while cow_pos != John_pos:
        each_move(John_pos, next_dir_John, "F")
        each_move(cow_pos, next_dir_cow, "C")


        del_time += 1
    return del_time


def str_replace(strs, pos, item):
    strs = strs[:pos] + item + strs[pos + 1:]
    return strs

def each_move(pos, next_dir, item):
    x = pos[0][0]
    y = pos[0][1]

    next_x = x + next_pos[next_dir[0]][0]
    next_y = y + next_pos[next_dir[0]][1]

    go = True

    if next_x >= 0 and next_x < 10 and next_y >= 0 and next_y < 10:
        if list_map[next_x][next_y] != '*':
            list_map[next_x] = str_replace(list_map[next_x], next_y, item)
            list_map[x] = str_replace(list_map[x], y, '.')
        else:  # 障碍
            go = False

    else:  # 越界,相当于碰障碍
        go = False

    if go == True:
        pos[0][0] = next_x
        pos[0][1] = next_y
    else:
        next_dir[0] += 1
        next_dir[0] %= 4


if __name__ == '__main__':
    main()

2024/4/9 16:16
加载中...