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 ...
随机推荐
- vue学习记录
vue中常用的指令 v-model 双向数据绑定,一般用于表单元素 v-for 对数组或对象进行循环操作,使用的是v-for <!-- 普通循环 --><li v-for=" ...
- 3 Kafka两个版本在Zookeeper的元数据存储
0.8 [consumers, admin, config, controller, brokers, controller_epoch] 这些节点都归于谁管理 consumers: 0.8版本的客户 ...
- git 提交模板配置
1.创建模板文件,比如gitTemplate.txt,内容如下: ABSTRACT:修改自测发现的多度数据同步相关问题. Bug Fix [Y/N]:NBug ID:New Feature [Y/N] ...
- 洛谷P1102 A-B数对
洛谷P1102 A-B数对 https://www.luogu.org/problem/show?pid=1102 题目描述 出题是一件痛苦的事情! 题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的A ...
- openGL笔记-画基本图形
#include "iostream" #include <GL/glut.h> #include<cmath> #include<vector> ...
- 图论:LCA-欧拉序
#include<cmath> #include<vector> #include<cstdio> #include<cstring> #include ...
- highCharts使用记录
公司的架构师让我做一个mockup,要用到highCharts,,以前想接触的,没时间学习,也没有用过,正好工作可以用上了,可以边学边做了. 环境 <script src="./js/ ...
- Nginx upstream的5种权重分配方式【转】
原文地址:Nginx upstream的5种权重分配方式 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight指定轮询几率,weig ...
- # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
20155222 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 java中所有错误都会被包装为对象,如果你愿意,可以尝试(try)执行程序并捕捉代表错误的对 ...
- 配置ODBC DSN数据源,导出数据库数据到Excel过程记录
一.前言 工作中我们可能遇到这样的需要:查询数据库中的信息,并将结果导出到Excel文件.这本来没什么,但数据量比较大时,用PLSQL.toad导出Excel会出现内存不足等情况,使用odbc+Mic ...