USACO 4.4 Shuttle Puzzle
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的更多相关文章
- USACO4.4 Shuttle Puzzle【bfs+优化】
直接上$bfs$,每一个状态记录下当前字符串的样子,空格的位置,和走到这个状态的答案. 用空格的位置转移,只有$50pts$ 考虑到题目一个性质:$W$只往右走,$B$只往左走,就可以过了. #inc ...
- [USACO 09FEB]Fair Shuttle
Description 逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼——如果要逛完一整天的集市,他们一定会筋疲力尽的.所以为了让 奶牛们也能愉快地逛集市,约翰准备让奶 ...
- p2739 Shuttle Puzzle
观察样例得知就是和离'_'左边最近的'w'交换位置,然后和离'_'右边最近的'b'交换位置,轮流进行. #include <iostream> #include <cstdio> ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- USACO刷题索引
序 在距离CSP2019还有41天的国庆备战中,考了一场画风非常奇特的六校联赛,然后被教练建议刷一下这个巩固代码实现能力,然后就来了||ヽ(* ̄▽ ̄*)ノミ|Ю. 这个网站还是挺好玩儿的吧,刚开始各种 ...
- BZOJ1577 USACO 2009 Feb Gold 1.Fair Shuttle Solution
权限题,不给传送门啦!在学校OJ上交的.. 有些不开心,又是一道贪心,又是一个高级数据结构的模板,又是看了别人的题解还写崩了QAQ,蒟蒻不需要理由呀. 正经题解: 首先,我们可以由「显然成立法」得出, ...
- [USACO 2009 Feb Gold] Fair Shuttle (贪心+优先队列)
题目大意:有N个站点的轻轨站,有一个容量为C的列车起点在1号站点,终点在N号站点,有K组牛群,每组数量为Mi(1≤Mi≤N),行程起点和终点分别为Si和Ei(1≤Si<Ei≤N).计算最多有多少 ...
- TOJ 5021: Exchange Puzzle
5021: Exchange Puzzle Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit ...
- 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 ...
随机推荐
- 流媒体技术学习笔记之(十六)H264编码profile & level控制
H.264有四种画质级别,分别是baseline, extended, main, high: 1.Baseline Profile:基本画质.支持I/P 帧,只支持无交错(Progressive)和 ...
- Java并发编程原理与实战九:synchronized的原理与使用
一.理论层面 内置锁与互斥锁 修饰普通方法.修饰静态方法.修饰代码块 package com.roocon.thread.t3; public class Sequence { private sta ...
- JS替换地址栏参数值
首先,页面引入JS代码片段(整体复制粘贴即可): var Query=function(a){"use strict";var b=function(a){var b=[],c,d ...
- c# net 使用反射为对象赋值
public T Bson2T(MongoDB.Bson.BsonDocument bson) { T t = default(T); //获取T类中的所有属性 PropertyInfo[] Tpro ...
- HDU 2095 find your present (2) 动态链表
解题报告:输入一个n,后面紧跟着输入n个数,输入的这n个数中,除了有一个数的个数为奇数外,其它的数的个数都是偶数个,现在要你找出这个个数为奇数的这个数. 看起来好像很简单的样子,不过,这题的重点不在这 ...
- js数据类型隐式转换问题
js数据类型隐式转换 ![] == false //true 空数组和基本类型转换,会先[].toString() 再继续比较 ![] == [] //true ![] //false [] == [ ...
- J - Borg Maze +getchar 的使用注意(二维字符数组的输入)
题目链接: https://vjudge.net/contest/66965#problem/J 具体思路: 首先将每个点之间的最短距离求出(bfs),A 或者 S作为起点跑bfs,这样最短距离就求出 ...
- std 与标准库
1.命名空间std C++标准中引入命名空间的概念,是为了解决不同模块或者函数库中相同标识符冲突的问题.有了命名空间的概念,标识符就被限制在特定的范围(函数)内,不会引起命名冲突.最典型的例子就是st ...
- for-of循环和for-in循环的区别
基本上for in用于大部分常见的由key-value对构成的对象上以遍历对象内容. 但是for in在遍历数组对象时并不方便,这时候用for of会很方便.
- bind系统调用
/* * Bind a name to a socket. Nothing much to do here since it's * the protocol's responsibility to ...