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