Shuttle Puzzle
Traditional

The Shuttle Puzzle of size 3 consists of 3 white marbles, 3 black marbles, and a strip of wood with 7 holes. The marbles of the same color are placed in the holes at the opposite ends of the strip, leaving the center hole empty.

INITIAL STATE: WWW_BBB
GOAL STATE: BBB_WWW

To solve the shuttle puzzle, use only two types of moves. Move 1 marble 1 space (into the empty hole) or jump 1 marble over 1 marble of the opposite color (into the empty hole). You may not back up, and you may not jump over 2 marbles.

A Shuttle Puzzle of size N consists of N white marbles and N black marbles and 2N+1 holes.

Here's one solution for the problem of size 3 showing the initial, intermediate, and end states:

WWW BBB
WW WBBB
WWBW BB
WWBWB B
WWB BWB
W BWBWB
WBWBWB
BW WBWB
BWBW WB
BWBWBW
BWBWB W
BWB BWW
B BWBWW
BB WBWW
BBBW WW
BBB WWW

Write a program that will solve the SHUTTLE PUZZLE for any size N (1 <= N <= 12) in the minimum number of moves and display the successive moves, 20 per line.

PROGRAM NAME: shuttle

INPUT FORMAT

A single line with the integer N.

SAMPLE INPUT (file shuttle.in)

3

OUTPUT FORMAT

The list of moves expressed as space-separated integers, 20 per line (except possibly the last line). Number the marbles/holes from the left, starting with one.

Output the the solution that would appear first among the set of minimal solutions sorted numerically (first by the first number, using the second number for ties, and so on).

SAMPLE OUTPUT (file shuttle.out)

3 5 6 4 2 1 3 5 7 6 4 2 3 5 4

——————————————————题解

设白棋是1黑棋是2空格是0

若此时1101222 如果白棋左移那么会回到之前的一个状态1110222

若此时1011222 如果白棋左移那么会回到之前的一个状态1101222

重复状态不优所以只要搜索白棋右移黑棋左移即可

 /*
ID: ivorysi
LANG: C++
TASK: shuttle
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x7fffffff
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 30005
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
struct node {
int line[],bl;
vector<int> v;
}f;
queue<node > q;
int n;
bool check(node tem) {
if(tem.bl==n+) {
bool flag=;
siji(i,,n) {
if(tem.line[i]!=) flag=;
}
siji(i,n+,*n+) {
if(tem.line[i]!=) flag=;
}
if(flag) return true;
}
return false;
}
void bfs() {
scanf("%d",&n);
siji(i,,n) {
f.line[i]=;
}
f.bl=n+;
siji(i,n+,*n+) {
f.line[i]=;
}
q.push(f);
while(!q.empty()) {
node now=q.front();q.pop();
if(check(now)) {f=now;break;}
node t=now;
if(t.bl> && t.line[t.bl-]!=t.line[t.bl-] && t.line[t.bl-]==) {
t.v.push_back(t.bl-);
t.line[t.bl]=t.line[t.bl-];
t.line[t.bl-]=;
t.bl=t.bl-;
q.push(t);
t=now;
} if(t.bl> && t.line[t.bl-]==) {
t.v.push_back(t.bl-);
t.line[t.bl]=t.line[t.bl-];
t.line[t.bl-]=;
t.bl=t.bl-;
q.push(t);
t=now;
} if(t.bl<*n+ && t.line[t.bl+]==) {
t.v.push_back(t.bl+);
t.line[t.bl]=t.line[t.bl+];
t.line[t.bl+]=;
t.bl=t.bl+;
q.push(t);
t=now;
} if(t.bl<*n && t.line[t.bl+]!=t.line[t.bl+] && t.line[t.bl+]==) {
t.v.push_back(t.bl+);
t.line[t.bl]=t.line[t.bl+];
t.line[t.bl+]=;
t.bl=t.bl+;
q.push(t);
}
}
xiaosiji(i,,f.v.size()) {
printf("%d%c",f.v[i]," \n"[i==f.v.size()- || (i+)%==]);
}
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("shuttle.in","r",stdin);
freopen("shuttle.out","w",stdout);
#else
//freopen("f1.in","r",stdin);
#endif
bfs();
return ;
}

USACO 4.4 Shuttle Puzzle的更多相关文章

  1. USACO4.4 Shuttle Puzzle【bfs+优化】

    直接上$bfs$,每一个状态记录下当前字符串的样子,空格的位置,和走到这个状态的答案. 用空格的位置转移,只有$50pts$ 考虑到题目一个性质:$W$只往右走,$B$只往左走,就可以过了. #inc ...

  2. [USACO 09FEB]Fair Shuttle

    Description 逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼——如果要逛完一整天的集市,他们一定会筋疲力尽的.所以为了让 奶牛们也能愉快地逛集市,约翰准备让奶 ...

  3. p2739 Shuttle Puzzle

    观察样例得知就是和离'_'左边最近的'w'交换位置,然后和离'_'右边最近的'b'交换位置,轮流进行. #include <iostream> #include <cstdio> ...

  4. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  5. USACO刷题索引

    序 在距离CSP2019还有41天的国庆备战中,考了一场画风非常奇特的六校联赛,然后被教练建议刷一下这个巩固代码实现能力,然后就来了||ヽ(* ̄▽ ̄*)ノミ|Ю. 这个网站还是挺好玩儿的吧,刚开始各种 ...

  6. BZOJ1577 USACO 2009 Feb Gold 1.Fair Shuttle Solution

    权限题,不给传送门啦!在学校OJ上交的.. 有些不开心,又是一道贪心,又是一个高级数据结构的模板,又是看了别人的题解还写崩了QAQ,蒟蒻不需要理由呀. 正经题解: 首先,我们可以由「显然成立法」得出, ...

  7. [USACO 2009 Feb Gold] Fair Shuttle (贪心+优先队列)

    题目大意:有N个站点的轻轨站,有一个容量为C的列车起点在1号站点,终点在N号站点,有K组牛群,每组数量为Mi(1≤Mi≤N),行程起点和终点分别为Si和Ei(1≤Si<Ei≤N).计算最多有多少 ...

  8. TOJ 5021: Exchange Puzzle

    5021: Exchange Puzzle  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit ...

  9. USACO . Your Ride Is Here

    Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...

随机推荐

  1. Hadoop生态圈-使用MapReduce处理HBase数据

    Hadoop生态圈-使用MapReduce处理HBase数据 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.对HBase表中数据进行单词统计(TableInputFormat) ...

  2. Java基础-面向接口编程-JDBC详解

    Java基础-面向接口编程-JDBC详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.JDBC概念和数据库驱动程序 JDBC(Java Data Base Connectiv ...

  3. 获取异常信息e.printStackTrace()的内容

    获取异常信息e.printStackTrace()的内容 最近做项目的时候需要记录操作的日志,但是记录异常信息的是发现使用e.getMessage()根本无法满足需要,并且e.getMessage() ...

  4. OC中线程安全的单例

    @implementation MySingleton + (instancetype)sharedInstance { static MySingleton* instance = nil; sta ...

  5. HTTP协议(4):CGI

    CGI接口原理及实现(2012-12-7 Over) 1.CGI定义: CGI(CommonGateway Interface)是HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须 ...

  6. python安装pymssql

    安装pymssql pip install pymssql 关于python安装pymssql报错export PYMSSQL_BUILD_WITH_BUNDLED_FREETDS=1 然后再 pip ...

  7. java内存溢出xms xmx

    java内存堆栈不够用时我们会寻求java参数-Xms和-Xmx的帮助,网上也有许多前辈给出了例子,但很多人喜欢把-Xms和-Xmx的值设置成一样的,甚至我还见过有吧-Xms设的比-Xmx还要大(-X ...

  8. jquery radio的操作

    radio 按钮组, name=”sex”. <input type="radio" name="sex" value="Male"& ...

  9. redis内存模型

    前言 Redis是目前最火爆的内存数据库之一,通过在内存中读写数据,大大提高了读写速度,可以说Redis是实现网站高并发不可或缺的一部分. 我们使用Redis时,会接触Redis的5种对象类型(字符串 ...

  10. Cookie详解、ASP.NET核心知识(7)

    无状态的http协议 1.回顾http协议 Http协议是请求响应式的,有请求才有响应,是无状态的,不会记得上次和网页“发生了什么”. 关于http协议的这种特点,黑兔在前面的这三篇博文中进行了详细的 ...