Poj 2023 Choose Your Own Adventure (Data Structure + Deep Search)

Source: Internet
Author: User
Tags truncated
Choose Your Own Adventure
Time Limit:1000 MS   Memory Limit:30000 K
Total Submissions:1559   Accepted:638

Description

After reading the book Tim and Marc Kill Kenny about every ty zillion times, James decided he 'd had it with choose-your-own-adventure stories. no matter what choices he made, it seemed like Kenny always fell down an abandoned mine shaft, got run over by a bus load of nuns, or was messily incluured by stray cats. james eventually found the page with the happy ending (where Kenny saves himself by trapping Tim and Marc between the pizza and the hungry programmers) by flipping through the book, but he can't figure out how to get there by following the rules. luckily, he owns a C compiler...

Input

Input to this problem will consist of a (non-empty) series to up to 100 data sets, each representing a choose-your-own-adventure story. each data set will be formatted according to the following description, and there will be no blank lines separating data sets.

The first line contains a single integer n indicating the number of data sets.

A single data set has 2 components:

  1. Page Count-A line containing a single integer X, where 1 <X <100, indicating the number of pages in the story.
  2. Page List-A sequence of X lines, each of which represents
    A page from the book. Each line has the following components separated
    From one another by single spaces:
    • Line type-A single character indicating what type of line
      This is. It will represent either a "C" choice page, or an "E" end page.
      Page 1 is always a choice page.
    • Text-A string of text surrounded by double quotes.
      Including the quotes, this component will not exceed 256 characters.
      Quotes are given for input purposes only and shocould not be considered
      Part of the text. The text will not contain in embedded double quotes.
    • Choices-Two positive integers from 1 to X indicating
      Pages where the reader can go from this page. Only choice pages have
      This component.
    • Ending Type-Either the text "HAPPY" or "GRISLY". There
      Will only be one happy ending per story, and only end pages have this
      Component.

Output

For each story in the input:
  1. Output a single line, "STORY #" where # is 1 for the first story, 2 for the second story, etc.
  2. Determine the story that begins on page 1 and ends on
    Happy ending page. Output the text of this story, printing one "page"
    Text per line. Note that there is only one such story for each data
    Set.

Sample Input

23C "Arrived at LSU for the contest" 2 3E "Was devoured by sidewalk ants" GRISLYE "Won the contest. Received glory and nachos." HAPPY5C "Saw a peanut" 3 5E "Made peanut butter sandwich" HAPPYC "Found a hammer" 4 2E "Hit self on head with hammer, ouch!" GRISLYE "Ate the peanut, choked on it, and died" GRISLY

Sample Output

STORY 1 Arrived at LSU for the contestWon the contest. initialized ed glory and nachos. STORY 2Saw a peanutFound a hammerMade peanut butter sandwich

Meaning understanding:
(1) Each test group has only one result, that is, it can exit after the search results are found.
(2) Each time the search starts from the first page, you can start from the first page instead of looking for the search location.
(4) Each string is a story. The front side "C" indicates that there are two integers behind it. It is the page number of the next page that can be read. The index value of the number of rows indicates the page number, and "E" indicates that this is the end page.
(5) happy is the exit condition to be searched. It is a perfect ending. If it is not happy, a superior will be returned for the new search.

Source

# Include <iostream> # include <stdlib. h> # include <stdio. h> using namespace std; // definition Page, which stores the input row information typedef struct {int next1, next2; char type; string text; string end;} Page; page page [101]; // indicates whether the Page number has been read. 0 indicates that it has not been read, and 1 indicates that it has read int mark [101]. // storage path, if the value is 0, int path [101] is reached. void init () {for (int I = 0; I <101; I ++) {page [I]. end = ""; page [I]. text = ""; page [I]. next1 = 0; page [I]. next2 = 0; mark [I] = 0; path [I] = 0 ;}/// Deep Search, P indicates the index of the page number to be read on the next page, and k indicates the path length. The number of pages read is k + 1 bool dfs (int p, int k) on each page added) {// if read, the upper-level if (mark [p] = 1) return false; // record path, the k path is the P page path [k] = p; // mark this page. mark [p] = 1; // The end of the story, returns true if (page [p]. type = 'E' & page [p]. end. compare ("HAPPY") = 0) return true; if (page [p]. type = 'C') {// This path returns true if it is successful, because each group of data has only one ending, so it is returned after it is found, and no need to search again. If (dfs (page [p]. next1, k + 1) return true; if (dfs (page [p]. next2, k + 1) return true;} // This path does not have a perfect ending, this path returns the initial value, marked as unread, path reset 0 path [k] = 0; mark [p] = 0; return false;} // print void print () {int I = 1; while (path [I]! = 0) {cout <page [path [I]. text <endl; I ++ ;}int main () {// Number of test data groups int n; // Number of pages int x; // page number int page1, page2; // content string text, end; // number of rows string line; cin> n; for (int m = 1; m <= n; m ++) {cin> x; // carriage return getchar (); // initialize init (); for (int I = 1; I <= x; I ++) {// read a row at a time getline (cin, line); // if it is C, it is initialized according to the format of line C. The string is truncated first, put it in a struct if (line [0] = 'C') {page1 = atoi (line. substr (line. find_last_of ("\" ") + 2, 1 ). c_str (); page2 = atoi (line. substr (line. find_last_of ("\" ") + 4, 1 ). c_str (); text = line. substr (line. find_first_of ("\" ") + 1, line. find_last_of ("\" ")-line. find_first_of ("\" ")-1); page [I]. next1 = page1; page [I]. next2 = page2; page [I]. text = text; page [I]. type = 'C';} else if (line [0] = 'E') {// if it is E, it is initialized in line E format. The string is truncated first, put text = line in a struct. substr (line. find_first_of ("\" ") + 1, line. find_last_of ("\" ")-line. find_first_of ("\" ")-1); end = line. substr (line. find_last_of ("\" ") + 2, line. length ()-line. find_last_of ("\" "); page [I]. end = end; page [I]. text = text; page [I]. type = 'E';} // clear the stream and content. Note: otherwise, the content will be duplicated after reading the text line = ""; cin. clear () ;}// search for mark [1] = 1; path [1] = 1 from the first page of the book; cout <"STORY" <m <endl; if (dfs (page [1]. next1, 2) print (); else {if (dfs (page [1]. next2, 2) print () ;}} return 0 ;}

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.