码迷,mamicode.com
首页 > 其他好文 > 详细

(简单) POJ 3087 Shuffle'm Up,枚举。

时间:2015-01-16 23:40:54      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:

  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 containing C 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 for C = 5:

技术分享

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

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

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

 

  题目就是洗牌了,按照他的规则来洗,这个过程可以直接模拟,可以发现编号小于等于C(从下往上编号。)就乘以2,大于C的就乘以2然后-1,然后取模,这样的话可以看出洗牌会循环,因为只有2C种编号,对于某个来说第2C+1次一定是之前有过的编号,每一个都会循环的话,那这整个一定也会循环吧,我觉的就是周期为2C。

  然后就是一次次洗牌就好了,出现循环的话就算是不可能了。

 

代码如下:

技术分享
#include<iostream>
#include<cstring>

using namespace std;

char End[210];
char Sta[210];
int Snum[210];
int C;

void change()
{
    for(int i=1;i<=2*C;++i)
        if(Snum[i]<=C)
            Snum[i]*=2;
        else
            Snum[i]=(Snum[i]-C)*2-1;
}

bool judge()
{
    for(int i=1;i<=C*2;++i)
        if(End[Snum[i]-1]!=Sta[i-1])
            return 0;

    return 1;
}

void slove()
{
    for(int i=1;i<=2*C;++i)
        Snum[i]=i;

    int ans=1;

    change();

    while(Snum[1]!=1)
    {
        if(judge())
        {
            cout<<ans<<endl;
            return;
        }

        change();
        ++ans;
    }

    if(judge())
        cout<<ans<<endl;
    else
        cout<<-1<<endl;
}

int main()
{
    ios::sync_with_stdio(false);

    int T;
    char temp[110];
    cin>>T;

    for(int cas=1;cas<=T;++cas)
    {
        cin>>C;
        cin>>Sta;
        cin>>temp;
        strcat(Sta,temp);
        cin>>End;

        cout<<cas<< ;
        slove();
    }

    return 0;
}
View Code

 

(简单) POJ 3087 Shuffle'm Up,枚举。

标签:

原文地址:http://www.cnblogs.com/whywhy/p/4229887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!