这道题给你两部分木块。问你能否到达目标状态。注意是先放堆2,然后再放堆1,我们直接搜就好了。写一个生成状态函数,然后与目标状态比较。
#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<stack>
#include<set>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#define ll __int64
#define lll unsigned long long
#define MAX 10000009
#define MAXN 2009
#define eps 1e-8
#define INF 0x7fffffff
#define mod 1000000007
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
using namespace std;
string s,s1,s2;
int num,m;
string ok(string x,string y)
{
string ans ="";
for(int i = 0; i<m; i++)
{
ans+=y[i];
ans+=x[i];
}
return ans;
}
int solve()
{
map<string,bool>mp;
string str = ok(s1,s2);
if(s==str)
{
return 1;
}
//cout<<str<<endl;
while(mp.find(str)==mp.end())//判断map.find如果没有找到,会返回结束位置的迭代器,map.end()是map的尾部,我认为这里是第一遍到达str的状态等于map.end(),存在map的尾部,当再次到达str时候,str不存在map尾部,所以就结束循环
{
mp[str] = true;
s1 = str.substr(0,m);
s2 = str.substr(m,m);
str = ok(s1,s2);
//cout<<s<<endl;
num++;
if(s==str)
{
return num;
}
}
return -1;
}
int main()
{
//freopen("ans.txt","r",stdin);
int T;
scanf("%d",&T);
for(int cas = 1; cas<=T; cas++)
{
scanf("%d",&m);
num = 1;
cin>>s1>>s2>>s;
cout<<cas<<" "<<solve()<<endl;
}
return 0;
}