dfs 90pts 求调
  • 板块题目总版
  • 楼主mcturtle
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/6/23 22:07
  • 上次更新2024/6/24 12:47:39
查看原帖
dfs 90pts 求调
823830
mcturtle楼主2024/6/23 22:07

看很多ds都用状压dp,但本人是个蒟蒻不会

//dfs + 回溯 = ?
#include<bits/stdc++.h>
using namespace std;
struct node{//x、y结构体
    double x, y;
}a[17];
int n;
double ans = 0x3f3f3f3f;
bool v[17];//标记数组
double Euclid_distance(int xx, int yy){//欧几里得距离公式
    return sqrt((a[xx].x - a[yy].x) * (a[xx].x - a[yy].x) + (a[xx].y - a[yy].y) * (a[xx].y - a[yy].y));
}
void dfs(int xx, int de, double d){//回溯搜索(dfs)
    if(d >= ans)return;//边界
    if(de == n){
        ans = min(d, ans);//更改最小值 & 边界
        return;
    }
     for(int i = 1;i <= n;i++){//开始搜索
        if(i == xx)continue;//同样是边界
        if(!v[i]){//未标记
            v[i] = 1;//标记
            dfs(i, de+1, d + Euclid_distance(xx, i));//搜索
            v[i] = 0;//回溯
        }
    }
    return;
}
int main(){
    cin >> n;
    for(int i = 1;i <= n;i++)cin >> a[i].x >> a[i].y;
    dfs(0, 0, 0.0);//全是0!!!!
    printf("%.2lf", ans);//输出
    return 0;
}
2024/6/23 22:07
加载中...