洛谷 P1433 吃奶酪 (dfs)

基本的dfs

要注意的地方都有注释

一开始将dis设成全局变量
结果90分 还有一个点TLE
然后改成局部变量就过了 ( ̄. ̄)。。。
不知道为什么
哪位大神知道的 教教我 (○'ω'○)

//P1433 吃奶酪
//2017.2.28

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int n, book[20], cur;
double x[20], y[20], ans = 999999999, dis0;

double _distance(int a, int b){   //注意:1.不可命名成distance  2.函数返回值为浮点型 
//  cout << a << " -> " << b << endl;;
//  cout << x[a] << " " << x[b] << " " << x[a] - x[b] << " " << (x[a] - x[b]) * (x[a] - x[b]) << endl;
//  cout << y[a] << " " << y[b] << " " << y[a] - y[b] << " " << (y[a] - y[b]) * (y[a] - y[b]) << endl;
//  cout << (x[a] - x[b]) * (x[a] - x[b]) + (y[a] - y[b]) * (y[a] - y[b]) << endl;
//  cout << sqrt((x[a] - x[b]) * (x[a] - x[b]) + (y[a] - y[b]) * (y[a] - y[b])) << endl;
//  cout << endl;
    return sqrt((x[a] - x[b]) * (x[a] - x[b]) + (y[a] - y[b]) * (y[a] - y[b]));
}

void dfs(int num, int cur, double dis){   //即将吃第num个,现位于第cur个,已走了dis的距离
//  cout << num << "  "  << cur << "  " << dis << endl;
    if (dis > ans) return ;   //Cut
    if (num > n){
        ans = min(ans, dis);
        return ;
    }
    for (int i = 1; i <= n; i++){   //下一步往哪儿 
//      if (i == cur) continue; 
        if (book[i] == 0){
            book[i] = 1;
//          cout << cur << " -> " << i << ": " << _distance(cur, i) << endl;
            dfs(num + 1, i, dis + _distance(cur, i));
            book[i] = 0;
        }
    }
}

int main(){
    cin >> n;
    x[0] = y[0] = 0;   //老鼠一开始在(0,0)点处
    for (int i = 1; i <= n; i++)
        scanf("%lf%lf", &x[i], &y[i]);

    dfs(1, 0, 0);

    printf("%.2lf", ans);

    return 0;
}

好像还有一种DP的解法。。。
什么时候有时间做一下

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值