一 原题

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. Ubuntu 想要更新源 报错 “E: 无法获得锁 /var/lib/dpkg/lock-frontend - open (11: 资源暂时不可用)”
  2. iOS 最新版 CocoaPods 的安装流程
  3. 操作系统原理第九章:虚拟内存
  4. Python NumPy的使用
  5. MyBatis -- 结果集映射
  6. browsersync php,用browserSync吞下4个php
  7. Zynq SOC学习笔记之设备树
  8. double free or corruption的原因
  9. 禁止html文件控件手动输入的方法
  10. CSS 显示风格 appearance属性
  11. react 16 对外暴露function_【第 25 期】React 架构的演变 从同步到异步(一)
  12. Smoothy将于4月22日-27日在BSCPad等4个平台进行IDO
  13. Abra CEO:PayPal满足的比特币购买需求比每天挖出的比特币数量更多
  14. python爬虫步骤-python爬虫步骤 (新手备学 )爬虫编程。
  15. 用JavaScript编写COM组件的步骤
  16. 三位数除以两位数竖式计算没有余数_三位数除以两位数竖式
  17. Java开发基础(四)——dbutils的使用
  18. html table 斜线表头,Table表格加斜线表头
  19. IE5的兼容问题——记录给自己看的
  20. 云端服务器怎么修改密码,云端服务器怎么设置登录密码

热门文章

  1. echart湖南地图
  2. ahri8.php,文件上传处理 - [ php中文手册 ] - 在线原生手册 - php中文网
  3. 地球最后的夜晚 HDTC
  4. w3cschool算法挑战记录1-11(数组截断算法挑战)
  5. 这里有一份元宇宙春节旅游攻略
  6. 农民伯伯android,[同人]墨水儿哥哥乡下小山村的农民伯伯生活.avi
  7. gitlab 502 Whoops, GitLab is taking too much time to respond.
  8. sqlserver清除换行符和回车符\r\n
  9. 安全漏洞中的倚天剑——XSS跨站脚本攻击
  10. 怎样循序渐进、有效地学习JavaScript(转)