您的位置:首页 > 其它

POJ 3087 Shuffle'm Up (深搜)

2018-04-04 19:25 267 查看
【题目链接】

http://poj.org/problem?id=3087

题目意思

模拟洗牌的过程,每次把牌分成两部分。然后下部分的牌一张上部分牌一张,无限循环下去问你是否能切出对应的牌。如果能要洗多少次,如果不能就输出-1(用字符表示各种牌)。

解题思路

模拟分牌,合牌过程,由于不断重复这个过程递归就可以了

代码部分

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3
const int N = 1e5+5;
int a[4]={1000,100,10,1};
int n;
string s12,s1,s2;
map<string , int>M;
int dfs (string s1, string s2,int step)
{
string c1;  ///串1
string c2;  ///串2
string c12; ///合串
c12 = s1+s2;
if (M[c12] == 1)  ///出现过的串说明循环了
return -1;
if (c12 == s12)
return step;
M[c12]++;
int k = 0;
for (int i = 0; i < n; i++) ///重新分串
{
if (k < n)
c1 += s2[i];
else
c2 += s2[i];
k++;
if (k < n)
c1 += s1[i];
else
c2 += s1[i];
k++;
}
return dfs(c1,c2,step+1);
}
int main()
{
int t,cas = 1;
scanf("%d",&t);
while (t--)
{
M.clear();
scanf("%d",&n);
cin>>s1>>s2>>s12;
int s=dfs(s1,s2,0);
printf("%d %d\n",cas++,s);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: