-
Notifications
You must be signed in to change notification settings - Fork 0
/
DN03.c
109 lines (95 loc) · 3.59 KB
/
DN03.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <stdlib.h>
int getSnackType(int locX, int locY, int *snaxX, int *snaxY, int *snaxT, int numberOfSnax) {
for (int i = 0; i < numberOfSnax; ++i) {
if ((*(snaxX + i) == locX) && (*(snaxY + i) == locY)) {
return *(snaxT + i);
}
}
return 0;
}
void moveTheSnake(int *snakeX, int *snakeY, int snakeDirectionX, int snakeDirectionY, int snakeLen, int currentSnack) {
for (int i = (currentSnack == 0) ? (snakeLen - 1) : (snakeLen); i > 0; i--) {
*(snakeX + i) = *(snakeX + i - 1);
*(snakeY + i) = *(snakeY + i - 1);
}
*snakeX += snakeDirectionX;
*snakeY += snakeDirectionY;
}
int checkSnakeBite(int *snakeX, int *snakeY, int snakeLen) {
int headX = *snakeX;
int headY = *snakeY;
for (int i = 1; i < snakeLen; i++) {
if ((*(snakeX + i) == headX) && (*(snakeY + i) == headY)) {
return 1;
}
}
return 0;
}
int main() {
// Crucial snake data
int snakeLen = 1;
int snakeDirectionX = 0;
int snakeDirectionY = 1;
// Snake body parts locations :)
int *snakeX = (int *) malloc(1000 * sizeof(int));
int *snakeY = (int *) malloc(1000 * sizeof(int));
*snakeX = 0;
*snakeY = 0;
int numberOfSnacks;
scanf("%d", &numberOfSnacks);
// Location and data of snacks
int *snackLocX = (int *) malloc(numberOfSnacks * sizeof(int));
int *snackLocY = (int *) malloc(numberOfSnacks * sizeof(int));
int *snackType = (int *) malloc(numberOfSnacks * sizeof(int));
for (int i = 0; i < numberOfSnacks; i++) {
scanf("%d %d %d", snackLocX + i, snackLocY + i, snackType + i);
}
int steps;
scanf("%d", &steps);
for (int j = 0; j < steps; ++j) {
// Determine if we happen to stand on a snack.
int currentSnack = getSnackType(*snakeX, *snakeY, snackLocX, snackLocY, snackType, numberOfSnacks);
// Change snake direction should the snake's head land on one of the special snacks.
if (currentSnack == 2) {
if (snakeDirectionX == 0 && snakeDirectionY == 1) {
snakeDirectionX = -1;
snakeDirectionY = 0;
} else if (snakeDirectionX == -1 && snakeDirectionY == 0) {
snakeDirectionX = 0;
snakeDirectionY = -1;
} else if (snakeDirectionX == 0 && snakeDirectionY == -1) {
snakeDirectionX = 1;
snakeDirectionY = 0;
} else if (snakeDirectionX == 1 && snakeDirectionY == 0) {
snakeDirectionX = 0;
snakeDirectionY = 1;
}
} else if (currentSnack == 3) {
if (snakeDirectionX == 0 && snakeDirectionY == 1) {
snakeDirectionX = 1;
snakeDirectionY = 0;
} else if (snakeDirectionX == 1 && snakeDirectionY == 0) {
snakeDirectionX = 0;
snakeDirectionY = -1;
} else if (snakeDirectionX == 0 && snakeDirectionY == -1) {
snakeDirectionX = -1;
snakeDirectionY = 0;
} else if (snakeDirectionX == -1 && snakeDirectionY == 0) {
snakeDirectionX = 0;
snakeDirectionY = 1;
}
}
moveTheSnake(snakeX, snakeY, snakeDirectionX, snakeDirectionY, snakeLen, currentSnack);
if (currentSnack == 1) {
snakeLen++;
}
int bite = checkSnakeBite(snakeX, snakeY, snakeLen);
if (bite == 1) {
printf("%d %d %d\n", 0, *snakeX, *snakeY);
return 0;
}
}
printf("%d %d %d\n", snakeLen, *snakeX, *snakeY);
return 0;
}