编程介的小学生 2017-02-22 05:21 采纳率: 20.5%
浏览 789
已采纳

Two-Stacks Solitaire

Card games for one player are called Patience in Britain and Solitaire in the United States. One very difficult (some say maddening) solitaire game is called Two-Stacks and has the following rules:

Tableau

The tableau consists of a stock pile, two intermediate piles and one foundation pile.

Cards

A game may use up to four complete decks, or parts of those four decks. A complete 
deck contains 52 cards; since all cards in a deck can be ordered, in this description we 
will forget about faces and suits and we will use the numbers from 1 to 52 to represent 
the cards in a deck.

Dealing

Once the cards to be used in the game have been chosen, they are dealt face up in the 
tableau, one on top of the other, forming the stock pile.

Moving cards

Cards may be moved one at a time. A card can be moved from the stock pile to one 
of the intermediate piles, or from one intermediate pile to the foundation pile. In a pile 
(stock or intermediate) only the topmost card can be moved, although all the others are 
visible.

Object

The goal is to have all cards used in the game in non-decreasing order, from bottom to 
top, constituting the foundation pile.

As you may have noticed by now, even when a solution exists, chances of winning a Two-Stacks solitaire game are very low. But your grandmother has just learned the game and loved it. She has asked you to help her to learn playing the game, by writing a program that would coach her through her first tries, showing which movements to make.

Input

The input consists of several test cases. The first line of a test case contains a single integer N (1 <= N <= 208), representing the number of cards in the game. The second line of a test case contains a sequence of N integers (between 1 and 52), separated by single blank spaces, representing the cards. The cards will be dealt in the order they appear in the input, so that the topmost card in the stock pile is the Nth card in the line. Notice that each number from 1 to 52 will appear at most 4 times in each test case. The end of input is indicated by a test case with N = 0.

Output

For each test case in the input your program must produce an answer. The first line of an answer must contain a test case identifier, in the form '#i' where i starts from 1 and is incremented for every test case. Then, if it is possible to win the game, print a sequence of movements to win the game. Each movement must be described in a separate line, in the form push x' orpop x' where x' is 1 or 2;push x' moves the topmost card from the stock pile to intermediate pile x, and pop x' moves the topmost card from intermediate pile x to the foundation pile. If more than one solution exists, print any one. If it is not possible to win the game, print a line with the wordimpossible'.

Sample Input

4
4 1 3 2
4
1 4 3 2
4
1 2 2 2
0
Sample Output

#1
push 1
push 2
push 1
pop 1
pop 1
push 1
pop 2
pop 1
#2
impossible
#3
push 1
push 2
push 2
push 2
pop 1
pop 2
pop 2
pop 2

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-02-28 14:31
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏