Tour Guide 解题报告 Kattis 暴力求解法

解题思路:
一共只有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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值