diff options
Diffstat (limited to '9.c')
-rw-r--r-- | 9.c | 72 |
1 files changed, 72 insertions, 0 deletions
@@ -0,0 +1,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; +} |