题解 | #玛雅人的密码#

玛雅人的密码

https://www.nowcoder.com/practice/761fc1e2f03742c2aa929c19ba96dbb0

def hyx(s: str, index: int):#实现数字互换
    x = list(s)
    x[index], x[index + 1] = x[index + 1], x[index]
    s = "".join(x)
    return s


def maya(n, x: str):#求解最少次数
    if "2012" in x:
        return 0
    if x.count("2") < 2 or x.count("1") < 1 or x.count("0") < 1:
        return -1
    ct = 0
    ans = []
    ans.append(x)
    zc = []
    while True:
        ans += zc
        for s in ans:#遍历每一层
            for i in range(len(s) - 1):#每一层内遍历检查
                if "2012" in s:
                    return ct
                zc.append(hyx(s, i))
        ct += 1


while True:
    try:
        n = int(input())
        s = input()
        print(maya(n, s))
    except:
        break

全部评论

相关推荐

码农索隆:有点耳熟,你们是我教过最差的一届
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务