summaryrefslogtreecommitdiff
path: root/5a.c
blob: 277690640816b66eb959aed53a8842f5a31b8896 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX_BOARDS 100

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 int Max(int a, int b)
{
    return a > b ? a : b;
}

static int Min(int a, int b)
{
    return a < b ? a : b;
}

int main(void)
{
    char buff[1024];
    int board[1000][1000] = {0};
    char *p = NULL;
    int f = 0;
    int x1 = 0;
    int y1 = 0;
    int x2 = 0;
    int y2 = 0;
    int x = 0;
    int y = 0;
    int sum = 0;

    while (ReadLine(buff, sizeof buff, stdin))
    {
        p = strtok(buff, " ,->");
        x1 = atoi(p);
        p = strtok(NULL, " ,->");
        y1 = atoi(p);
        p = strtok(NULL, " ,->");
        x2 = atoi(p);
        p = strtok(NULL, " ,->");
        y2 = atoi(p);

        if (x1 == x2)
        {
            for(y = Min(y1, y2); y <= Max(y1, y2); y++)
            {
                board[y][x1]++;
            }
        }
        else if (y1 == y2)
        {
            for(x = Min(x1, x2); x <= Max(x1, x2); x++)
            {
                board[y1][x]++;
            }
        }
        else
        {
            int dx;
            int dy;

            dx = x1 < x2 ? 1 : -1;
            dy = y1 < y2 ? 1 : -1;

            x = x1;
            y = y1;

            while(x != x2 && y != y2)
            {
                board[y][x]++;
                x += dx;
                y += dy;
            }

            board[y][x]++;
        }
    }

    for(y = 0; y < 999; y++)
    {
        for(x = 0; x < 999; x++)
        {
            if (board[y][x] > 1)
            {
                sum++;
            }
        }
    }

    printf("sum = %d\n", sum);

    return 0;
}