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. Camera ISO、快门、光圈、曝光这几个概念

    转载自知乎:https://www.zhihu.com/question/21427664 种田要知节气,开车要懂离合,任何一样手艺都有行话.虽然我觉得尽量从实际问题说起,尽量不要说的很专业,但有几个 ...

  2. VScode常见问题汇总(持续更新)

    1.formatOnSave--保存后自动格式化        根据每个人安装的插件不同,可能问题原因也不同.        我个人的原因是因为JS-CSS-HTML Formatter这个插件,那么 ...

  3. 算法习题-FFT

    Q1(hdu1402): 给出两个很大的数字A,B,计算二者乘积. 分析:这个题目java应该能过,用FFT做能够加速计算.这里将字符串A按权(10进制)展开,前面的系数就是多项式的系数,这样就构造出 ...

  4. SQL语句(二十一)—— 触发器(DML触发器)

    触发器 一 .触发器概述(特殊的存储过程) 定义: 在修改指定表值的数据时执行的 存储过程. 不同的是 : 执行存储过程要使用EXEC语句来调用,而触发器的执行不需要使用EXEC语句来调用. 作用: ...

  5. 51 Free Data Science Books

    51 Free Data Science Books A great collection of free data science books covering a wide range of to ...

  6. jdk源码

    Java 8 之默认方法(Default Methods) public interface Player { String getName(); default boolean isMale() { ...

  7. Javascript 常用的工具函数,更新中...

    1.时间戳转为格式化时间 /** * 时间戳转为格式化时间 * @Author chenjun * @DateTime 2017-11-10 * @param {[date]} timestamp [ ...

  8. [转载]memset()的效率

    http://blog.csdn.net/hackbuteer1/article/details/7343189 void *memset(void *s, int ch, size_t n); 作用 ...

  9. 为什么JavaScript声明变量的时候鼓励加var关键字

    在JavaScript中,var用来声明变量,但是这个语法并不严格要求,很多时修改,我们可以直接使用一个变量而不用var声明它. var x = "XX"; y ="xx ...

  10. js数组排序 reverse()和sort()方法的使用

    WEB前端|js数组排序reverse()和sort()方法的使用,数组中已经存在两个可以直接用来重排序的方法:reverse()和sort(). reverse()方法会对反转数组项的顺序. var ...