基本的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的解法。。。
什么时候有时间做一下