Title Link: Shuffle ' m up
It's a bit disgusting except for the English question. Discovery Simulation + BFS can be past, is the water.
One bug is that the FILP function failed to get a string s12. Well. According to the thigh, string does not specify that the initialization length of the string is 0, which means that the assignment of each element is meaningless. Or. String is a reference data type, so the name is a pointer. Then the Str.resize () function happily solves the problem.
And there's the test instructions. The string from left to right is from bottom to top. I had a little bit of a rip-off.
The meaning of the road see Code Bar.
1 /*2 The topic is very long. Didn't look at it either. Baidu to test instructions. Give two cards of the same length. Shuffle way See figure get s12. Then to the bottom of the corresponding length of S1, the above for S2, and then, re-shuffle. 3 Ask S12 if it is possible to get S12 and target cards. What is the number of steps. 4 At first there was no train of thought. Then simulate. And then. BFs Take the first s12 as the initial state, and then start into the queue. Mark whether or not a map is present5 Examples:6 Next up7 Ahah8 HAHA9 HhaaaahhTen One Ahah A HAHA - Haahhaah - the Haah - Haah - Hhaaaahh - */ + -#include <string.h> +#include <iostream> A#include <queue> at#include <map> -#include <string> - #defineMAXN 1000000 - using namespacestd; - -queue<string>que; inmap<string,int>MP; - strings1, S2, S12; to stringtemp, now; + - intans; the intLen; * $ voidFILP (stringS1,stringS2) {Panax Notoginseng stringTempans; - //cout << "======\n"; the for(intI=0, j=0; i<len; i++, j+=2) { +TEMP[J] =S2[i]; A } the //cout << "------\ n"; + for(intI=0, j=1; i<len; i++, j+=2) { -TEMP[J] =S1[i]; $ } $temp[2*len] =' /'; - } - the voidDfs () { - while(!Que.empty ()) {Wuyinow =Que.front (); the Que.pop (); - //cout << now << "= =" << Endl; Wu if(now = =S12) { -Ans =Mp[now]; About return; $ } - for(intI=0; i<len; ++i) { -S1[i] =Now[i]; - } A for(intI=0; i<len; ++i) { +S2[i] = now[i+Len]; the } - Filp (S1, S2); $ if(!Mp[temp]) { the Que.push (temp); theMp[temp] = Mp[now] +1; the } the } - return; in } the the About intMain () { the intT; theCIN >>T; the intCNT =0; + while(t--) { - mp.clear (); theAns =MAXN;BayiCin >> len >> s1 >> S2 >>S12; theTemp.resize (2*len); the Filp (S1, S2); - -mp[temp]++; the while(!Que.empty ()) { the Que.pop (); the } the Que.push (temp); - dfs (); the if(ans = =MAXN) { thecout << ++cnt <<" "<<-1<<Endl; the }94 Elsecout << ++cnt <<" "<< ans <<Endl; the dfs (); the } the return 0;98}
View Code
POJ 3087 Shuffle ' m up BFS