【算法练习】递归 百练poj2275:Flipping Pancake 翻煎饼

题目链接:http://bailian.openjudge.cn/practice/2275

注意要用STL模板的话,reverse 

#include<algorithm>

2275:Flipping Pancake

总时间限制: 

1000ms

内存限制: 

65536kB

描述

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

反正这个思路我是完全照着别人写的

https://www.cnblogs.com/mjc467621163/archive/2011/07/22/2114080.html

还是有点不明觉厉!

从idx=N开始循环 1-N 找最大的  ;

在最大的左边找次大的;

如果次大的在最大的左边就给他翻转到最大左边;

翻转最大的到本次idx最后;

idx--

AC代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
int N,idx,b1,b2;  //b1代表当前最大数的index  b2代表次大数的index
int list[50];
vector<int> ans;
//就是翻转1~x  两边都是闭区间
//reverse函数是左闭右开
void Flip(int x){
    reverse(list+1,list+x+1);
    ans.push_back(x);
}

int main(){
    while(cin>>N){
        if(N==0)
            break;

        ans.clear();   //初始化翻转的答案 vector
        //从1开始计数
        for(int i=1;i<=N;i++){
            cin>>list[i];   //用数组形式读入
        }


        //每一层循环都使得当前最大数翻转到当前的最下面
        for(idx=N;idx>1;idx--){
            if(list[idx]!=idx){ //如果当前最后一个数不是最大数
                //找到当前最大数
                for(b1=1;b1<=idx;b1++){
                    if(list[b1]==idx)
                        break;
                }
                int flag=0;
                //在最大数的左边找次小的数
                for(b2=b1-1;b2>=1;b2--){
                    if(list[b2]==idx-1){
                        flag=1;
                        break;
                    }
                }
                //如果次小的数在最大数的左边
                if(flag){
                    if(b2!=1){
                        Flip(b2);
                    }
                    Flip(b1-1);  //翻转到最大的左边
                }
                if(b1!=1){
                    Flip(b1);
                }
                Flip(idx);

            }

        }
        cout<<ans.size()<<" ";
        for(int i=0;i<ans.size();i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40760678/article/details/100552882