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;
}


 
 
 
 
 

你可能感兴趣的:(ACM,number,bfs,ural,1837,Isenbaevs)