-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
185 lines (156 loc) · 3.87 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Q_SIZE 64
#define M_SIZE 100
#define ITERATIONS 100000
#define MAX_PRIORITY 999999999
#define MAP_WIDTH 10
#define MAP_HEIGHT 16
typedef struct {
int x, y;
int armor;
char map[MAP_WIDTH][MAP_HEIGHT];
char moves[M_SIZE]; int movesSize;
} State;
typedef struct {
int priority; // 2*distance + moves - 2*armor
State data;
} Node;
typedef Node Queue[Q_SIZE];
typedef struct {
char color;
int x, y;
} Flower;
#define END_X 4
#define END_Y 14
static Queue q = {{
.priority = 1,
.data = {
.x = 4, .y = 1,
.armor = 1,
.map = {
" L R ",
" R R ",
" R B ",
" L E ",
" L B E E",
"# B E ",
"# R R ",
"# B L ",
" L R ",
"# #### ###",
},
},
}};
void push(int priority, State *data) {
int min = MAX_PRIORITY;
int best = 0;
for (int i = 0; i < Q_SIZE; i++) {
if (q[i].priority < min) {
best = i;
min = q[i].priority;
}
}
q[best] = (Node) { .priority = priority, .data = *data };
}
void pop(State *data) {
int max = 0;
int best = 0;
for (int i = 0; i < Q_SIZE; i++) {
if (q[i].priority > max) {
best = i;
max = q[i].priority;
}
}
*data = q[best].data;
q[best].priority = 0;
}
char get(const State *s, int x, int y) {
if (x < 0 || x > MAP_WIDTH - 1 || y < 0 || y > MAP_HEIGHT - 1)
return '#';
return s->map[x][y];
}
int canMove(const State *s, char dir) {
int newX = s->x, newY = s->y;
switch (dir) {
case 'f':
newY += 1;
break;
case 'b':
newY -= 1;
break;
case 'l':
newX -= 1;
break;
case 'r':
newX += 1;
break;
}
return !strchr("#RB", get(s, newX, newY));
}
void move(State *s, char dir) {
switch (dir) {
case 'f':
++(s->y);
break;
case 'b':
--(s->y);
break;
case 'l':
--(s->x);
break;
case 'r':
++(s->x);
break;
}
int vertical = dir == 'f' || dir == 'b';
char lineDmg = vertical ? 'R' : 'B';
char diagDmg = vertical ? 'B' : 'R';
for (int i = -2; i <= 2; i++) {
if (i != 0) {
if (get(s, s->x + i, s->y) == lineDmg)
--(s->armor);
if (get(s, s->x, s->y + i) == lineDmg)
--(s->armor);
if (get(s, s->x + i, s->y + i) == diagDmg)
--(s->armor);
if (get(s, s->x + i, s->y - i) == diagDmg)
--(s->armor);
}
}
if (s->armor > 0 && s->map[s->x][s->y] == 'L') {
++(s->armor);
s->map[s->x][s->y] = ' ';
}
s->moves[s->movesSize++] = dir;
}
int priority(const State *s) {
int distance = abs(s->x - END_X) + abs(s->y - END_Y);
return MAX_PRIORITY + 2*s->armor - 2*distance - s->movesSize;
}
void play() {
State currentState, movedState;
for (int count = 0; count < ITERATIONS; count++) {
pop(¤tState);
for (int i = 0; i < 4; ++i) {
char direction = "flrb"[i];
if (!canMove(¤tState, direction))
continue;
movedState = currentState;
move(&movedState, direction);
if (movedState.map[movedState.x][movedState.y] == 'E') {
printf("Solution: %s with %d armor\n", movedState.moves, movedState.armor);
return;
} else if (movedState.armor > 0 && movedState.movesSize < M_SIZE - 1) {
push(priority(&movedState), &movedState);
}
}
}
}
int main(void)
{
play();
printf("AI finished!\n");
return 0;
}