题目描述
房间里放着n块奶酪。一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处。
输入输出格式
输入格式:
第一行一个数n (n<=15)
接下来每行2个实数,表示第i块奶酪的坐标。
两点之间的距离公式=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))
输出格式:
一个数,表示要跑的最少距离,保留2位小数。
输入输出样例
输入样例#1: 复制
4 1 1 1 -1 -1 1 -1 -1
输出样例#1: 复制
7.41
思路:一开始想的贪心,每次加上离当前点(需维护)距离最近的点,只贪了40分,因为有可能直接从中间点开始会比贪心更近,所以DFS+剪枝 A掉
//神兽勿删
// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ┃┳┛┗┳ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏┛ Code is far away from bug with the animal protecting
// ┃ ┃ 神兽保佑,代码无bug
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ┃ > < ┃
// ┃ ┃
// ┃... ⌒ ... ┃
// ┃ ┃
// ┗━┓ ┏┛
// ┃ ┃ Code is far away from bug with the animal protecting
// ┃ ┃ 神兽保佑,代码无bug
// ┃ ┃
// ┃ ┃
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
#include<bits/stdc++.h>
using namespace std;
#define maxn 15
typedef long long ll;
ll n;
bool flag[maxn];
long double ans=1e18;
struct Why
{
long double x,y;
}A[maxn];
void dfs(ll num,long double s,long double a,long double b) // 记得传进来的坐标可以不是整数
{
if(s > ans) //剪枝
return ;
if(num == n)
{
ans=min(ans,s);
return ;
}
//cout << "本次 " << num << " " << s << " " << a << " " << b << endl;
for(ll i = 0; i < n; i++)
{
if(flag[i] == 0)
{
flag[i] = 1;
dfs(num+1,sqrt((a-A[i].x)*(a-A[i].x)+(b-A[i].y)*(b-A[i].y))+s ,A[i].x,A[i].y);
flag[i] = 0;
}
}
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n;
for(ll i = 0; i < n; i++)
cin >> A[i].x >> A[i].y;
dfs(0,0,0,0);
cout << fixed << setprecision(2) << ans << endl;
return 0;
}
..