summaryrefslogtreecommitdiffstats
path: root/šola/p1/dn/DN02_63230317.java
blob: 1eaa9dec36ba8f90cebc2c18461c0b08891c6442 (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
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.sqrt(tipka)/2;
	}
	public static int indeks (int tipka) {
		return tipka-(obroč(tipka)*2)*(obroč(tipka)*2);
	}
	public static int velikost (int obr) {
		return ((obr+1)*2)*((obr+1)*2) - (obr*2)*(obr*2);
	}
	public static int[] koordinate (int tipka) {
		int koord[] = {
			-obroč(tipka),
			-obroč(tipka)
		};
		if (indeks(tipka) < velikost(obroč(tipka))/4) {
			koord[0] += indeks(tipka);
			return koord;
		}
		if (indeks(tipka) < velikost(obroč(tipka))/2) {
			koord[0] += velikost(obroč(tipka))/4;
			koord[1] += indeks(tipka);
			return koord;
		}
		if (indeks(tipka) < 3*velikost(obroč(tipka))/4) {
			koord[0] += velikost(obroč(tipka))/2-indeks(tipka);
			koord[1] += velikost(obroč(tipka))/4;
			return koord;
		}
		koord[1] += velikost(obroč(tipka))-indeks(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);
	}
}