# Homework Help: C++ Destruction Droid program

1. Jan 6, 2010

### Wanna-be Eng.

1. The problem statement, all variables and given/known data
Problem Destruction Droid

Introduction

Life was easy and happy on the third moon of Sirius 8: people lived and worked in the many peaceful
colonies. However, the Klingon Empire decided to extend its control to this part of the galaxy: they
landed a Destruction DroidTM
on the surface of the moon. The Destruction DroidTM
will go to one
colony and destroy it completely. If we knew which colony is targeted by the Destruction DroidTM, then
we could concentrate our forces to defend that colony. Fortunately, we have intercepted the commands
transmitted to the Destruction DroidTM, thus it is possible to determine its destination. Your task is to
write a program that determines the unfortunate colony where the Destruction DroidTM
goes.

One of the nice things about this moon is that it is almost completely flat, hence everything can be
described easily with two coordinates. East is x
direction, North is y
direction.

Input

Each input file begins with a line containing four integers:

n<=500, the number of commands,
m<=100, the number of colonies,
x and y, the starting coordinates of the Destruction DroidTM

The first line is followed by n
lines containing one command each. There are 5 different commands:

LEFT p : turn left p degrees
RIGHT p : turn right p degrees
FASTER p : increase the speed with p units
SLOWER p : decrease the speed with p units
WAIT p : the Destruction DroidTM goes for p time units in the current direction with the current speed

Speed is given in coordinate units per time units. It can be assumed that the commands do not increase
the speed above 500 or below 0. After landing, the Destruction DroidTM faces North(y direction), and
its speed is 0. The commands LEFT, RIGHT, FASTER, and SLOWER are executed in zero time: the
Destruction DroidTM does not move during these commands.

The last m lines of the input describe the colonies. Each lin ebegin swith a name of length at most 20,
which does not contain any space characters. The name is followed by the two coordinates of the colony.

Output

You have to output the name of the colony where the Destruction DroidTM will be after executing the
commands.
Write a newline character afterthe name ofthe colony. It canbe assumedthattheDestruction DroidTM will be at a distance of at most 10 from some colony, and there is a unique such colony. It is possible that the Destruction DroidTM goes through some other colonies while executing the commands, but we do not care about that.

Sample Input

5 4 0 0

FASTER 10
WAIT 10
RIGHT 135
SLOWER 5
WAIT 28
Aaaa 0 0
Bbbb 100 100
Cccc 0 100
Dddd 100 0

Sample Output

Dddd

2. Relevant equations

all c++ you can get

3. The attempt at a solution

I was thinking about using functions. Is it necessary to use clases? I'm not sure how to make the program calculate the last position of the droid. I'll come up with some code, probably tommorrow.

Last edited: Jan 6, 2010
2. Jan 7, 2010

### CompuChip

Whether to use functions or classes is up to you, it depends on your preference.

In the end, you will have coordinates for the position of the droid, which you need to compare to the coordinates of the colonies. I would use the following algorithm:
Code (Text):

int nearestColonyIndex = -1, theDistance = 0, smallestDistance = -1;
for(i = 0; i <= count(colonies); i++) {
theDistance = distance(droidCoordinates, colonies[i].coordinates);
if(nearestColonyIndex == -1 || theDistance < smallestDistance) {
smallestDistance = theDistance;
nearestColonyIndex = i;
}
}
cout << "The nearest colony is at index " << i << " and is named " << colonies[i].name << endl;

int distance(int x1, y1, x2, y2) {
// Calculate distance (squared) by Pythagorean theorem
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}

3. Jan 13, 2010

### Wanna-be Eng.

I managed to do the program, now I need to know how to access a .txt file, so i can take data from it, and also modify data. Using functions and stdio library

4. Jan 13, 2010

### Staff: Mentor

Use the functions whose prototypes are in stdio.h - fopen() for opening a file, fclose() for closing it when you are done, and either fscanf() or fread() for reading from the file. Presumably you have a textbook or at least some documentation that describes these functions and how to use them.