USACO SECTION 4.4 Shuttle Puzzle

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

 

#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。感觉代码又写烂了。。

不过空间居然没有超。。。数据太弱?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值