Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
WA了几次,终于过了,,,发现自己的代码比较短,,,做好人,,代码留下,,不到1000B,,并留点测试数据,,随便改的,,#include <iostream> #include <string> using namespace std; const int N=25; int p[N],c[N],num[N],top[N],down[N]; //p->parent,,下边的积木,,c->child,,上面的积木,,num,,积木i所在的堆的号码,,top,,这个堆最上边的积木,,down,,这个堆最下面的积木,,,不存在时都为-1,,, void move(int a) { top[num[a]]=a; int x=c[a]; c[a]=-1; for(;x!=-1;x=c[x]) { num[x]=x; p[x]=-1; if(top[x]==-1) top[x]=x; c[x]=down[x]; if(down[x]!=-1) p[down[x]]=x; down[x]=x; } } int main() { int n,i; scanf("%d",&n); for(i=0;i<n;i++) top[i]=num[i]=down[i]=i,p[i]=c[i]=-1; string s1,s2; int a,b; while(cin>>s1&&s1!="quit") { cin>>a>>s2>>b; if(num[a]==num[b]) continue; if(s1=="move") move(a); if(s2=="onto") move(b); int na=num[a],nb=num[b]; for(int x=a;x!=-1;x=c[x]) num[x]=nb; int tmp=top[nb]; top[nb]=top[na]; top[na]=p[a]; if(p[a]==-1) down[na]=-1; else c[p[a]]=-1; p[a]=tmp; c[tmp]=a; } for(i=0;i<n;i++) { printf("%d:",i); for(int x=down[i];x!=-1;x=c[x]) printf(" %d",x); printf("\n"); } return 0; } 10 move 9 onto 1 move 8 over 1 move 7 onto 1 move 6 over 1 pile 9 over 6 pile 8 over 5 move 7 over 1 move 4 over 9 quit 0: 0 1: 1 7 6 9 4 2: 2 3: 3 4: 5: 5 8 6: 7: 8: 9: Press any key to continue Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator