Created
March 3, 2015 08:11
-
-
Save paaloeye/632b5e2e3777098bc728 to your computer and use it in GitHub Desktop.
Thor solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <math.h> | |
/** | |
* Auto-generated code below aims at helping you parse | |
* the standard input according to the problem statement. | |
**/ | |
#define DIRECTION_COUNT 8 | |
#define DIRECTION_START 0 | |
#define DIRECTION_STOP 7 | |
typedef struct _context_type { | |
int thor_x; | |
int thor_y; | |
int light_x; | |
int light_y; | |
} context_type; | |
typedef struct _direction_entry { | |
char name[3]; | |
int xoffset; | |
int yoffset; | |
} direction_entry; | |
typedef enum _direction_type { | |
N = 0, | |
NE, | |
E, | |
SE, | |
S, | |
SW, | |
W, | |
NW | |
} direction_type; | |
direction_entry direction_array[DIRECTION_COUNT] = { | |
{"N\0\0",0,-1}, | |
{"NE\0",1,-1}, | |
{"E\0\0",1,0}, | |
{"SE\0",1,1}, | |
{"S\0\0",0,1}, | |
{"SW\0",-1,1}, | |
{"W\0\0",-1,0}, | |
{"NW\0",-1,-1} | |
}; | |
double emetric(context_type * context); | |
void move(direction_type direction, context_type * context); | |
direction_type best_move(context_type * context); | |
int main() | |
{ | |
context_type context = {0}; | |
scanf("%d%d%d%d", &context.light_x, &context.light_y, &context.thor_x, &context.thor_y); | |
// game loop | |
while (1) { | |
int E; // The level of Thor's remaining energy, representing the number of moves he can still make. | |
direction_type dir; | |
scanf("%d", &E); | |
fprintf(stderr, "Thor's remaining energy: %d\n", E); | |
fprintf(stderr, "Distance: %f\n", emetric(&context)); | |
fprintf(stderr, "Thor coordinate: x=%d, y=%d\n", context.thor_x, context.thor_y); | |
dir = best_move(&context); | |
move(dir, &context); | |
} | |
} | |
double emetric(context_type * context) | |
{ | |
return sqrt( | |
pow(abs(context->thor_x - context->light_x),2) + pow(abs(context->thor_y - context->light_y),2) | |
); | |
} | |
void move(direction_type dir, context_type * context) | |
{ | |
direction_entry * current = &direction_array[dir]; | |
context->thor_x += current->xoffset; | |
context->thor_y += current->yoffset; | |
printf("%s\n", current->name); | |
} | |
direction_type best_move(context_type * context) | |
{ | |
double metric_array[DIRECTION_COUNT] = {0}; | |
direction_type best; | |
direction_entry * current; | |
double min; | |
for (size_t i = 0; i < DIRECTION_COUNT; i++) | |
{ | |
context_type copy_context = *context; | |
current = &direction_array[i]; | |
copy_context.thor_x += current->xoffset; | |
copy_context.thor_y += current->yoffset; | |
metric_array[i] = emetric(©_context); | |
} | |
min = metric_array[0]; | |
best = DIRECTION_START; | |
for (size_t i = 1; i < DIRECTION_COUNT; i++) | |
{ | |
if (metric_array[i] < min) { | |
min = metric_array[i]; | |
best = i; | |
} | |
} | |
return best; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment