题解 | 玛雅人的密码
#include<iostream> #include<queue> using namespace std; struct my{ string ss; int i; }; int main(){ int n; cin>>n; string s; cin>>s; my m,y; m.ss = s; m.i = 0; queue<my> q; q.push(m); while(!q.empty()){ m = q.front(); string t = m.ss; if(t.find("2012")!=string::npos){ cout<<m.i<<endl; break; } q.pop(); for(int i=0;i<t.size()-1;i++){ string x = t; char tmp = x[i]; x[i] = x[i+1]; x[i+1] = tmp; y.ss = x; y.i = m.i+1; q.push(y); } q.push(y); } }