【递归】百练2275 九度1146 Flipping Pancake(翻饼子) (递归、游戏)

题目描述:

    We start with a stack n of pancakes of distinct sizes. The problem is to convert the stack to one in which the pancakes are in size order with the smallest on the top and the largest on the bottom. To do this, we are allowed to flip the top k pancakes over as a unit (so the k-th pancake is now on top and the pancake previously on top is now in the k-th position).

    For example: This problem is to write a program, which finds a sequence of at most (2n - 3) flips, which converts a given stack of pancakes to a sorted stack. 

输入:

    Each line of the input gives a separate data set as a sequence of numbers separated by spaces. The first number on each line gives the number, N, of pancakes in the data set. The input ends when N is 0 (zero) with no other data on the line. The remainder of the data set are the numbers 1 through N in some order giving the initial pancake stack.

    The numbers indicate the relative sizes of the pancakes. N will be, at most, 30. 

输出:

     For each data set, the output is a single-space separated sequence of numbers on a line. The first number on each line, K, gives the number of flips required to sort the pancakes. This number is followed by a sequence of K numbers, each of which gives the number of pancakes to flip on the corresponding sorting step. There may be several correct solutions for some datasets. For instance 3 3 2 3 is also a solution to the first problem below. 

样例输入:
3 1 3 2
5 4 3 2 5 1
0
样例输出:
3 2 3 2 
3 3 4 5 
来源:
2009年北京大学计算机研究生机试真题

思路:

著名的翻饼子游戏。要求最后状态是小饼在大饼的上面,求翻得次数。最多需要翻2n-3次。

这种题要用逆向思维以及递归方法来分析,考察最终状态及前一步,逐步往前考察,从而发现游戏思路。


!!!!他有很多种解法,求出来一种就行,之前用输出去对,结果,理解的不是很好,其实你自己理解的很正确!!! 
因为问题只要求2n-3次,并没有别的要求,所以
最简单一个思想就是每一次循环完成一个目标就是把当前最大的放到最后,
那么第一步就是先经过旋转把当前最大的放在数组最前面,第二步就是旋转到对应位置
样例输入
3 1 3 2
5 4 3 2 5 1
0
样例输出
3 2 3 2
3 3 4 5


/*
*/
#include <stdio.h>
using namespace std;
void change(int num,int *A,int m)
{
    int left=1,tmp;
    while(left<m)
    {
        tmp=A[left];
        A[left]=A[m];
        A[m]=tmp;
        left++;
        m--;
    }
}
void display(int *A,int num)
{
    for(int i=1;i<=num;i++)
        printf("%d ",A[i]);
    printf("\n");
}
int main()
{
    //freopen("data.in","r",stdin);
    int num,A[31],B[100];
    while(scanf("%d",&num)!=EOF&&num!=0)
    {
        int index=0;
        for(int i=1;i<=num;i++)
            scanf("%d",&A[i]);
        for(int i=num;i>=1;i--)//找最大的;
        {
            if(A[i]==i)//如果已经在最后了,那么就不需要旋转;
                continue;
            if(A[1]!=i)//如果目前最大的不在最前,则需要旋转一次,把最大的放到第一个;
            {
                for(int j=2;j<=num;j++)
                {
                    if(A[j]==i)
                    {
                        change(num,A,j);
                        //display(A,num);
                        B[index++]=j;
                    }
                }
            }
            change(num,A,i);
            //display(A,num);
            B[index++]=i;
        }
        printf("%d",index);
        for (int i=0;i<index;i++)
        {
            printf(" %d",B[i]);
        }
        printf("\n");
    }
    return 0;
}
/**************************************************************
    Problem: 1146
    User: vincent_ynh
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1020 kb
****************************************************************/

猜你喜欢

转载自blog.csdn.net/momo_mo520/article/details/80790503