题解 | #玛雅人的密码#

玛雅人的密码

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 17:10
什么素质,我请问呢,要掉小珍珠了。。。又憋屈又生气
苍蓝星上艾露:给它们能的,一群dinner牛马挥刀向更弱者罢了。我写的开源求职AI co-pilot工具,优化你的简历,找到你匹配的岗位,定制你的简历,并让你做好面试准备https://github.com/weicanie/prisma-ai
点赞 评论 收藏
分享
Lorn的意义:你这种岗位在中国现在要么牛马天天加班,要么关系户进去好吃好喝,8年时间,真的天翻地覆了,对于资本来说你就说一头体力更好的牛马,哎,退伍没有包分配你真的亏了。
点赞 评论 收藏
分享
06-23 11:28
门头沟学院 Java
牛客91966197...:也有可能是点拒绝的时候自动弹的话术
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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