Stacks of Flapjacks |
Background
Stacks and Queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. Stacks are also important in the theory of formal languages.
This problem involves both butter and sustenance in the form of pancakes rather than bread in addition to a finicky server who flips pancakes according to a unique, but complete set of rules.
The Problem
Given a stack of pancakes, you are to write a program that indicates how the stack can be sorted so that the largest pancake is on the bottom and the smallest pancake is on the top. The size of a pancake is given by the pancake's diameter. All pancakes in a stack have different diameters.
Sorting a stack is done by a sequence of pancake ``flips''. A flip consists of inserting a spatula between two pancakes in a stack and flipping (reversing) the pancakes on the spatula (reversing the sub-stack). A flip is specified by giving the position of the pancake on the bottom of the sub-stack to be flipped (relative to the whole stack). The pancake on the bottom of the whole stack has position 1 and the pancake on the top of a stack of n pancakes has position n.
A stack is specified by giving the diameter of each pancake in the stack in the order in which the pancakes appear.
For example, consider the three stacks of pancakes below (in which pancake 8 is the top-most pancake of the left stack):
8 7 2 4 6 5 6 4 8 7 8 4 5 5 6 2 2 7
The stack on the left can be transformed to the stack in the middle via flip(3). The middle stack can be transformed into the right stack via the command flip(1).
The Input
The input consists of a sequence of stacks of pancakes. Each stack will consist of between 1 and 30 pancakes and each pancake will have an integer diameter between 1 and 100. The input is terminated by end-of-file. Each stack is given as a single line of input with the top pancake on a stack appearing first on a line, the bottom pancake appearing last, and all pancakes separated by a space.
The Output
For each stack of pancakes, the output should echo the original stack on one line, followed by some sequence of flips that results in the stack of pancakes being sorted so that the largest diameter pancake is on the bottom and the smallest on top. For each stack the sequence of flips should be terminated by a 0 (indicating no more flips necessary). Once a stack is sorted, no more flips should be made.
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int a[35],b[35],n; 6 bool bb; 7 int rd() 8 { 9 int i,j,k,xx; 10 char c; 11 if (scanf("%c",&c)==-1) return 0; 12 xx=c-'0'; 13 while (scanf("%c",&c)&&c!=' '&&c!=' ') 14 xx=xx*10+c-'0'; 15 if (c==' ') bb=0; 16 return xx; 17 } 18 void flip(int x) 19 { 20 int c[35],i; 21 printf("%d ",n-x+1); 22 memcpy(c,a,sizeof(a)); 23 for (i=1;i<=x;i++) 24 a[i]=c[x-i+1]; 25 } 26 int main() 27 { 28 int i,j,k,p,q,x,y,z; 29 bb=1; 30 n=0; 31 while (x=rd()) 32 { 33 while (bb) 34 { 35 a[++n]=x; 36 x=rd(); 37 } 38 a[++n]=x; 39 printf("%d",a[1]); 40 for (i=2;i<=n;i++) 41 printf(" %d",a[i]); 42 printf(" "); 43 memcpy(b,a,sizeof(a)); 44 sort(b+1,b+n+1); 45 for (i=n;i>1;i--) 46 { 47 p=i; 48 while (a[p]!=b[i]) p--; 49 if (i==p) continue; 50 if (p>1) flip(p); 51 flip(i); 52 } 53 printf("0 "); 54 bb=1; 55 n=0; 56 } 57 }
由于没要求求出最优解,所以只要找到一个解即可。
方法是每次找见第i大的元素,如果他不在应该在的位置,先把它翻到顶上(如果已经在顶上就不用了),再翻到它该去的位置。
因为每次翻动都不会影响之下的,所以之前排好的不会重新弄乱。
注意重复元素的处理。找的时候从底下找,可以减少无用的翻动。