#include <iostream>
#include <algorithm>
#include <queue>
#include <unordered_map>
using namespace std;
string start,over;
int bfs(string start)
{
queue<string> q;
unordered_map<string,int> d;
q.push(start);
d[start] = 0;
int dx[6] = {-3,-2,-1,1,2,3}; //6个方向
while (q.size())
{
auto t = q.front();
q.pop();
int distance = d[t];
if (t == over) return distance;
int k = t.find('*'); //找到空杯子的下标
for (int i = 0 ; i < 6 ; i ++)
{
int a = k + dx[i];
if (a >= 0 && a < start.size())
{
swap(t[k],t[a]);
if (!d.count(t))
{
d[t] = distance + 1;
q.push(t);
}
swap(t[k],t[a]);
}
}
}
}
int main()
{
cin >> start >> over; //起始状态,结束状态
cout << bfs(start) <<endl;
return 0;
}
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
string s1,s2;
map<string,int> vis; //标记字符串是否出现过
struct node
{
string s;
int step;
node(string ss , int sstep)
{
s = ss,
step = sstep;
}
};
queue<node> q;
int bfs()
{
int len = s1.size();
q.push(node(s1,0));
while (q.size())
{
auto t = q.front();
q.pop();
if (t.s==s2) return t.step;
for (int i = 0 ; i < len ; i ++)
{
if (t.s[i]=='*')
{
for (int j = max(0,i - 3) ; j <= min (len - 1, i + 3 ); j ++) //枚举空杯子周围的3个杯子,注意边界
{
string ts = t.s;
if (ts[j] != '*')
{
swap(ts[j],ts[i]);
if (vis[ts] == 0)
{
vis[ts] = 1;
q.push(node(ts,t.step + 1));
}
}
}
}
}
}
}
int main()
{
cin >> s1 >> s2;
cout << bfs() <<endl;
return 0;
}