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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct
{
char State[26];
int Move;
int Path;
}Shuttle;
FILE *fin,*fout;
char Goal[26];
int Size;
Shuttle Queue[300000];
int Front=0,Rear=0;
Shuttle Now,Next;
int Count=0;
void Initial()
{
int i,j;
for(i=0,j=0;i<Size;i++){
if(j<Size)
Goal[j++]='B';
else
Goal[j++]='W';
if(j==Size){
Goal[j++]=' ';
i=-1;
}
}
}
void Print(Shuttle N)
{
if(Queue[N.Path].Path==-1){
fprintf(fout,"%d",N.Move);
Count++;
}
else{
Print(Queue[N.Path]);
if(Count%20==0){
fprintf(fout,"\n%d",N.Move);
Count=1;
}
else{
fprintf(fout," %d",N.Move);
Count++;
}
}
}
int main()
{
int i,j;
fin=fopen("shuttle.in","r");
fout=fopen("shuttle.out","w");
fscanf(fin,"%d",&Size);
Initial();
for(i=0,j=0;i<Size;i++){
if(j<Size)
Now.State[j++]='W';
else
Now.State[j++]='B';
if(j==Size){
Now.State[j++]=' ';
i=-1;
}
}
Now.Path=-1;
Queue[Rear].Path=Now.Path;
strcpy(Queue[Rear++].State,Now.State);
while(Front!=Rear){
strcpy(Now.State,Queue[Front++].State);
if(strcmp(Now.State,Goal)==0)
break;
for(i=0;i<Size*2+1;i++){
strcpy(Next.State,Now.State);
if(Next.State[i]=='W'){
if(i+1<2*Size+1&&Next.State[i+1]==' '){
Next.State[i+1]='W';
Next.State[i]=' ';
Queue[Rear].Move=i+1;
Queue[Rear].Path=Front-1;
strcpy(Queue[Rear++].State,Next.State);
}
if(i+2<2*Size+1&&Next.State[i+1]=='B'&&Next.State[i+2]==' '){
Next.State[i+2]='W';
Next.State[i]=' ';
Queue[Rear].Move=i+1;
Queue[Rear].Path=Front-1;
strcpy(Queue[Rear++].State,Next.State);
}
}
if(Next.State[i]=='B'){
if(i>=1&&Next.State[i-1]==' '){
Next.State[i-1]='B';
Next.State[i]=' ';
Queue[Rear].Move=i+1;
Queue[Rear].Path=Front-1;
strcpy(Queue[Rear++].State,Next.State);
}
if(i>=2&&Next.State[i-1]=='W'&&Next.State[i-2]==' '){
Next.State[i-2]='B';
Next.State[i]=' ';
Queue[Rear].Move=i+1;
Queue[Rear].Path=Front-1;
strcpy(Queue[Rear++].State,Next.State);
}
}
}
}
Print(Queue[Front-1]);
fprintf(fout,"\n");
return 0;
}
用了BFS。感觉代码又写烂了。。
不过空间居然没有超。。。数据太弱?