summaryrefslogtreecommitdiff
path: root/12.c
blob: 238b418b1ef6ca5844f23d096fcea0fb87636ab7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX_CONNECTIONS 50
#define MAX_NODES       50

typedef struct node
{
    char        name[32];
    int         links;
    struct node *link[MAX_CONNECTIONS];
} Node;

static Node node[MAX_NODES] = {0};
static int nodes = 0;
static Node *start = NULL;
static Node *end = NULL;

static char *ReadLine(char *p, size_t size, FILE *fp)
{
    if ((p=fgets(p, size, fp)))
    {
        size_t l = strlen(p);

        while(l && p[l-1] == '\n')
        {
            p[--l] = 0;
        }
    }

    return p;
}

static Node *GetOrAddNode(const char *name)
{
    int f;

    for(f = 0; f < nodes; f++)
    {
        if (strcmp(name, node[f].name) == 0)
        {
            return node + f;
        }
    }

    if (f == MAX_NODES)
    {
        fprintf(stderr, "Too many nodes\n");
        exit(1);
    }

    strcpy(node[f].name, name);

    return node + f;
}

static void Link(Node *node1, Node *node2)
{
    node1->link[node1->links++] = node2;
    node2->link[node2->links++] = node1;
}

int main(void)
{
    char buff[4096];

    start = GetOrAddNode("start");
    end = GetOrAddNode("end");

    while(ReadLine(buff, sizeof buff, stdin))
    {
        Node *node1, *node2;
        char *p;

        p = strtok(buff, "-");
        node1 = GetOrAddNode(p);
        p = strtok(NULL, "-");
        node2 = GetOrAddNode(p);
        Link(node1, node2);
    }

    return 0;
}