【POJ 3087】Shuffle'm Up(BFS,模拟,)

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.

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 integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1 and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2 starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). 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 though N), 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

2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC

Sample Output

1 2
2 -1

题目大意

给你三个字符串s1,s2和s12,每次操作就是把s2的第一个字符作为底,s1的最后一个字符作为顶,然后s1,s2交叉形成s12,之后把s12底下c个字符作为新的s1,上面c个字符作为新的s2,重复上述步骤,问你需要几次操作才能变成s3。操作流程如下图:
这里写图片描述

扫描二维码关注公众号,回复: 912578 查看本文章

思路

这题有两种做法,一种是模拟,一种是搜索,模拟相对简单。但是不管模拟还是搜索都需要处理一个核心的问题,就是怎样判断无法到达目标字符串?试想,每次的操作都是一样的,那么当我某次操作完后出现了和前面已经出现过的一样并且这个字符串不是目标字符串的情况,那么你继续操作下去是肯定不会到达目标字符串的,因为已经形成了一个循环。所以思路很简单,把已经出现过的字符串存起来或者标记,一旦出现重复返回输出-1就好了。具体实现看代码:

代码

BFS实现:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>

using namespace std;
const int maxn=1000;

char s1[maxn],s2[maxn],pos[2*maxn];
int c,n;

struct proc
{
    char str[2*maxn];
    int step;
};

void exchange(char s1[],char s2[],char str[])//分拆s成s1,s2; 
{
    int k=0;
    for(int i=0;i<c;i++)
    {
        s1[i]=str[i];
    }
    s1[c]='\0';
    for(int i=c;i<2*c;i++)
    {
        s2[k++]=str[i];
    }
    s2[c]='\0';
}

void join(char str[],char s1[],char s2[])//合并s1,s2 
{
    int k1=0,k2=0;
    for(int i=0;i<2*c;i++)
    {
        if(i%2==0) str[i]=s2[k2++];
        else str[i]=s1[k1++];
    }
    str[2*c]='\0';
}

int bfs(char s[])
{

    map<string,bool>vis;//用map来标记当前合并成的串是否已被访问过,如果被访问过即当前串和以前出现的串重复了,那么就形成了一个循环,肯定无法到达目标串。 
    proc vw,vn;
    queue<proc> q;
    strcpy(vw.str,s);
    vw.step=1;
    vis[vw.str]=true;
    q.push(vw);
    while(!q.empty())
    {
        char ts1[maxn],ts2[maxn];
        vw=q.front();
        q.pop();
        if(!strcmp(vw.str,pos))
        {
            return vw.step;
        }
        exchange(ts1,ts2,vw.str);
        join(vn.str,ts1,ts2);
        //if(vis[vn.str]&&strcmp(vn.str,pos)) return -1;  这段语句放这里就是WA,至今不懂为什么,害我Debug 2天。 
        if(!vis[vn.str])
        {
            vis[vn.str]=true;
            vn.step=vw.step+1;
            q.push(vn);
        }
    }
    return -1;
}

int main()
{
    int caseTime=1;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&c);
        cin>>s1>>s2>>pos;
        char ss[2*maxn];
        join(ss,s1,s2);
        int ans=bfs(ss);
        if(ans!=-1) printf("%d %d\n",caseTime++,ans);
        else printf("%d -1\n",caseTime++);
    }
    return 0;
}

模拟实现:摘自:http://blog.csdn.net/lyy289065406/article/details/6645450

//Memory Time 
//204K   0MS 

#include<iostream>
#include<string>
#include<map>
using namespace std;

int main(int i,int k)
{
    int test,c;
    int t=0;
    cin>>test;
    while(++t<=test)
    {
        cin>>c;
        char s1[201];   //牌堆1
        char s2[201];   //牌堆2
        char s12[401];  //预设最终的牌堆状态
        cin>>s1>>s2>>s12;

        map<string,bool>vist;   //记录出现过的洗牌状态(map缺省值为0)
        vist[s12]=true;

        int step=0;  //洗牌次数
        while(true)
        {
            char s[201];  //当前s1与s2洗牌后的状态
            int ps=0;  //s[]指针
            for(i=0;i<c;i++)    //s1与s2洗牌
            {
                s[ps++]=s2[i];
                s[ps++]=s1[i];
            }
            s[ps]='\0';

            step++;

            if(!strcmp(s,s12))   //当洗牌后的状态能达到预设状态时,输出
            {
                cout<<t<<' '<<step<<endl;
                break;
            }
            else if(vist[s] && strcmp(s,s12))  //当前洗牌后状态 与 前面出现过的状态重复了,但该状态不是预设状态
            {                                  //说明预设的状态无法达到
                cout<<t<<' '<<-1<<endl;
                break;
            }

            vist[s]=true;   //状态记录

            for(i=0;i<c;i++)   //分拆出s1与s2
                s1[i]=s[i];
            s1[i]='\0';

            for(k=0;i<2*c;i++,k++)
                s2[k]=s[i];
            s2[i]='\0';
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/iceiceicpc/article/details/52083516