summaryrefslogtreecommitdiffstats
path: root/šola/p1/dn/DN02_63230317.java
blob: 95522d6e03b1cd817f5183bca92e2dd6a3115983 (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
71
import java.util.*;	
public class DN01_63230317 {
	public static int vrstica (int tipka) {
		return (int) Math.sqrt(tipka);
	}
	public static int stolpec (int tipka) {
		return tipka-vrstica(tipka)*vrstica(tipka)-vrstica(tipka);
	}
	public static int obroč (int tipka) {
		return (int) Math.ceil(Math.floor(Math.sqrt(tipka))/2);
	}
	public static int prva (int obr) {
		return (int) Math.pow(obr*2-1, 2);
	}
	public static int indeks (int tipka) {
		if (tipka == 0)
			return 0;
		return tipka - prva(obroč(tipka));
	}
	public static int velikost (int obr) {
		return prva(obr+1) - prva(obr);
	}
	public static int[] koordinate (int tipka) {
		int koord[] = {
			-obroč(tipka),
			obroč(tipka)
		};
		if (indeks(tipka) < velikost(obroč(tipka))/4) {
			koord[0] += indeks(tipka);
		} else if (indeks(tipka) < velikost(obroč(tipka))/2) {
			koord[0] += velikost(obroč(tipka))/4;
			koord[1] -= indeks(tipka)-velikost(obroč(tipka))/4;
		} else if (indeks(tipka) < 3*velikost(obroč(tipka))/4) {
			koord[0] += velikost(obroč(tipka))/4-(3*velikost(obroč(tipka))/4-indeks(tipka));
			koord[1] -= velikost(obroč(tipka))/4;
		} else
			koord[1] -= velikost(obroč(tipka))-indeks(tipka);
		if (System.getenv("DN02_DEBUG") != null) {
			System.err.println("Tipka " + tipka + " je na koordinatah " + koord[0] + ", " + koord[1] + " ... obroč=" + obroč(tipka) + ", indeks=" + indeks(tipka) + ", velikost=" + velikost(obroč(tipka)));
		}
		return koord;
	}
	public static int razdalja (int d, int oblika, int začetek, int konec) {
		switch (oblika) {
			case 1: // ravnovrstnica
				return Math.abs(konec-začetek);
			case 2: // kvadratnica
				return Math.abs(konec%d-začetek%d)+Math.abs(konec/d-začetek/d);
			case 3: // piramidnica
				return Math.abs(vrstica(konec)-vrstica(začetek))+Math.abs(stolpec(konec)-stolpec(začetek));
			case 4: // spiralnica
				return Math.abs(koordinate(konec)[0]-koordinate(začetek)[0])+Math.abs(koordinate(konec)[1]-koordinate(začetek)[1]);
		}
		throw new Error("napačna tipkovnica");
	}
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int oblika = sc.nextInt();
		int d = sc.nextInt();
		int dolžina = sc.nextInt();
		int pot = 0;
		int stara = 0;
		for (int i = 0; i < dolžina; i++) {
			int tipka = sc.nextInt();
			if (i > 0)
				pot += razdalja(d, oblika, tipka, stara);
			stara = tipka;
		}
		System.out.println(pot);
	}
}