URAL 1837 Isenbaev's Number (BFS)
2015-09-01 12:38
459 查看
#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网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- HDU 1568
- HDU1290