3087POJ

这道题给你两部分木块。问你能否到达目标状态。注意是先放堆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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值