USACO 4.4 Shuttle Puzzle

时间:2024-10-27 10:36:08

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 ;
}