hdu 3786 找出直系亲属 (dfs)

Problem Description
如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,则A,B是C的grandparent,C是A,B的grandchild,如果A,B是C的(外)曾祖父,曾祖母,则A,B是C的great-grandparent,C是A,B的great-grandchild,之后再多一辈,则在关系上加一个great-。
 
Input
输入包含多组测试用例,每组用例首先包含2个整数n(0<=n<=26)和m(0<m<50), 分别表示有n个亲属关系和m个问题, 然后接下来是n行的形式如ABC的字符串,表示A的父母亲分别是B和C,如果A的父母亲信息不全,则用-代替,例如A-C,再然后是m行形式如FA的字符串,表示询问F和A的关系。
当n和m为0时结束输入。
 
Output
如果询问的2个人是直系亲属,请按题目描述输出2者的关系,如果没有直系关系,请输出-。
具体含义和输出格式参见样例.
 
Sample Input
 
3 2
ABC
CDE
EFG
FA
BE
0 0
 
Sample Output
 
great-grandparent
-

 

思路:写的dfs搜索,感觉还行,具体可以看注释......

 

 

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<map>
  7 #include<set>
  8 #include<vector>
  9 #include<queue>
 10 using namespace std;
 11 #define ll long long 
 12 
 13 const int maxn=105;//A->Z : 65->90 
 14 
 15 int e[maxn][2];
 16 int book[maxn];
 17 
 18 int countt;
 19 
 20 void dfs(int zi,int step,int endd)
 21 {
 22     if(countt!=-1)//已经找到,直接return 
 23         return ;
 24     
 25     if(zi==endd)//找到了 
 26     {
 27         countt=step;
 28         return ;
 29     }
 30     
 31     for(int i=0;i<2;i++)//两个方向搜索 
 32     {
 33         if(e[zi][i]!=0&&book[e[zi][i]]==0)
 34         {
 35             book[e[zi][i]]=1;
 36             dfs(e[zi][i],step+1,endd);
 37             book[e[zi][i]]=0;
 38         }
 39     }
 40     return ;
 41 }
 42 
 43 int main()
 44 {
 45     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 46     
 47     int n,m;
 48     while(cin>>n>>m&&(n||m))
 49     {
 50         memset(e,0,sizeof(e));
 51         
 52         string str;
 53         
 54         int zi,fu,mu; 
 55     
 56         for(int i=0;i<n;i++)
 57         {
 58             cin>>str;
 59     
 60             zi=str[0];    fu=str[1];    mu=str[2];
 61             
 62             if(str[1]!='-')//排除 
 63                 e[zi][0]=fu;
 64             if(str[2]!='-')//排除 
 65                 e[zi][1]=mu;
 66             
 67         }
 68         
 69         int a,b;
 70         
 71         for(int i=0;i<m;i++)
 72         {
 73             cin>>str;
 74             a=str[0];    b=str[1];
 75             
 76             if(a==b)//这题没有这个特例,不用特判也能过 
 77             {
 78                 cout<<"-"<<endl;
 79                 continue;
 80             }
 81             
 82             countt=-1;
 83             
 84             memset(book,0,sizeof(book));
 85             book[a]=1;
 86             
 87             int flag=0; 
 88             
 89             dfs(a,0,b);//从a向上找,有没有b 
 90 
 91             if(countt==-1)//没找到 
 92             {
 93                 flag=1;//刷新flag 
 94                 
 95                 memset(book,0,sizeof(book));//重新初始化再找 
 96                 book[b]=1;
 97                 dfs(b,0,a);//从b向上找,有没有a 
 98             }
 99             
100             if(countt==-1)//找两次都没找到 
101                 cout<<"-"<<endl;//显然两个人没关系 
102             else
103             {
104                 if(flag==1)//第二次从b找到了a, a是b的长辈 
105                 {
106                     if(countt==1)//格式输出 
107                         cout<<"parent"<<endl;
108                     else if(countt==2)
109                         cout<<"grandparent"<<endl;
110                     else if(countt==3)
111                         cout<<"great-grandparent"<<endl;
112                     else
113                     {
114                         for(int i=0;i<countt-3;i++)
115                             cout<<"great-";
116                         cout<<"great-grandparent"<<endl;
117                     }
118                 }
119                 else if(flag==0)//第一次从a找到了b, a是b的晚辈 
120                 {
121                     if(countt==1)
122                         cout<<"child"<<endl;
123                     else if(countt==2)
124                         cout<<"grandchild"<<endl;
125                     else if(countt==3)
126                         cout<<"great-grandchild"<<endl;
127                     else
128                     {
129                         for(int i=0;i<countt-3;i++)
130                             cout<<"great-";
131                         cout<<"great-grandchild"<<endl;
132                     }
133                 }
134             }    
135         }    
136     }
137     
138     return 0;
139 }

 

 

转载于:https://www.cnblogs.com/xwl3109377858/p/11048092.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值