AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 商店
  • 应用
  • 文章
    • 题解
    • 分享
    • 问答
  • 吐槽
  • 登录/注册

AcWing 3385. 玛雅人的密码    原题链接    中等

作者: 作者的头像   我呼吸了 ,  2022-08-05 21:55:41 ,  所有人可见 ,  阅读 8


2


#include <bits/stdc++.h>
using namespace std;

int n;
string s;
unordered_map<string, int> d, st;

int bfs()
{
    queue<string> q;
    q.push(s);
    d[s] = 0;
    st[s] = 1;

    while(q.size())
    {
        string t = q.front();
        q.pop();

        if(t.find("2012") != t.npos) return d[t];

        for(int i = 1; i < t.size(); i ++)
        {
            string tt = t;

            swap(tt[i - 1], tt[i]);

            if(!st[tt])
            {
                st[tt] = 1;
                q.push(tt);
                d[tt] = d[t] + 1;
            }
        }
    }

    return -1;
}

int main()
{
    cin >> n >> s;

    cout << bfs() << endl;

    return 0;
}

0 评论

你确定删除吗?

© 2018-2022 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息