zoukankan      html  css  js  c++  java
  • 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 }
    大佬见笑,,
  • 相关阅读:
    HDU 4628 Pieces
    HDU 2983 Integer Transmission
    HDU 1820 Little Bishops
    CodeForces 165E Compatible Numbers
    CodeForces 11D A Simple Task
    HDU 4804 Campus Design
    HDU 3182 Hamburger Magi
    Linux的用户和组
    Linux文件/目录权限及归属
    Vim使用介绍
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/11048092.html
Copyright © 2011-2022 走看看