一 原题
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
二 分析
三 代码
USER: Qi Shen [maxkibb3] TASK: shuttle LANG: C++ Compiling... Compile: OK Executing... 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!
/*
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;
}