URAL 1837 Isenbaev's Number (BFS)

#include<stdio.h>

#define NAMES_IN_A_TEAM (3)
#define MAX_TEAMS (100 + 1)
#define MAX_SIMBOLS (20 + 1)

struct NAME {
	char name[MAX_SIMBOLS];
	int team;
	int num;
};
NAME NAMEArray[NAMES_IN_A_TEAM * MAX_TEAMS];
int numOfNAMEs;
int NAMENum;
NAME queue[NAMES_IN_A_TEAM * MAX_TEAMS];
int head;
int tail;

int strCmp(char *one, char *another){
	while (*one == *another){
		if (*one == '\0')
			return 0;
		one++;
		another++;
	}
	return (*one - *another);
}

void strCpy(char *to, char *from){
	while (*from != '\0'){
		*to = *from;
		to++;
		from++;
	}
	*to = '\0';
}

void input(){
	int teams;
	scanf("%d", &teams);
	int team;
	for (team = 1; team <= teams; team++){
		int indexOfName;
		for (indexOfName = 1; indexOfName <= NAMES_IN_A_TEAM; indexOfName++){
			char name[MAX_SIMBOLS];
			scanf("%s", name);
			numOfNAMEs++;
			NAMENum = numOfNAMEs;
			NAMEArray[NAMENum].team = team;
			strCpy(NAMEArray[NAMENum].name, name);
			if (strCmp(name, "Isenbaev") == 0){
				NAMEArray[NAMENum].num = 0;
				queue[tail++] = NAMEArray[NAMENum];
			} else
				NAMEArray[NAMENum].num = -1;			
		}
	}
}

void BFS(){
	while (head < tail){
		NAME NAMEPoped = queue[head++];
		char namePoped[MAX_SIMBOLS];
		strCpy(namePoped, NAMEPoped.name);
		int nextNum = NAMEPoped.num + 1;

		int firstNAMENumInTeam = 1 + (NAMEPoped.team - 1) * NAMES_IN_A_TEAM;
		int lastNAMENumInTeam = firstNAMENumInTeam + 2;
		int indexOfNAMEInTeam;
		for (indexOfNAMEInTeam = firstNAMENumInTeam; indexOfNAMEInTeam <= lastNAMENumInTeam; indexOfNAMEInTeam++){
			if (NAMEArray[indexOfNAMEInTeam].num != -1 || strCmp(namePoped, NAMEArray[indexOfNAMEInTeam].name) == 0)
				continue;
			NAMEArray[indexOfNAMEInTeam].num = nextNum;

			char name[MAX_SIMBOLS];
			strCpy(name, NAMEArray[indexOfNAMEInTeam].name);
			int indexOfNAME;
			for (indexOfNAME = 1; indexOfNAME <= numOfNAMEs; indexOfNAME++){
				if (NAMEArray[indexOfNAME].num != -1 || strCmp(NAMEArray[indexOfNAME].name, name) != 0)
					continue;
				NAMEArray[indexOfNAME].num = nextNum;
				NAME NAMEPushed = NAMEArray[indexOfNAME];
				queue[tail++] = NAMEPushed;
			}

		}
	}
}

void NAMECopy(NAME *to, NAME *from){
	strCpy(to->name, from->name);
	to->num = from->num;
}

void quickSortNAMEs(int start, int end){
	if (start >= end)
		return;
	while (start < end){
		int left = start;
		int right = end;
		NAME pivotNAME;
		NAMECopy(&pivotNAME, &NAMEArray[start]);
		char pivotName[MAX_SIMBOLS];
		strCpy(pivotName, pivotNAME.name);
		while (left < right){
			while (left < right && strCmp(NAMEArray[right].name, pivotName) >= 0) 
				right--;
			NAMECopy(&NAMEArray[left], &NAMEArray[right]);
			while (left < right && strCmp(NAMEArray[left].name, pivotName) <= 0)
				left++;
			NAMECopy(&NAMEArray[right], &NAMEArray[left]);
		}
		NAMECopy(&NAMEArray[left], &pivotNAME);
		int pivot = left;
		quickSortNAMEs(start, pivot - 1);
		start = pivot + 1;
	}
}

void output(){
	char preName[MAX_SIMBOLS] = "";
	int indexOfNAME;
	for (indexOfNAME = 1; indexOfNAME <= numOfNAMEs; indexOfNAME++){
		if (strCmp(NAMEArray[indexOfNAME].name, preName) == 0)
			continue;
		printf("%s ", NAMEArray[indexOfNAME].name);
		if (NAMEArray[indexOfNAME].num == -1)
			printf("undefined\n");
	    else
			printf("%d\n", NAMEArray[indexOfNAME].num);
		strCpy(preName, NAMEArray[indexOfNAME].name);	
	}
}

int main(){
	input();
	BFS();
	quickSortNAMEs(1, numOfNAMEs);
	output();	
	return 0;
}


 
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值