算法

POJ2023总结

Choose Your Own Adventure

题目来源

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

我的代码

bfs+路径记录。路径记录就是记录队列每一个元素的父元素下标,答案是根据出口的下标回溯所经过的元素。

#include<stdio.h>
#include<queue>
using namespace std;
struct story
{
	char sentence[260];
	char type;
	char end[10];
	int x,y;
}storys[101];
int main()
{
	int t;
	scanf("%d",&t);
	for(int o=1;o<=t;o++)
	{
		int n;
		char type;
		char story[101][260];
		scanf("%d",&n);
		for (int i=1;i<=n;i++)
		{
			scanf(" %c",&storys[i].type);
			int num=0,words=0;
			char temp;
			while(num!=2)
			{
				scanf("%c",&temp);
				
				if (temp!='"')
				{
					if (num==1)
					storys[i].sentence[words++]=temp;
				}
				else
				{
					num++;
				}
			}
			storys[i].sentence[words]='\0';
			if (storys[i].type=='C')
			{
				scanf("%d%d",&storys[i].x,&storys[i].y);
			}
			else
			{
				
				scanf(" %s",storys[i].end);
			}
		}
		int que[101],father[101];
		int head=0,tail=1;
		que[0]=1;
		father[0]=0;
		while(head<tail)
		{
			if (storys[que[head]].type=='C')
			{
				que[tail]=storys[que[head]].x;
				father[tail++]=head;
				que[tail]=storys[que[head]].y;
				father[tail++]=head;
			}
			else
			{
				if (storys[que[head]].end[0]=='H')
				{
					break;
				}
			}
			head++;
		}
		
		vector <int> ans;
		while(que[head]!=1)
		{
			ans.push_back(que[head]);
			head=father[head];
		}
		ans.push_back(1);
		printf("STORY %d\n",o);
		for (int i=ans.size()-1;i>=0;i--)
		{
			printf("%s\n",storys[ans[i]].sentence);
		}
	}
	return 0;
}

反思

这题用dfs也可以,但是好像有回路,会导致死循环;我用宽搜就没什么事。

对于本题的输入,有这种方法:

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

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

原文链接

发表评论