usaco4.4.1 Shuttle Puzzle

来源:互联网 发布:淘宝怎么找代理商 编辑:程序博客网 时间:2024/06/02 18:12

一 原题

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 BBBWW WBBBWWBW BBWWBWB BWWB BWBW BWBWB WBWBWBBW WBWBBWBW WBBWBWBW BWBWB WBWB BWWB BWBWWBB WBWWBBBW WWBBB 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: shuttleLANG: 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 yoursubmission #2 for this problem. Congratulations!


AC代码:
/*ID:maxkibb3LANG: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;}


0 0
原创粉丝点击