其他分享
首页 > 其他分享> > 凸多边形最优三角剖分(动态规划)

凸多边形最优三角剖分(动态规划)

作者:互联网

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

struct point {
double x;
double y;
};

double w(int a, int b, int c, point* p) {
double d1 = sqrt((p[a].x - p[b].x) * (p[a].x - p[b].x) + (p[a].y - p[b].y) * (p[a].y - p[b].y));
double d2 = sqrt((p[a].x - p[c].x) * (p[a].x - p[c].x) + (p[a].y - p[c].y) * (p[a].y - p[c].y));
double d3 = sqrt((p[c].x - p[b].x) * (p[c].x - p[b].x) + (p[c].y - p[b].y) * (p[c].y - p[b].y));
return d1 + d2 + d3;
}

template<class Type>
void MinWeight(int n, Type** t, int** s,point*p) {
for (auto i = 1; i <= n; i++)t[i][i] = 0;
for (auto r = 2; r <= n; r++) {//当前计算的链长
for (auto i = 1; i <= n - r + 1; i++) {//n-r+1为最后一个r链的前边界
int j = i + r - 1;//就算前边界为i,链长为r的链的后边界
t[i][j] = t[i + 1][j] + w(i - 1, i, j, p);//将链ij划分为A(i) * ( A[i+1:j] )这里实际上就是k=i
s[i][j] = i;
for (int k = i + 1; k < i + r - 1; k++) {
//将链ij划分为( A[i:k] )* (A[k+1:j])
int u = t[i][k] + t[k + 1][j] + w(i - 1, k, j, p);
if (u < t[i][j]) {
t[i][j] = u;
s[i][j] = k;
}
}
}
}
}
void Traceback(int i, int j, int** s)
{
if (i == j) return;
Traceback(i, s[i][j], s);
Traceback(s[i][j] + 1, j, s);
cout << i - 1 << s[i][j] << j << endl;
}
int main()
{
int n;
cin >> n;
point* p = new point[n + 1];
for (auto i = 0; i < n; i++) {
cin >> p[i].x >> p[i].y;
}
double** t = new double* [n + 1];
int** s = new int* [n + 1];
for(auto i = 0; i < n + 1; i++) {
t[i] = new double[n + 1];
s[i] = new int[n + 1];
}
MinWeight(n, t, s, p);
Traceback(1, n-1, s);
}

/*
7
8 26
0 20
0 10
10 0
22 12
27 21
15 26

*/

标签:Traceback,剖分,point,int,double,凸多边形,auto,new,最优
来源: https://www.cnblogs.com/msboke/p/14727087.html