Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Летим в другой сектор*/
- /*
- День 27.
- Поздравляю старший кадет %username%, ваша подготовка закончена и теперь вы капитан! Сегодня вам будут предоставлены настоящий корабль и настоящий И.С.А.А.К. Ваша задача добраться до системы Процион, по заданным вам координатам. На вашем курсе будут 2 заправочные станции, так что воспользуйтесь ими при необходимости. Как и раньше лучшим результатом считается минимальное количество сделанных вами дозаправок, удачи капитан. Используйте весь накопленный опыт для решения этой задачи и будьте осторожны с кефиром на борту!
- Гарантируется, что дальняя от вас станция ближе к конечной цели. Сначала вводятся координаты ближней станции, потом дальней.
- */
- #include <iostream>
- #include <string>
- #include <cmath>
- using namespace std;
- class SpaceShip {
- public:
- // Остаток топлива
- int fuelLeft;
- // Вместимость топливного бака (максимум топлива)
- int fuelMax;
- // Название корабля
- string name;
- // Количество дозаправок
- int fillCount = 0;
- // Потерялся ли корабль в космосе
- bool isLost = false;
- //Координаты корабля
- int X, Y;
- void printStatus() {
- cout << fuelLeft << " fuel left from " << fuelMax << " at the ship " << name << endl;
- cout << "Status: ";
- if (isLost)
- cout << "lost in space";
- else
- cout << "target reached with " << fillCount << " fill up";
- }
- int calculateDistance(int x, int y) {
- return sqrt((X - x)*(X - x) + (Y - y)*(Y - y));
- }
- void flight(int x, int y) {
- int distance = calculateDistance(x, y);
- fuelLeft -= distance;
- if (fuelLeft < 0) {
- fuelLeft = 0;
- isLost = true;
- }
- X = x;
- Y = y;
- }
- void prepare() {
- cin >> name >> fuelLeft >> fuelMax >> X >> Y;
- }
- };
- class SpaceStation {
- public:
- // Название станции
- string name;
- // Расстояние до станции
- int X, Y;
- void prepare() {
- cin >> name >> X >> Y;
- }
- void fillupShip(SpaceShip &ship) {
- int fillCount = ship.fuelMax - ship.fuelLeft;
- ship.fuelLeft = ship.fuelMax;
- ship.fillCount++;
- cout << "Ship " << ship.name << " filled " << fillCount << " tn fuel on station " << name << endl;
- }
- };
- int main() {
- SpaceShip space_ship;
- SpaceStation space_station1, space_station2;
- space_ship.prepare();
- space_station1.prepare();
- space_station2.prepare();
- int finallyX, finallyY;
- cin >> finallyX >> finallyY;
- //Проверяем долетим ли до конечной цели
- int dist = space_ship.calculateDistance(finallyX, finallyY);
- if (dist > space_ship.fuelLeft) {
- // Проверяем дальнюю станцию
- dist = space_ship.calculateDistance(space_station2.X, space_station2.Y);
- if (dist <= space_ship.fuelLeft) {
- space_ship.flight(space_station2.X, space_station2.Y);
- space_station2.fillupShip(space_ship);
- space_ship.flight(finallyX, finallyY);
- } else {
- // Летим на ближнюю
- space_ship.flight(space_station1.X, space_station1.Y);
- if (!space_ship.isLost) {
- // Долетели, проверяем конечную цель
- space_station1.fillupShip(space_ship);
- dist = space_ship.calculateDistance(finallyX, finallyY);
- if (dist > space_ship.fuelLeft) {
- // Летим на вторую станцию
- space_ship.flight(space_station2.X, space_station2.Y);
- if (!space_ship.isLost) {
- space_station2.fillupShip(space_ship);
- space_ship.flight(finallyX, finallyY);
- }
- }
- else space_ship.flight(finallyX, finallyY);
- }
- }
- } else space_ship.flight(finallyX, finallyY);
- space_ship.printStatus();
- return 0;
- }
- /*
- Тест1
- Ввод:
- Firefly 24 24 0 0
- Omicron-1 6 6
- Omicron-2 12 12
- 32 25
- Вывод:
- Ship Firefly filled 16 tn fuel on station Omicron-2
- 1 fuel left from 24 at the ship Firefly
- Status: target reached with 1 fill up
- Тест2
- Ввод:
- Enterprise 100 100 0 0
- Sunrise 50 30
- MoonRise 90 60
- 120 110
- Вывод:
- Ship Enterprise filled 58 tn fuel on station Sunrise
- Ship Enterprise filled 50 tn fuel on station MoonRise
- 42 fuel left from 100 at the ship Enterprise
- Status: target reached with 2 fill up
- Тест3
- Ввод:
- Nostromo 40 50 0 0
- LV-426 20 30
- MoonRise 70 70
- 120 110
- Вывод:
- Ship Nostromo filled 46 tn fuel on station LV-426
- 0 fuel left from 50 at the ship Nostromo
- Status: lost in space
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement