You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TV频道切换最小按键次数代码问题排查与修复请求

电视频道切换最少按键次数代码问题排查

题目背景

我们有一台带100个频道的电视,遥控器包含0-9数字键、下划线()键、上(up)键和下(down)键。上/下键可将频道±1;直接切换到两位数频道需先按_键(如切换到85需按 8 5),一位数频道直接按对应数字键即可。

输入规则:

  • 前4行输入代表按键状态(1为可用,0为损坏),格式示例:
    1 2 3 up
    4 5 6 down
    7 8 9 _
    0 1 
    
  • 下一行输入两个数X(当前频道)和Y(目标频道),0≤X,Y≤99。

输出规则:输出从X到Y的最少按键次数,无法到达则输出-1。

问题描述

编写了上述问题的Python代码(如下),预期获得满分,但仅通过平台70%的测试用例,剩余30%测试用例输出错误。尝试排查所有可能出错的场景但未发现问题,请求帮忙找出代码中的错误并提供修复方案。

代码

a1 , a2 , a3 , aU = input().split(" ")
a4 , a5 , a6 , aD = input().split(" ")
a7 , a8 , a9 = input().split(" ")
a  , a0 = input().split(" ")
X , Y = input().split(" ")
a1 , a2 , a3 , a4, a5, a6, a7 , a8, a9 , a0 , aU, aD, a = map(int, (a1 , a2, a3, a4, a5, a6, a7, a8, a9, a0, aU, aD, a))
a1 , a2 , a3 , a4, a5, a6, a7 , a8, a9 , a0 , aU, aD, a = map(bool, (a1 , a2, a3, a4, a5, a6, a7, a8, a9, a0, aU, aD, a))
X , Y = int(X) , int(Y)
keys = [False for i in range(100)]
keys[0:10] = map(bool, (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9))

currentNum10 = 0
if a:
    keys[0:10] = map(bool, (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9))
    while currentNum10 != 9:
        currentNum10 += 1
        if keys[currentNum10]:
            currentNum = -1
            while currentNum != 9:
                currentNum += 1
                if keys[currentNum]:
                    keys[(currentNum10*10)+currentNum] = True

keys[X] = True
movesU = 0
movesD = 0
if aU:
    i = Y
    while i >= 0:
        if keys[i]:
            if i == X:
                movesU = Y - i
                break
            elif i >= 10: 
                movesU = 3 + Y - i
                break
            else: 
                movesU = 1 + Y - i
                break
        i -= 1

if aD:
    i = Y
    while i <= 99:
        if keys[i]:
            if i == X:
                movesD = i - Y
                break
            elif i >= 10: 
                movesD = 3 + i - Y
                break
            else: 
                movesD = 1 + i - Y
                break
        i += 1

if movesD == 0 and movesU != 0:
    if X < Y and Y - X < movesU:
        print(Y - X)
    else:
        print(movesU)
elif movesD != 0 and movesU == 0:
    if X > Y and X - Y < movesD:
        print(X - Y)
    else:
        print(movesD)
elif movesD >= movesU and movesU != 0:
    print(movesU)
elif movesD < movesU:
    print(movesD)
elif X != Y:
    print(-1)
else:
    print(0)

代码说明

输入处理后,生成可直接切换的频道布尔数组;随后检查可通过上下键到达目标频道的路径;最后比较直接上下切换与按键切换的次数,输出最小值。


内容的提问来源于stack exchange,提问作者shahriarkm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 11:24:53