一 原题

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 BWBWBWBWBWB
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

二 分析

加一个小小的剪枝的BFS 即最优解里W只会向右移 B只会向左移。最后没输出换行WA一次

三 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: shuttle
LANG: C++Compiling...
Compile: OKExecuting...Test 1: TEST OK [0.000 secs, 4192 KB]Test 2: TEST OK [0.000 secs, 4192 KB]Test 3: TEST OK [0.000 secs, 4192 KB]Test 4: TEST OK [0.000 secs, 4192 KB]Test 5: TEST OK [0.011 secs, 4324 KB]Test 6: TEST OK [0.022 secs, 4456 KB]Test 7: TEST OK [0.054 secs, 4976 KB]Test 8: TEST OK [0.130 secs, 6032 KB]Test 9: TEST OK [0.302 secs, 7884 KB]Test 10: TEST OK [0.799 secs, 11684 KB]All tests OK.

Your program ('shuttle') produced all correct answers! This is your submission #2 for this problem. Congratulations!

AC代码:
/*
ID:maxkibb3
LANG:C++
PROB:shuttle
*/#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;int n;
set<string> vis;struct Node {char a[25];int space_idx;vector<int> trace;string get_str() {string s = "";for(int i = 0; i < 2 * n + 1; i++) {if(a[i] == ' ') s = s + '+';else s = s+ a[i];}//cout << s << endl;return s;}bool judge() {if(a[n] != ' ') return false;for(int i = 0; i < n; i++) {if(a[i] != 'B') return false;if(a[n + i + 1] != 'W') return false;}return true;}Node move(int type) {Node ret;for(int i = 0; i <= 2 * n; i++) ret.a[i] = a[i];ret.space_idx = space_idx;ret.trace = trace;if(type == 0) {if(space_idx == 0) {ret.space_idx = -1;return ret;}if(a[space_idx - 1] == 'B') {ret.space_idx = -1;return ret;}swap(ret.a[space_idx], ret.a[space_idx - 1]);ret.space_idx--;ret.trace.push_back(space_idx);}else if(type == 1) {if(space_idx == 2 * n) {ret.space_idx = -1;return ret;}if(a[space_idx + 1] == 'W') {ret.space_idx = -1;return ret;}swap(ret.a[space_idx], ret.a[space_idx + 1]);ret.space_idx++;ret.trace.push_back(space_idx + 2);}else if(type == 2) {if(space_idx < 2) {ret.space_idx = -1;return ret;}if(a[space_idx - 1] == a[space_idx - 2] || a[space_idx - 1] == 'W') {ret.space_idx = -1;return ret;}swap(ret.a[space_idx], ret.a[space_idx - 2]);ret.space_idx -= 2;ret.trace.push_back(space_idx - 1);}else if(type == 3) {if(space_idx > 2 * n - 2) {ret.space_idx = -1;return ret;}if(a[space_idx + 1] == a[space_idx + 2] || a[space_idx + 1] == 'B') {ret.space_idx = -1;return ret;}swap(ret.a[space_idx], ret.a[space_idx + 2]);ret.space_idx += 2;ret.trace.push_back(space_idx + 3);}return ret;}
};
queue<Node> q;void init() {scanf("%d", &n);Node head;for(int i = 0; i < n; i++) {head.a[i] = 'W';head.a[i + n + 1] = 'B';}head.a[n] = ' ';head.space_idx = n;head.trace.clear();q.push(head);
}void solve() {bool find_ans = false;int cnt = 0;while(!q.empty()) {Node head = q.front();q.pop();for(int i = 0; i < 4; i++) {Node new_ele = head.move(i);if(new_ele.space_idx == -1) continue;string str = new_ele.get_str();if(vis.find(str) != vis.end()) continue;vis.insert(str);if(new_ele.judge()) {for(int i = 0; i < new_ele.trace.size(); i++) {if(i % 20 == 0) printf("%d", new_ele.trace[i]);else if(i % 20 == 19) printf(" %d\n", new_ele.trace[i]);else printf(" %d", new_ele.trace[i]);}if(new_ele.trace.size() % 20 != 0) printf("\n");find_ans = true;break;}q.push(new_ele);}if(find_ans) break;}
}int main() {freopen("shuttle.in", "r", stdin);freopen("shuttle.out", "w", stdout);init();solve();return 0;
}

usaco4.4.1 Shuttle Puzzle相关推荐

  1. usaco training 4.4.1 Shuttle Puzzle 题解

    Shuttle Puzzle题解 Traditional The Shuttle Puzzle of size 3 consists of 3 white marbles, 3 black marbl ...

  2. usaco shuttle puzzle(dfs剪枝)

    这题一看我也以为找规律,然后无法下手之后又想到bfs最后看题解是用dfs大神dfs用的出神入化. 不过这题好像可以找规律. /* ID:jinbo wu TASK: shuttle LANG:C++ ...

  3. 学会在Unity中创建一个Match-3益智游戏 Learn To Create a Match-3 Puzzle Game in Unity

    MP4 |视频:h264,1280×720 |音频:AAC,44.1 KHz,2 Ch 语言:英语+中英文字幕(根据原英文字幕机译更准确) |时长:48场讲座(6h 38m) |大小解压后:2.8 G ...

  4. 【杭电ACM】1097 A hard puzzle

    [杭电ACM]1097  A hard puzzle http://acm.hdu.edu.cn/showproblem.php?pid=1097 先用int手写了算法结果竟然wrong answer ...

  5. R语言基于MASS包中的shuttle数据集以及neuralnet包构建神经网络模型

    R语言基于MASS包中的shuttle数据集以及neuralnet包构建神经网络模型 目录 R语言基于MASS包中的shuttle数据集以及neuralnet包构建神经网络模型

  6. Eight puzzle --HOJ 11918

    1.题目类型:模拟.哈希表.BFS. 2.解题思路:(1)模拟Eigh Puzzle的变换方式,并记录在数组中 :(2)由于变换的最终结果相同,所以采用反向的BFS遍历所有情况,并记录所有情况:(3) ...

  7. 补第四周作业总结——8 puzzle

    8 puzzle已经提供了解决思路,早期的人工智能算法A.我只能感觉它的神奇,但是没法创造性地使用它.只能按部就班地完成这周的作业. 难点在于对过程的不理解.这个33的格子搜索算法没有尽头,随着步数的 ...

  8. UVA227 Puzzle

    问题链接:UVA227 Puzzle.基础训练级的问题,用C语言编写程序. 问题简述:一个5×5的网格,一个格子是空的,其他格子各有一个字母,一共有四种指令:A,B,L,R,分别表示把空格上.下.左. ...

  9. UVa10639 Square Puzzle(WA)

    例子通过了,并且udebug上的例子也通过了,但是提交还是错误. 针对特殊情况: 3 4 7 0 2 1 2 2 3 3 2 4 2 4 4 0 4 7 0 0 4 0 4 2 3 2 2 1 1 2 ...

最新文章

  1. PCL基础4:PCLVisualizer可视化窗口显示
  2. 批量图片压缩工具:JPGCompact 2.0绿色版
  3. Star 10.9K!这份Google面试攻略,牛逼了!
  4. 进程调度(第三章 处理调度与死锁)
  5. 震惊,竟然有人用Taro来。。。
  6. python程序后台运行的实现
  7. wenzhixin bootstrap-table 点击table单元格改变颜色
  8. ajax将数据显示在class为content的标签中_利用selenium实现自动翻页爬取某鱼数据
  9. 1.9 编程基础之顺序查找 11 连续出现的字符 python
  10. java五子棋判断_JAVA 五子棋 判断输赢的代码实现
  11. C# 关闭主窗口后让所有线程都停止工作
  12. RK3288_Android7.1基于tinyalsa的音频调试说明
  13. 人脸识别,人脸识别门禁系统的原理
  14. vue2.x+antd-vue搭建后管项目
  15. 北航计算机691,2021考研:北京航空航天大学物理学专业691普通物理综合考试
  16. Premiere CS4无法导出视频
  17. 十 三 弟 你 快 回 来 吧, 皇 帝 四 哥 叫 你 一 起 学 习 大 数 据 呢
  18. 2016年8月28日 星期日 --出埃及记 Exodus 16:29
  19. nodejs 设置API代理
  20. pd.concat()

热门文章

  1. DTV 学习(一) 基本概念、分类
  2. 【Arduino】mega2560 驱动grove 三色水墨屏
  3. 企业领袖必备的八大特质
  4. 人民币贬值之后该买什么
  5. scp或者ssh报错“no matching host key type found. Their offer: ssh-rsa,ssh-dss“
  6. 巧妙使用多个旧路由器无线中继提升网络速度
  7. erdas空间建模_ERDAS空间建模工具介绍.ppt
  8. 在Docker中安装Home Assistant系统(以群晖系统为例)【Home Assistant入门安装篇1-2】
  9. 如何将tomcat注册成windows系统服务方法
  10. TextPad安装环境配置