Shuttle Puzzle棋盘游戏

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

大小为3的棋盘游戏里有3个白色棋子,3个黑色棋子,和一个有7个格子一线排开的木盒子。3个白棋子被放在一头,3个黑棋子被放在另一头,中间的格子空着。

初始状态: WWW_BBB 目标状态: BBB_WWW

在这个游戏里有两种移动方法是允许的:

  1. 你可以把一个棋子移到与它相邻的空格;
  2. 你可以把一个棋子跳过一个(仅一个)与它不同色的棋子到达空格。

大小为N的棋盘游戏包括N个白棋子,N个黑棋子,还有有2N+1个格子的木盒子。

这里是3-棋盘游戏的解,包括初始状态,中间状态和目标状态:

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

请编一个程序解大小为N的棋盘游戏(1 <= N <= 12)。要求用最少的移动步数实现。

格式

PROGRAM NAME: shuttle

INPUT FORMAT:

(file shuttle.in)

一个整数N。

OUTPUT FORMAT:

(file shuttle.out)

用空格在棋盘的位置(位置从左到右依次为1, 2, ..., 2N+1)表示棋盘的状态。输出棋盘的状态变换序列,每行20个数(除了最后一行)。

输出的解还应当有最小的字典顺序(即如果有多组移动步数最小的解,输出第一个数最小的解;如果还有多组,输出第二个数最小的解;...)。

SAMPLE INPUT

SAMPLE OUTPUT

3 5 6 4 2 1 3 5 7 6 4 2 3 5 4
构造法吧,我也是看着北极天南星的这个图模拟的。

这道题的规律在n=3时不容易发现,我们不妨把n扩大,然后寻找规律。

n=5时:

WWWWW BBBBB

WWWW WBBBBB

WWWWBW BBBB

WWWWBWB BBB

WWWWB BWBBB

WWW BWBWBBB

WW WBWBWBBB

WWBW WBWBBB

WWBWBW WBBB

WWBWBWBW BB

WWBWBWBWB B

WWBWBWB BWB

WWBWB BWBWB

WWB BWBWBWB

W BWBWBWBWB

 WBWBWBWBWB

BW WBWBWBWB

BWBW WBWBWB

BWBWBW WBWB

BWBWBWBW WB

BWBWBWBWBW

 

BWBWBWBWB W

BWBWBWB BWW

BWBWB BWBWW

BWB BWBWBWW

B BWBWBWBWW

BB WBWBWBWW

BBBW WBWBWW

BBBWBW WBWW

BBBWBWBW WW

BBBWBWB WWW

BBBWB BWWWW

BBB BWBWWWW

BBBB WBWWWW

BBBBBW WWWW

 

Special Step:

BBBBB WWWWW

USACO题解:

Examine the form the sample output gives:


The black squares that outline the moved marble in the
sample solution form a pattern:



There are two parts of the pattern, the expanding portion
and the contracting portion. The boundary of the movement expands by one each
pass in the expanding portion, and contracts by one each pass in the
contracting portion. In each pass, the selection moves two until it reaches the
other end of the boundary, and then goes moves to the new boundary. This
pattern can be calculated and outputted to produce the optimal answer.



Since each step can be computing in constant time, this algorithm should
run easily within time.

转载于:https://www.cnblogs.com/start07/archive/2009/04/08/1432076.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值