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 |
TLE ! 有哪位帮我看看 ??#include <iostream> using namespace std; struct node{ char a[7]; }; node stack[6000]; int head,tail,ttail,step; node last,first,impossible; bool check(node b,node c) { for(short i=0;i<7;i++) if(b.a[i]!=c.a[i]) break; if(i<7) return 0; return 1; } bool checknotinstack(node d) { for(int i=0;i<tail;i++) if(check(d,stack[i])) return 0; return 1; } short findnull(node d) { for(short i=0;i<7;i++) if(d.a[i]=='0') return i; } void addnode(node d) { node c; short t=findnull(d); if(t==0) { c=d; swap(c.a[0],c.a[2]); if(checknotinstack(c)) stack[tail++]=c; c=d; swap(c.a[0],c.a[4]); if(checknotinstack(c)) stack[tail++]=c; c=d; swap(c.a[0],c.a[6]); if(checknotinstack(c)) stack[tail++]=c; } else if(t%2==0) { c=d; swap(c.a[t],c.a[(t+1)%6]); if(checknotinstack(c)) stack[tail++]=c; c=d; swap(c.a[t],c.a[t-1]); if(checknotinstack(c)) stack[tail++]=c; c=d; swap(c.a[t],c.a[0]); if(checknotinstack(c)) stack[tail++]=c; } else { c=d; swap(c.a[t],c.a[t+1]); if(checknotinstack(c)) stack[tail++]=c; if(t==1) { c=d; swap(c.a[t],c.a[6]); if(checknotinstack(c)) stack[tail++]=c; } else { c=d; swap(c.a[t],c.a[t-1]); if(checknotinstack(c)) stack[tail++]=c; } } } void main() { int n,cur; for(short i=0;i<7;i++) { last.a[i]=i+48; impossible.a[i]=i+48; } //for(i=0;i<7;i++) // cout<<last.a[i]; swap(impossible.a[5],impossible.a[6]); cin>>n; while(n--) { for(i=0;i<7;i++) cin>>first.a[i]; step=head=0; tail=1; stack[0]=first; bool sign=1; while(sign) { ttail=tail; for(cur=head;cur<ttail;cur++) { if(check(stack[cur],last)) { cout<<step<<endl; sign=0; break; } if(check(stack[cur],impossible)) { cout<<"-1"<<endl; sign=0; break; } } while(head<ttail) { addnode(stack[head]); head++; } step++; } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator