AcWing 3385. 玛雅人的密码
原题链接
中等
作者:
我呼吸了
,
2022-08-05 21:55:41
,
所有人可见
,
阅读 8
#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;
}