OJ_1146 Flipping Pancake

#include <iostream>
using namespace std;
const int N=31;
int a[N];
int maxstep=2*N;
int result[2*N];// 最多2*(N-1) 
int temp[2*N];

void flip(int k)
{
     for(int i=1,j=k;i<j;i++,j--)
     {
             int t=a[i];
             a[i]=a[j];
             a[j]=t;
     }
}
bool isOk(int n)
{
     bool flag=true;
     for(int i=1;i<=n;i++)
     {
             if(a[i]!=i){flag=false;
                                    break;
                         }
     }
     return flag;
}
void getresult(int step,int n)
{
     if(isOk(n))
     {
                if(step>maxstep)
                {
                                return;
                }else if(step<maxstep)
                {
                  maxstep=step;
                  for(int i=1;i<=maxstep;i++)
                  {
                          result[i]=temp[i];
                  }
                      return;
                }
                     
                 
     }else{
         for(int i=2;i<=n;i++)
         {
                 if(step>0&&temp[step-1]==i)
                                            continue;
                 flip(i);
                 temp[step]=i;
                 getresult(step+1,n);
                 flip(i);
                 
         }
     }
}
void func()
{
     int n;
     while(cin>>n)
     {

                  if(n==0)break;
                  
                  for(int i=1;i<=n;i++)
                  {
                          cin>>a[i];
                  }
                  maxstep=2*n;
                  getresult(0,n);
                  cout<<maxstep;
                  for(int i=1;i<=maxstep;i++)
                  {
                          cout<<" "<<result[i];
                  }
                  cout<<endl;
                  
     }
     
}
int countway=0;
void getmaxfirst(int n)// 优先翻转出大的方法 
{
     for(int i=n;i>=1;i--)
     {
             if(a[i]==i)continue;
             for(int j=2;j<i;j++)
             {
                     if(a[j]==i)//  找到当前最大的,翻转到第1 
                     {
                                    flip(j);
                                    result[countway++]=j;
                                    break;
                     } 
                     
             }
             flip(i);// 把第一最大的翻转到底部
             result[countway++]=i; 
     }
}

void func1()
{
       int n;
     while(cin>>n)
     {

                  if(n==0)break;
                  
                  for(int i=1;i<=n;i++)
                  {
                          cin>>a[i];
                  }

                  countway=0;
                  getmaxfirst(n);
                  cout<<countway;
                  for(int i=0;i<countway;i++)
                  {
                          cout<<" "<<result[i];
                  }
                  cout<<endl;
                  
     }
}
int main(int argc, char *argv[])
{
    
	//printf("Hello, world\n");
	func1();
	return 0;
}


题目描述:

    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 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值