Title Description
Planet X's popular pet frogs, there are two colors: white and black.
Residents of Planet X likes to put them in a row in the cup, so you can watch them jumping around.
Below a row of cups, one on the left is empty, the right side of the cup, each have an inside frog.
* WWWBBB
wherein, W represents white letters frogs, B represents black frog, * indicates an empty cup.
Planet X is some frog habit, they only do one of three actions
1. jump to an adjacent empty cup.
2. other across a frog (whatever color) to jump to an empty cup.
3. across two other frogs (whatever color) to jump to an empty cup.
For the situation on the map, as long as one step, you can jump into the situation: WWW * BBB
task of this problem is known initial situation, ask at least a few steps in order to jump to another target situation.
Residents of Planet X likes to put them in a row in the cup, so you can watch them jumping around.
Below a row of cups, one on the left is empty, the right side of the cup, each have an inside frog.
* WWWBBB
wherein, W represents white letters frogs, B represents black frog, * indicates an empty cup.
Planet X is some frog habit, they only do one of three actions
1. jump to an adjacent empty cup.
2. other across a frog (whatever color) to jump to an empty cup.
3. across two other frogs (whatever color) to jump to an empty cup.
For the situation on the map, as long as one step, you can jump into the situation: WWW * BBB
task of this problem is known initial situation, ask at least a few steps in order to jump to another target situation.
Entry
A plurality of sets of test data input, for each test:
input rows 2, 2 strings, represents the initial situation and the target situation. The length of the input string does not exceed 15
input rows 2, 2 strings, represents the initial situation and the target situation. The length of the input string does not exceed 15
Export
For each set of test data: output request is an integer representing the number of steps of at least frog jump.
Sample input Copy
*WWBB
WWBB*
WWW * BBB
BBB * WWW
Sample output Copy
2
10
1 #include<bits/stdc++.h> 2 #define inf 0x3f3f3f3f 3 typedef long long ll; 4 using namespace std; 5 string a,b; 6 map<string,int>sigmap; 7 class node{ 8 public: 9 string s; 10 int step; 11 int id; 12 node(string S,int step,int id){ 13 this->s=S; 14 this->step = step; 15 this->id=id; 16 } 17 }; 18 void bfs() 19 { 20 queue<node>myque; 21 if(a==b) 22 cout<<0<<endl; 23 else{ 24 node Nod=node(a,0,a.find('*')); 25 myque.push(Nod); 26 sigmap[a]=1; 27 while(!myque.empty()){ 28 Nod=myque.front(); 29 myque.pop(); 30 for(int i=Nod.id-3; i<=Nod.id+3; i++){ 31 if(i>=0&&i<Nod.s.length()&&i!=Nod.id){ 32 string S=Nod.s; 33 swap(S[i],S[Nod.id]); 34 if(sigmap[S] == 1) 35 continue; 36 if(S==b){ 37 cout<<Nod.step+1<<endl; 38 return; 39 } 40 sigmap[S]=1; 41 node now=node(S,Nod.step+1,i); 42 myque.push(now); 43 } 44 } 45 } 46 } 47 } 48 int main() 49 { 50 while(cin>>a){ 51 cin>>b; 52 bfs(); 53 } 54 return 0; 55 }