zoukankan      html  css  js  c++  java
  • hdu3786 找出直系亲属 水题

    题意:
    找出直系亲属
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1189    Accepted Submission(s): 493


    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
    -
     


    思路:

           我用的是Floyd处理了一遍,也可以直接搜索处理,怎么都行,题简单,数据简单,随意怎么写,只要能记录两点之间的距离就行了,还有就是一定是有向图,还有个提示就是输入的不一定只是26个大写字母,一开始一看26就直接自己默认了,结果wa了好几次,丢脸啊..


    #include<stdio.h>
    
    #define INF 1000000000
    
    int map[105][105];
    
    void Floyd()
    {
       for(int k = 1 ;k <= 100 ;k ++)
       for(int i = 1 ;i <= 100 ;i ++)
       for(int j = 1 ;j <= 100 ;j ++)
       if(map[i][j] > map[i][k] + map[k][j])
       map[i][j] = map[i][k] + map[k][j];
    }
    
    int minn(int x, int y)
    {
       return x < y ? x : y;
    }
    
    int main ()
    {
       int n ,m ,i ,j ,a ,b ,c;
       char str[10];
       while(scanf("%d %d" ,&n ,&m) && n + m)
       {
          //getchar();
          for(i = 1 ;i <= 100 ;i ++)
          for(j = 1 ;j <= 100 ;j ++)
          if(i == j)map[i][j] = 0;
          else map[i][j] = INF;   
          for(i = 1 ;i <= n ;i ++)
          {
             scanf("%s" ,str);
            // getchar();
             a = str[0];
             b = str[1];
             c = str[2];
             map[a][b] = map[a][c] = 1;
          }
          Floyd();
          for(i = 1 ;i <= m ;i ++)
          {
             scanf("%s" ,str);
             a = str[0];
             b = str[1];
             c = minn(map[a][b] ,map[b][a]);
             if(c == INF || !c)
             {
                puts("-");
                continue;
             }
             if(c == 1)
             {
                if(map[a][b] == 1) puts("child");
                else puts("parent");
             }
             else
             {
                if(map[a][b] != INF)
                {
                   c -= 2;
                   for(j = 1 ;j <= c ;j ++)
                   printf("great-");
                   puts("grandchild");
                }
                else
                {
                   c -= 2;
                   for(j = 1 ;j <= c ;j ++)
                   printf("great-");
                   puts("grandparent");
                }
             }
          }
       }
       return 0;
    }
    

  • 相关阅读:
    二级联动选择框的实现
    vimperator
    Ipan笔记-2
    git的一些补充点
    联想云部署的笔记心得
    关于vim的折叠
    ipan笔记
    php中浮点数计算问题
    Chrome 控制台报错Unchecked runtime.lastError: The message port closed before a response was received
    PHP-redis中文文档
  • 原文地址:https://www.cnblogs.com/csnd/p/12063164.html
Copyright © 2011-2022 走看看