POJ 3087 Shuffle'm Up(DFS 循环)


Shuffle'm Up

Description

A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips,S1 and S2, each stack containingC chips. Each stack may contain chips of several different colors.

The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below forC = 5:

The single resultant stack, S12, contains 2 * C chips. The bottommost chip of S12 is the bottommost chip fromS2. On top of that chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom ofS2 and placing that on S12, followed by the 2nd chip from the bottom ofS1 and so on until the topmost chip from S1 is placed on top ofS12.

After the shuffle operation, S12 is split into 2 new stacks by taking the bottommostC chips from S12 to form a newS1 and the topmost C chips fromS12 to form a new S2. The shuffle operation may then be repeated to form a newS12.

For this problem, you will write a program to determine if a particular resultant stackS12 can be formed by shuffling two stacks some number of times.

Input

The first line of input contains a single integer N, (1 ≤N ≤ 1000) which is the number of datasets that follow.

Each dataset consists of four lines of input. The first line of a dataset specifies an integerC, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1 andS2). The second line of each dataset specifies the colors of each of theC chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of theC chips in stack S2 starting with the bottommost chip. Colors are expressed as a single uppercase letter (A throughH). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 *C uppercase letters (A through H), representing the colors of the desired result of the shuffling of S1 and S2 zero or more times. The bottommost chip’s color is specified first.

Output

Output for each dataset consists of a single line that displays the dataset number (1 thoughN), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative 1 (−1) for the number of shuffle operations.

Sample Input

24AHAHHAHAHHAAAAHH3CDECDEEEDDCC

Sample Output

1 22 -1


so easy的题目,虽然敲的的时候有点堵,但是题目还是挺简单的。主要卡在了一个特大意的地方,有个数组是c[],然后我又定义了一个名称为c的变量,输入的时候,就出错了。刚开始用的是DFS,本想着可能会超时,没想到一次就过了,然后试了试只用循环,也是一次就过了


DFS:
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int a[110],a1[110],b[110],b1[110],c[210],d[210],n,flag;
int judge()
{
    for(int i=0; i<2*n; i++)
        if(c[i]!=d[i])
            return 0;
    return 1;
}
int judg()
{
    for(int i=0; i<n; i++)
        if(a[i]!=a1[i])
            return 0;
    for(int i=0; i<n; i++)
        if(b[i]!=b1[i])
            return 0;
    return 1;
}
void dfs(int step)
{
    if(flag)
        return;
    if(judge())
    {
        printf("%d\n",step);
        flag=1;
        return;
    }
    for(int i=0; i<2*n; i++)
    {
        if(i%2)
            d[i]=a1[i/2];
        else
            d[i]=b1[i/2];
    }
    for(int i=0; i<2*n; i++)
    {
        if(i<n)
            a1[i]=d[i];
        else
            b1[i-n]=d[i];
    }
    if(judg())
    {
        printf("-1\n");
        flag=1;
        return;
    }
    dfs(++step);
}
int main()
{
    int cas;
    scanf("%d",&cas);
    for(int k=1; k<=cas; k++)
    {
        memset(a1,0,sizeof(a1));
        memset(b1,0,sizeof(b1));
        memset(d,0,sizeof(d));
        flag=0;
        scanf("%d",&n);
        char ct;
        getchar();
        for(int i=0; i<n; i++)
        {
            scanf("%c",&ct);
            a[i]=ct-'A';
            a1[i]=a[i];
        }
        getchar();
        for(int i=0; i<n; i++)
        {
            scanf("%c",&ct);
            b[i]=ct-'A';
            b1[i]=b[i];
        }
        getchar();
        for(int i=0; i<2*n; i++)
        {
            scanf("%c",&ct);
            c[i]=ct-'A';
        }
        getchar();
        printf("%d ",k);
        dfs(0);
    }
    return 0;
}




 
While:
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int a[110],a1[110],b[110],b1[110],c[210],d[210],n;
int judge()
{
    for(int i=0; i<2*n; i++)
        if(c[i]!=d[i])
            return 0;
    return 1;
}
int judg()
{
    for(int i=0; i<n; i++)
        if(a[i]!=a1[i])
            return 0;
    for(int i=0; i<n; i++)
        if(b[i]!=b1[i])
            return 0;
    return 1;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    for(int k=1; k<=cas; k++)
    {
        memset(a1,0,sizeof(a1));
        memset(b1,0,sizeof(b1));
        memset(d,0,sizeof(d));
        scanf("%d",&n);
        char ct;
        getchar();
        for(int i=0; i<n; i++)
        {
            scanf("%c",&ct);
            a[i]=ct-'A';
            a1[i]=a[i];
        }
        getchar();
        for(int i=0; i<n; i++)
        {
            scanf("%c",&ct);
            b[i]=ct-'A';
            b1[i]=b[i];
        }
        getchar();
        for(int i=0; i<2*n; i++)
        {
            scanf("%c",&ct);
            c[i]=ct-'A';
        }
        getchar();
        printf("%d ",k);
        for(int sum=0;;sum++)
        {
            if(judge())
            {
                printf("%d\n",sum);
                break;
            }
            for(int i=0; i<2*n; i++)
            {
                if(i%2)
                    d[i]=a1[i/2];
                else
                    d[i]=b1[i/2];
            }
            for(int i=0; i<2*n; i++)
            {
                if(i<n)
                    a1[i]=d[i];
                else
                    b1[i-n]=d[i];
            }
            if(judg())
            {
                printf("-1\n");
                break;
            }
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值