解题思路:
一共只有8个老人
n<=8
可以全排列枚举 8!
比较一下,哪种用时最少。
计算距离:用坐标计算距离。
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int n;
struct Olds//老人们
{
double x, y, v, theta;
};
Olds con [9], tempcon[9];
int pos[9];
double tour_x, tour_y, tour_v;
double t; //导游的信息
double res;
int main() {
std::ios::sync_with_stdio(false);
while (scanf("%d", &n) == 1 && n) {
res = 99999999;
scanf("%lf", &tour_v);
for (int i = 0; i < n; i++) {
scanf("%lf%lf%lf%lf", &con[i].x, &con[i].y, &con[i].v, &con[i].theta);
tempcon[i].x = con[i].x; tempcon[i].y = con[i].y;
}
// for (int i = 0; i < n; i++)
// {
// cout << con[i].x << " " << con[i].y << endl;
// }
for (int i = 0; i < n; i++)
{
pos[i] = i;//初始化第一种排列
}
do {
tour_x = 0; tour_y = 0;
t = 0;
for (int i = 0; i < n; i++) {
con[i].x = tempcon[i].x;
con[i].y = tempcon[i].y;
}
double a, b, c, x1, back_time, temptime = 0, s;
for (int j = 0; j < n; j++) {
// cout << pos[j]<<endl;
int index = pos[j];
a = pow(con[index].v, 2) - pow(tour_v, 2);
b = 2 * con[index].v * ((con[index].x - tour_x) * cos(con[index].theta) + (con[index].y - tour_y) * sin(con[index].theta));
c = pow(con[index].x - tour_x, 2) + pow(con[index].y - tour_y, 2);
s = b * b - 4 * a * c;
// x1 = (-b + sqrt(s)) / (2 * a);
// if ( x1 < 0 )
// {
// x1 = (-b - sqrt(s)) / (2 * a);
// }
// cout << x1 << endl;
// cout << (-b - sqrt(s)) / (2 * a) << endl;
x1 = max ((-b + sqrt(s)) / (2 * a), (-b - sqrt(s)) / (2 * a));
for (int k = 0; k < n; k++) {
con[k].x = con[k].x + con[k].v * x1 * cos(con[k].theta);
con[k].y = con[k].y + con[k].v * x1 * sin(con[k].theta);
}
tour_x = con[index].x; tour_y = con[index].y; t += x1;
back_time = sqrt(tour_x * tour_x + tour_y * tour_y) / con[index].v;
temptime = max(temptime, t + back_time);
}
res = min(res , temptime);
} while (next_permutation(pos, pos + n));
cout << round(res) << endl;
}
return 0;
}