题目链接:http://codeforces.com/gym/100989/problem/J
Raihan is helping Maram and Master Hasan in the design of their graduation project. They have a panel that shows a list of the objects created by the user. Objects can be nested inside each other, the user can click the '-' sign to collapse the items nested inside an object, or click the '+' sign to expand the objects nested inside it.
Check the following table for more details:
Note that all objects are nested inside a list item called project.
For each object in the project, you are given a list of objects nested inside it and whether it’s expanded or collapsed. Can you help Raihan in drawing the panel?
Input
The first line of input contains an integer N (0 ≤ N ≤ 100), the number of objects in the project. Objects are numbered from 1 to N. The project item is item number 0.
Each of the following N + 1 lines contains the description of an object. The ith(starting from 0) line describes the object number i in the following format:
S K A1 A2 ... AK
Where S is the state of the object (expanded or collapsed), where '-' means it's expanded and '+' means it's collapsed. The state of objects that do not have any nested objects will be '-'.
K is the number of objects nested inside this object, this number is followed by Kdistinct numbers representing the numbers of the nested objects.
Output
Output the current state of the list, the items nested in an object should be listed in the given order.
An object inside another is nested by two spaces, the first space is replaced with '+' or '-' depending on the state of the object, if the object does not have other objects inside it, then keep the space.
Examples
6
- 3 2 5 1
+ 1 3
- 1 6
- 1 4
- 0
- 0
- 0
- project
- object2
object6
object5
+ object1
0
- 0
project
题意:按照给出的项目要求,输出对象列表。
思路:因为他是按照先后顺序的,如果打开一个(输出)对象,接下来输出的是该嵌套在该对象中的其他对象,一直往下,直到到嵌套的最后一个对象,再依次返回上一层对象。所以很容易想到要用DFS。
知道用DFS后这道题就不难了。注意一下输出格式,下面是代码。
1 #include<stdio.h> 2 #include<string.h> 3 char s[105]; 4 int father[105];//指project的子孩子有哪些 5 int child[105][105];//child[i][j]指对象i的第j个child; 6 int child_number[105];//child_number[i]指对象i的孩子有多少个 7 void DFS(char s1,int childs,int i,int w)//(是'+'还是'-',孩子数,下标,缩放第几层次) 8 { 9 for(int q=0;q<w;q++) 10 printf(" "); 11 if(childs==0) 12 { 13 printf(" object%d\n",i); 14 return ; 15 } 16 else 17 { 18 printf("%c ",s1); 19 printf("object%d\n",i); 20 if(s1=='-')//只有需要展开的时候才需要DFS 21 { 22 for(int j=1;j<=childs;j++) 23 { 24 DFS(s[child[i][j]],child_number[child[i][j]],child[i][j],w+1); 25 } 26 } 27 } 28 } 29 int main() 30 { 31 int N; 32 while (scanf("%d",&N)!=EOF) 33 { 34 getchar(); 35 memset(child,0,sizeof(child)); 36 char s0;int k; 37 scanf("%c%d",&s0,&k); 38 for(int i=1;i<=k;i++) 39 scanf("%d",&father[i]); 40 for(int i=1;i<=N;i++) 41 { 42 getchar(); 43 scanf("%c%d",&s[i],&child_number[i]); 44 for(int j=1;j<=child_number[i];j++) 45 scanf("%d",&child[i][j]); 46 } 47 if(N==0) 48 { 49 printf(" project\n"); 50 } 51 else 52 { 53 if(s0=='+') 54 printf("%c project\n",s0); 55 else 56 { 57 printf("%c project\n",s0); 58 for(int i=1;i<=k;i++) 59 { 60 DFS(s[father[i]],child_number[father[i]],father[i],1); 61 } 62 } 63 } 64 } 65 return 0; 66 }