Poj 2023 Choose Your Own Adventure

Choose Your Own Adventure
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1738 Accepted: 711

Description

After reading the book Tim and Marc Kill Kenny about fifty 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 devoured 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 of 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. The quotes are given for input purposes only and should not be considered part of the text. The text will not contain embedded double quotes. 
    • Choices - Two positive integers from 1 to X indicating the 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 the happy ending page. Output the text of this story, printing one "page" of text per line. Note that there is only one such story for each data set.

Sample Input

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

Sample Output

STORY 1
Arrived at LSU for the contest
Won the contest. Received glory and nachos.
STORY 2
Saw a peanut
Found a hammer
Made peanut butter sandwich

Source

South Central USA 2004

思路:从最后面拿数字,然后判断是不是到了终点"HAPPY",是的话就输出,不是就继续查找,BFS

AC代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cstdlib>
using namespace std;
const int maxn=200;
char  str[maxn][maxn+100];
int path[maxn],vis[maxn];

void dis(int n){//递归输出

   if(n!=1)
    dis(path[n]);
   for(int i=3;str[n][i]!='"';i++)
    cout<<str[n][i];
   cout<<endl;
}

void bfs(){
    queue<int> Q;
    while(!Q.empty()) Q.pop();
    memset(vis,0,sizeof(vis));
    Q.push(1);
    char num1[4],num2[4];
    int cnt=3;
    int tmp1,tmp2;
    int now;
    while(!Q.empty()){
        now=Q.front();Q.pop();
        int len=strlen(str[now]);

       if(str[now][0]=='C'){
            int i;
            memset(num1,'0',sizeof(num1));
            memset(num2,'0',sizeof(num2));
            cnt=3;
            for(i=len-1;str[now][i]!=' ';i--){//获取最后面的数字,这里要注意数字范围不止是一位
                num1[--cnt]=str[now][i];
            }
            i--;cnt=3;
            for(;str[now][i]!=' ';i--){
                num2[--cnt]=str[now][i];
            }
            num1[3]=num2[3]='\0';
           // cout<<num1<<" "<<num2<<endl;
            tmp1=atoi(num1);//字符串转换成数字
            tmp2=atoi(num2);
            //cout<<tmp1<<" "<<tmp2<<endl;
            if(!vis[tmp1]){
                Q.push(tmp1);
                vis[tmp1]=1;
                path[tmp1]=now;
            }

            if(!vis[tmp2]){
                Q.push(tmp2);
                vis[tmp2]=1;
                path[tmp2]=now;
            }
       }
       else{
            if(str[now][len-2]=='P'){
                   dis(now);
                break;
            }
       }

    }
}

int main(){

    int t;
    cin>>t;
    int cnt=1;
    while(t--){
        int n;
        memset(path,0,sizeof(path));
        cin>>n;
        getchar();
        for(int i=1;i<=n;i++){
            gets(str[i]);
        }
        cout<<"STORY "<<cnt++<<endl;
        bfs();
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值