`
java-mans
  • 浏览: 11340056 次
文章分类
社区版块
存档分类
最新评论

2009年浙江大学计算机及软件工程研究生机试真题

 
阅读更多

http://ac.jobdu.com/problem.php?pid=1035 找出直系亲属

//floyd
#include<stdio.h>
#include<string.h>
#include<memory.h>

int map[27][27];

void floyd_warshall(int n)
{
	int i,j,k;
	for(k=0;k<=n;k++)
	{
		for(i=0;i<=n;i++)
		{
			for(j=0;j<=n;j++)
			{
				if(map[i][k]+map[k][j]<map[i][j])
					map[i][j]=map[i][k]+map[k][j];
			}
		}
	}
}

int main(void)
{
	int m,n,i,from,to,max,k;
	char str[4];
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		if(!m && !n)
			break;
		memset(map,1,sizeof(map));
		max=0;
		for(i=0;i<n;i++)    //建图
		{
			scanf("%s",str);
			from=str[0]-'A';
			to=str[1]-'A';
			map[from][to]=1;
			if(to>max)
				max=to;
			to=str[2]-'A';
			map[from][to]=1;
			if(from>max)
				max=from;
			if(to>max)
				max=to;
		}
		floyd_warshall(max);
		for(i=0;i<m;i++)     //16843009
		{
			scanf("%s",str);
			from=str[0]-'A';
			to=str[1]-'A';
			if(map[from][to]==16843009 && map[to][from]==16843009)
				printf("-\n");
			else if(map[from][to]==16843009 && map[to][from]<16843009)
			{
				k=map[to][from];
				while(k>2)
				{
					k--;
					printf("great-");
				}
				if(k==2)
					printf("grandparent\n");
				else if(k==1)
					printf("parent\n");
			}
			else if(map[from][to]<16843009 && map[to][from]==16843009)
			{
				k=map[from][to];
				while(k>2)
				{
					k--;
					printf("great-");
				}
				if(k==2)
					printf("grandchild\n");
				else if(k==1)
					printf("child\n");
			}
		}
	}
	return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics