"Buaa 591" the last Alpha Star
DFS output conforming path after binary tree achievement
The main pit in the achievements ... It's not enough to pay some gods.
Code
#includeUsing namespace std;typedef struct node node;typedef struct node*nd; typedef struct node{intData Nd r,l;} Node;intD,tp,f;intnum[5555];bool Settree (Nd*t){ (*t) = (Nd) malloc (sizeof (Node)); Char ch;int x=0, F =0; ch = getchar (); while(ch = =' \ n '|| ch = =' \ r ') ch = getchar (); while(Ch >=' 0 '&& CH <=' 9 ') {f =1;x=x*10+ch-' 0 '; ch = getchar (); }if(f) {(*t)->data =x; }Else(*t) = NULL;//cout<<tp<<" "<<x<<endl;if(ch = =' (') {tp++;if(Settree (& (*t)) (->l)) GetChar ();if(Settree (& (*t)) (->r)) GetChar (); tp--;return 1; }Else{if(f) {(*t)->l = NULL; (*t)->r = NULL; }return 0; }}void Dfs (intSum,nd t) {//cout<<sum<<endl;intIif(T->l = = NULL && T->r = = NULL && sum = = d) {f =1; for(i =0; I < Tp;++i) {if(i)printf(" ");printf("%d", Num[i]); }printf("\ n"); }Else if(Sum > D)return;if(t->l) {num[tp++] = t->l->data; Dfs (sum+num[tp-1],T->L); tp--; }if(T->r) {num[tp++] = t->r->data; Dfs (sum+num[tp-1],T->R); tp--; }}intMain () {//freopen("In.txt","R", stdin);intT,z =0; scanf"%d", &t); Char ch; while(t--) {d =0; NdTR; Settree (&TR); ch = getchar (); TP =0; while(ch = =' \ n ') ch = getchar (); while(ch! =' \ n '&& ch! =' \ r ') {d = d*10+ch-' 0 '; ch = getchar (); } TP = f =0;printf("Case %d: \ n", ++z); num[tp++] =TR->data; Dfs (num[0],TR);if(!f)printf("I am so sorry!\n"); }return 0;}
"Buaa 591" the last Alpha Star