summaryrefslogtreecommitdiff
path: root/9.c
blob: fc8d0e4799c105a43d4301a65df71e5b3458ab30 (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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX_SIZE 256

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 Lowest(char map[MAX_SIZE][MAX_SIZE],
                  int x, int y, int width, int height)
{
    char val;

    val = map[y][x];

    return (y == 0 || map[y-1][x] > val) &&
           (y == height - 1 || map[y+1][x] > val) &&
           (x ==0 || map[y][x-1] > val) &&
           (x == width -1 || map[y][x+1] > val);
}

int main(void)
{
    char buff[0x8000];
    char map[MAX_SIZE][MAX_SIZE]={0};
    int map_height = 0;
    int map_width = 0;
    int sum = 0;
    int x = 0;
    int y = 0;
    int f = 0;

    while(ReadLine(buff, sizeof buff, stdin))
    {
        map_width = strlen(buff);

        for(f = 0; f < strlen(buff); f++)
        {
            map[map_height][f] = buff[f] - '0';
        }

        map_height++;
    }

    for(y = 0; y < map_height; y++)
    {
        for(x = 0; x < map_width; x++)
        {
            if (Lowest(map, x, y, map_width, map_height))
            {
                sum += map[y][x] + 1;
            }
        }
    }

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

    return 0;
}