poj 2023 -- scanf用法

来源:互联网 发布:单机游戏mac 编辑:程序博客网 时间:2024/03/28 16:49



题目链接:http://poj.org/problem?id=2023



Choose Your Own Adventure
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 1503 Accepted: 609

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

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 1Arrived at LSU for the contestWon the contest. Received glory and nachos.STORY 2Saw a peanutFound a hammerMade peanut butter sandwich

Source

South Central USA 2004


题意:James要给自己做一个冒险故事,他希望他的故事到最后是Happy的。


现在给出故事条目:

每个故事有n页每一页上面有一段故事,这段故事可以是一个choice page或者 end page

choice page 可以选择2叶下故事要发展下去的页码,end page 则表示故事结束。


其实就是把页码构图,然后DFS一下最终结局是不是Happy的,是的话就打印路径。

这里写这篇文章是为了介绍下scanf的用法


题中的输入如果用cin,get,甚至是cin.getline()都很麻烦

scanf 里面有一个类似cin.getline(str,len,"")的用法

scanf("%[^C]") ,C表示scanf读取的截至字符exp:scanf("%[^\"]",str) “\”是转义字符, 这个表示读取字符到str,直到遇到“"”。

scanf("%[C]") , C表示scanf读取的字符列表exp: scanf("%[abc]",str)  这个表示只读取abc这3个字符,如果遇到其他字符则停止读入


所以本题源码


#include <iostream>using namespace std;int t,n,cas=0;struct story{bool isend;char tex[280];bool ishappy;}Mstory[110];int sum[110];//AdListint Adlist[110][110];bool mk[110];//markint path[110];bool end;void go(int now,int cnt)//dfs{if(end)return ;path[cnt]=now;if(Mstory[now].isend&&Mstory[now].ishappy){ for(int i=0;i<=cnt;i++)printf("%s\n",Mstory[path[i]].tex); end = true; return ;}for(int i=0;i<sum[now];i++){if(!mk[Adlist[now][i]]){mk[Adlist[now][i]]=true;go(Adlist[now][i],cnt+1);mk[Adlist[now][i]]=false;}}}int main(){scanf("%d",&t);int i,x,y,total=0;char tpy[30];while (t--){scanf("%d",&n);//initmemset(sum,0,sizeof(int)*n);memset(Adlist,255,sizeof(Adlist));memset(mk,0,sizeof(mk));end=false;for (i=0;i<n;i++){scanf("%s",tpy);if(tpy[0]=='C'){//自己理解一下为什么这么读scanf("%[^\"]%c%[^\"]%s %d %d",tpy,&tpy[0],Mstory[i].tex,tpy,&x,&y);Mstory[i].isend=false;Adlist[i][sum[i]++]=x-1;Adlist[i][sum[i]++]=y-1;}else{//自己理解一下为什么这么读scanf("%[^\"]%c%[^\"]%s %s",tpy,&tpy[0],Mstory[i].tex,tpy,tpy);if(tpy[0]=='G')Mstory[i].ishappy=false;elseMstory[i].ishappy=true;Mstory[i].isend = true;}}printf("STORY %d\n",++total);mk[0]=true;go(0,0);}return 0;}



原创粉丝点击