summaryrefslogtreecommitdiffstats
path: root/inf/lige/1/2.c
blob: b96109388859508b44a35d2c0b48393ff314f88f (plain) (blame)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void) {
	char buf[128];
	fgets(buf, 128, stdin);
	char * c = buf;
	int n = strtol(c, &c, 10);
	c++;
	int k = strtol(c, &c, 10);
	int * zx = calloc(1000000, sizeof *zx);
	int * zy = calloc(1000000, sizeof *zy);
	int x = 0;
	int y = 0;
	int num = 0;
	int vseh = 0;
	while (1) {
		char a = getchar();
		switch (a) {
			case EOF:
				goto x;
			case '\r':
				break;
			case '\n':
#ifndef EVAL
				fprintf(stderr, "newline\n");
#endif
				if (num) {
					zx[num-1] = x;
					zy[num-1] = y;
					num = 0;
					vseh++;
				}
				x = 0;
				y++;
				break;
			case ' ':
#ifndef EVAL
				fprintf(stderr, "presledek\n");
#endif
				if (num) {
					zx[num-1] = x;
					zy[num-1] = y;
					num = 0;
					vseh++;
				}
				x++;
				break;
			default:
#ifndef EVAL
				fprintf(stderr, "številka %c\n", a);
#endif
				num *= 10;
				num += a - '0';
		}
	}
x:;
	long long int sum = 0;
	int curx = 0;
	int cury = 0;
	for (int i = 0; i < vseh; i++) {
		sum += abs(curx - zx[i]) + abs(cury - zy[i]);
#ifndef EVAL
		fprintf(stderr, "znamenitost %d je na %d %d, sum je %lld\n", i, zx[i], zy[i], sum);
#endif
		curx = zx[i];
		cury = zy[i];
	}
	printf("%lld\n", sum);
}