E-POJ-3087 Shuffle'm Up

简介: POJ-3087 Description A common pastime for poker players at a poker table is to shuffle stacks of chips.

POJ-3087

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

网上都是模拟,可以看成单向搜索,重复就跳出来,感觉和模拟没啥不同

// 0MS 764KB
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<map>
using namespace std;
string s1,s2,s12;
int C;
struct Node{
    string cur;
    int step;
};
string Do(string &s1,string &s2){
    string now;
    for(int i=0;i<C;i++){
        now+=s2[i];
        now+=s1[i];
    }
    return now;
}
void BFS(string s1,string s2,string s12){
    map<string,bool>vis;     //来标记出现过的情况
    queue<Node>Q;
    Node now,next;
    now.step=1;
    now.cur=Do(s1,s2);
    vis[now.cur]=true;
    Q.push(now);
    while(!Q.empty()){
        now=Q.front();
        Q.pop();
        if(now.cur==s12){
            printf("%d\n",now.step);
            return;
        }
        s1.clear();s2.clear();   //开始没清空
        for(int i=0;i<C;i++)
            s1+=now.cur[i];
        for(int i=C;i<2*C;i++)
            s2+=now.cur[i];
        next.cur=Do(s1,s2);
        if(!vis[next.cur]){
            next.step=now.step+1;
            vis[next.cur]=true;
            Q.push(next);
        }
    }
    printf("-1\n");
}
int main(){
    int N,flag=1;
    scanf("%d",&N);
    while(N--){
        cin>>C;
        cin>>s1>>s2>>s12;
        printf("%d ",flag);
        flag++;
        BFS(s1,s2,s12);
    }
    return 0;
}

原本用的模拟过的,不过听学长说可以广搜过,写这的时候就写了个,结果bug找半天没找出来,只好找大佬帮忙了。发现又是没有清空数据。。。

目录
相关文章
|
Ubuntu 编译器 C++
【Conan 入门教程 】在Ubuntu上使用Conan编译C++第三方库:一站式解决方案
【Conan 入门教程 】在Ubuntu上使用Conan编译C++第三方库:一站式解决方案
4007 1
|
10天前
|
人工智能 数据可视化 安全
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
本文详解如何用阿里云Lighthouse一键部署OpenClaw,结合飞书CLI等工具,让AI真正“动手”——自动群发、生成科研日报、整理知识库。核心理念:未来软件应为AI而生,CLI即AI的“手脚”,实现高效、安全、可控的智能自动化。
34596 28
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
|
3天前
|
人工智能 机器人 开发工具
Windows 也能跑 Hermes Agent!完整安装教程 + 飞书接入,全程避坑
Hermes Agent 是一款自学习AI智能体系统,支持一键安装与飞书深度集成。本教程详解Windows下从零部署全流程,涵盖依赖自动安装、模型配置、飞书机器人接入及四大典型兼容性问题修复,助你快速构建企业级AI协作平台。(239字)
4273 10
|
5天前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
本文介绍了Claude Code终端AI助手的使用指南,主要内容包括:1)常用命令如版本查看、项目启动和更新;2)三种工作模式切换及界面说明;3)核心功能指令速查表,包含初始化、压缩对话、清除历史等操作;4)详细解析了/init、/help、/clear、/compact、/memory等关键命令的使用场景和语法。文章通过丰富的界面截图和场景示例,帮助开发者快速掌握如何通过命令行和交互界面高效使用Claude Code进行项目开发,特别强调了CLAUDE.md文件作为项目知识库的核心作用。
4619 19
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
|
22天前
|
人工智能 JSON 机器人
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
本文带你零成本玩转OpenClaw:学生认证白嫖6个月阿里云服务器,手把手配置飞书机器人、接入免费/高性价比AI模型(NVIDIA/通义),并打造微信公众号“全自动分身”——实时抓热榜、AI选题拆解、一键发布草稿,5分钟完成热点→文章全流程!
45444 151
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
|
12天前
|
人工智能 JSON 监控
Claude Code 源码泄露:一份价值亿元的 AI 工程公开课
我以为顶级 AI 产品的护城河是模型。读完这 51.2 万行泄露的源码,我发现自己错了。
5216 21
|
4天前
|
机器学习/深度学习 存储 人工智能
还在手写Skill?hermes-agent 让 Agent 自己进化能力
Hermes-agent 是 GitHub 23k+ Star 的开源项目,突破传统 Agent 依赖人工编写Aegnt Skill 的瓶颈,首创“自我进化”机制:通过失败→反思→自动生成技能→持续优化的闭环,让 Agent 在实践中自主构建、更新技能库,持续自我改进。
1011 3

热门文章

最新文章

下一篇
开通oss服务