Poj 2023 choose your own adventure has been tested in three methods: extensive queue search, deep stack search, and recursive deep search.

Source: Internet
Author: User

Enter an integer n to indicate that there are N groups of test data,

Enter an integer x in the first row of each group to indicate that the group has a total of X pages for testing. Then, enter x rows. Each row indicates a page,

Each page may start with "C" or "E" and enclose a piece of text in quotation marks. Note that the first line must start with "C" and two numbers (a) should be entered at the end of "C, B indicates that after this page is complete, you can jump to page a or page B,

Enter a word at the end starting with E, either happy or grisly (there is only one page ending with happy ),

Now you need to find the path starting with "C" and ending with "E" and ending with "happy" at the end of "E" line based on the input. The content in the quotation marks in each page of the output path does not contain quotation marks.

Extensive search by queue:

# Include <stdio. h> # include <string. h> char STR [150] [300]; // The original array name is story, and the result cannot run normally. Is story a reserved character of the system? Reply to int queue [150], pre [150], re [150], FF, TT, AA, BB; // pre [I] The ancestor int main () {int t, x, I, Len, Count = 0, J; scanf ("% d ", & T); While (t --) {count ++; scanf ("% d", & X); getchar (); for (I = 1; I <= X; I ++) gets (STR [I]); // reads the content queue [0] = 1; // enters the memset (PRE, 0, sizeof (pre); FF = TT = 0; // FF indicates the opposite TT indicates the end while (1) {Len = strlen (STR [queue [ff]); if (STR [queue [ff] [0] = 'C') {AA = STR [queue [ff] [len-3]-'0 '; // retrieve the following number BB = STR [queue [ff] [len-1]-'0'; If (pre [AA ] = 0) // if its ancestor is 0, it indicates that the team has not yet joined, then it is like the team {queue [++ TT] = AA; pre [AA] = queue [ff]; // records the current row number as the ancestor} If (pre [BB] = 0) // same as {queue [++ TT] = BB; Pre [BB] = queue [ff];} FF ++; // current line out} else {If (STR [queue [ff] [len-2] = 'P') // if it ends with happy, jump out of break directly; else FF ++ ;}} memset (Re, 0, sizeof (re); Re [0] = queue [ff]; // the last row I = 1; while (1) // trace the path {re [I] = pre [re [I-1]; If (Re [I] = 0) break; else I ++ ;}j = I-1; printf ("story % d \ n", count); For (; j> = 0; j --) {for (I = 3; STR [re [J] [I]! = '"'; I ++) printf (" % C ", STR [re [J] [I]); printf (" \ n ");}} return 0 ;}

Deep stack search:

After reading a brother's recursive deep search (attached below), I practiced deep search with stacks.

# Include <stdio. h> # include <string. h> struct {char C, text [260], end [10]; int A, B;} PAG [102]; // Save the content of each page, Page C type; text page content; end ending; a and B point to struct {int num [150], top;} Zhan; // stack int X; int Fang (int) // check whether it has been accessed to prevent loop {int I; for (I = 1; I <Zhan. top; I ++) if (a = Zhan. num [I]) return 1; return 0;} void shuru () // input {int K, J; scanf ("% d", & X ); for (j = 1; j <= x; j ++) {getchar (); PAG [J]. C = getchar (); For (k = 0; 1; k ++) {PAG [J]. text [k] = getchar (); If (PAG [J]. text [k] = '"') {PAG [J]. text [k] = 0; break;} getchar (); If (PAG [J]. C = 'C') {scanf ("% d", & PAG [J]. a, & PAG [J]. b);} else {scanf ("% s", PAG [J]. end) ;}} void DFS () // Deep Search {Zhan. top =-1; memset (Zhan. num, 0, sizeof (Zhan. num); Zhan. num [++ Zhan. top] = 1; while (1) {If (Fang (Zhan. num [Zhan. top]) {Zhan. top --;} If (PAG [Zhan. num [Zhan. top]. C = 'E') {If (PAG [Zhan. num [Zhan. top]. end [0] = 'H') break; else {Zhan. top -- ;}} else {If (0 = Zhan. num [Zhan. top + 1]) // if the previous stack is zero, it indicates the first access to this stack. top ++; Zhan. num [Zhan. top] = PAG [Zhan. num [Zhan. top-1]. a;} else if (PAG [Zhan. num [Zhan. top]. A = Zhan. num [Zhan. top + 1]) // if the previous stack is A, that is, a is accessed, B {Zhan. top ++; Zhan. num [Zhan. top] = PAG [Zhan. num [Zhan. top-1]. b;} else // otherwise, both A and B have been accessed and the page does not work. The output stack {Zhan. top -- ;}}} void shuchu (int I) // The access route from the bottom of the stack to the top of the stack is output in turn {printf ("story % d \ n ", i); for (I = 0; I <= Zhan. top; I ++) {puts (PAG [Zhan. num [I]. text) ;}} int main () {int T, I; scanf ("% d", & T); for (I = 1; I <= T; I ++) {shuru (); DFS (); shuchu (I);} return 0 ;}

Deep recursive search:

#include<stdio.h>#include<string.h>#include<stdlib.h>struct story{char ch;char text[260];int ch1,ch2;char end[10];}st[102];int t,n,pos,flag;int mark[102];int ans[102];void solve(int num);void dfs(int p,int d);int main(){int i,j,k;scanf("%d",&t);for(i=0;i<t;i++){scanf("%d",&n);  for(j=0;j<n;j++){getchar();scanf("%c",&st[j].ch);if(st[j].ch=='C'){getchar();st[j].text[0]=getchar();for(k=1;;k++){st[j].text[k]=getchar();if(st[j].text[k]=='"')break;}scanf("%d%d",&st[j].ch1,&st[j].ch2);}else{getchar();st[j].text[0]=getchar();for(k=1;;k++){st[j].text[k]=getchar();if(st[j].text[k]=='"')break;}scanf("%s",st[j].end);}}solve(i+1);}return 0;}void solve(int num){int i,j;memset(mark,0,sizeof(mark));flag=0;dfs(0,0);printf("STORY %d\n",num);for(i=0;i<=pos;i++){for(j=1;;j++){if(st[ans[i]].text[j]=='"')break;printf("%c",st[ans[i]].text[j]);}printf("\n");}}void dfs(int p,int d){if(flag)return ;if(st[p].ch=='E'){if(!strcmp(st[p].end,"HAPPY")){flag=1;ans[d]=p;pos=d;}elsereturn ;}else{if(!mark[st[p].ch1]){mark[st[p].ch1]=1;dfs(st[p].ch1-1,d+1);if(flag){ans[d]=p;return;}}if(!mark[st[p].ch2]){mark[st[p].ch2]=1;dfs(st[p].ch2-1,d+1);if(flag){ans[d]=p;return;}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.