forked from TheBloodthirster/BUAA_Course_Sharing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
601bdb0
commit d224052
Showing
38 changed files
with
502 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
#include <iostream> | ||
using namespace std; | ||
//3d5 凸多边形最优三角剖分 | ||
const int N = 9;//凸多边形边数+1 | ||
int weight[][N] = { { 0,14,25,27,10,11,24,16 }, | ||
{ 14,0,18,15,27,28,16,14 }, | ||
{ 25,18,0,19,14,19,16,10 }, | ||
{ 27,15,19,0,22,23,15,14 }, | ||
{ 10,27,14,22,0,14,13,20 }, | ||
{ 11,28,19,23,14,0,15,18 }, | ||
{ 24,16,16,15,13,15,0,27 }, | ||
{ 16,14,10,14,20,18,27,0 }, };//凸多边形的权 | ||
|
||
int MinWeightTriangulation(int n, int **t, int **s); | ||
void Traceback(int i, int j, int **s);//构造最优解 | ||
int Weight(int a, int b, int c);//权函数 | ||
|
||
int main() | ||
{ | ||
int **s = new int *[N]; | ||
int **t = new int *[N]; | ||
for (int i = 0; i<N; i++) | ||
{ | ||
s[i] = new int[N]; | ||
t[i] = new int[N]; | ||
} | ||
|
||
cout << "g:" << MinWeightTriangulation(N - 1, t, s) << endl; | ||
cout << "最优三角剖分结构为:" << endl; | ||
Traceback(1, 8, s); //s[i][j]记录了Vi-1和Vj构成三角形的第3个顶点的位置 | ||
system("pause"); | ||
return 0; | ||
} | ||
|
||
int MinWeightTriangulation(int n, int **t, int **s) | ||
{ | ||
for (int i = 1; i <= n; i++) | ||
{ | ||
t[i][i] = 0; | ||
} | ||
for (int r = 2; r <= n; r++) //r为当前计算的链长(子问题规模) | ||
{ | ||
for (int i = 1; i <= n - r + 1; i++)//n-r+1为最后一个r链的前边界 | ||
{ | ||
int j = i + r - 1;//计算前边界为r,链长为r的链的后边界 | ||
|
||
t[i][j] = t[i + 1][j] + Weight(i - 1, i, j);//将链ij划分为A(i) * ( A[i+1:j] )这里实际上就是k=i | ||
|
||
s[i][j] = i; | ||
|
||
for (int k = i + 1; k<j; k++) | ||
{ | ||
//将链ij划分为( A[i:k] )* (A[k+1:j]) | ||
int u = t[i][k] + t[k + 1][j] + Weight(i - 1, k, j); | ||
if (u<t[i][j]) | ||
{ | ||
t[i][j] = u; | ||
s[i][j] = k; | ||
} | ||
} | ||
} | ||
} | ||
return t[1][N - 2]; | ||
} | ||
|
||
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 << "三角剖分顶点:V" << i - 1 << ",V" << j << ",V" << s[i][j] << endl; | ||
} | ||
|
||
int Weight(int a, int b, int c) | ||
{ | ||
return weight[a][b] + weight[b][c] + weight[a][c]; | ||
} | ||
|
||
*/ |
Binary file not shown.
28 changes: 28 additions & 0 deletions
28
算法设计与分析/作业/Assignment_1/ConsoleApplication1/ConsoleApplication1.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 14 | ||
VisualStudioVersion = 14.0.24720.0 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ConsoleApplication1", "ConsoleApplication1\ConsoleApplication1.vcxproj", "{EC05B173-879A-4BAE-A260-C1269A382392}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|x64 = Debug|x64 | ||
Debug|x86 = Debug|x86 | ||
Release|x64 = Release|x64 | ||
Release|x86 = Release|x86 | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{EC05B173-879A-4BAE-A260-C1269A382392}.Debug|x64.ActiveCfg = Debug|x64 | ||
{EC05B173-879A-4BAE-A260-C1269A382392}.Debug|x64.Build.0 = Debug|x64 | ||
{EC05B173-879A-4BAE-A260-C1269A382392}.Debug|x86.ActiveCfg = Debug|Win32 | ||
{EC05B173-879A-4BAE-A260-C1269A382392}.Debug|x86.Build.0 = Debug|Win32 | ||
{EC05B173-879A-4BAE-A260-C1269A382392}.Release|x64.ActiveCfg = Release|x64 | ||
{EC05B173-879A-4BAE-A260-C1269A382392}.Release|x64.Build.0 = Release|x64 | ||
{EC05B173-879A-4BAE-A260-C1269A382392}.Release|x86.ActiveCfg = Release|Win32 | ||
{EC05B173-879A-4BAE-A260-C1269A382392}.Release|x86.Build.0 = Release|Win32 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
EndGlobal |
112 changes: 112 additions & 0 deletions
112
算法设计与分析/作业/Assignment_1/ConsoleApplication1/ConsoleApplication1/ConsoleApplication1.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#include "stdafx.h" | ||
#include<iostream> | ||
#include<vector> | ||
using namespace std; | ||
|
||
const int N = 8; //八边形 | ||
|
||
//权值函数 | ||
vector<vector<int>> weight = { | ||
{ 0,14,25,27,10,11,24,16 }, | ||
{ 14,0,18,15,27,28,16,14 }, | ||
{ 25,18,0,19,14,19,16,10 }, | ||
{ 27,15,19,0,22,23,15,14 }, | ||
{ 10,27,14,22,0,14,13,20 }, | ||
{ 11,28,19,23,14,0,15,18 }, | ||
{ 24,16,16,15,13,15,0,27 }, | ||
{ 16,14,10,14,20,18,27,0 }}; | ||
|
||
//初始化存储数组 | ||
vector<int> tempweight(N, 0); | ||
vector<int> temppoint(N, -1); | ||
// bestweight[i][j] 记录凸子多边形 {Vi, ..., Vj} 三角剖分的最优权值。 | ||
vector<vector<int>> bestweight(N, tempweight); | ||
// bestpoint[i][j] 记录与 Vi、Vj 构成三角形第三个顶点 Vk 。 | ||
vector<vector<int>> bestpoint(N, temppoint); | ||
|
||
|
||
//计算Vi,Vk,Vj组成的三角形的权重之和 | ||
int GetWeight(int i, int k, int j); | ||
|
||
//自底向上动态规划计算n变形最优三角形的权值之和 | ||
int MinWeightTriangulation(int n); | ||
|
||
//打印凸子多边形 {Vi, ..., Vj} 的最优三角剖分结果 | ||
void Traceback(int i, int j); | ||
|
||
int main() { | ||
cout << "凸多边形权重矩阵为:" << endl; | ||
for (int i = 0; i < N; i++) { | ||
for (int j = 0; j < N; j++) { | ||
cout << weight[i][j] << " "; | ||
} | ||
cout << endl; | ||
} | ||
cout << endl; | ||
cout <<"动态规划算法计算结果:" << MinWeightTriangulation(N) << endl; | ||
cout << endl; | ||
cout << "最优三角剖分结构为:" << endl; | ||
Traceback(0, N - 1); | ||
cout << endl; | ||
|
||
cout << "bestPoint[i][j] 记录与 Vi、Vj 构成三角形第三个顶点 Vk 为:" << endl; | ||
for (int i = 0; i < N; i++) { | ||
for (int j = 0; j < N; j++) { | ||
cout << bestpoint[i][j] << "\t"; | ||
} | ||
cout << endl; | ||
} | ||
|
||
system("pause"); | ||
return 0; | ||
} | ||
|
||
|
||
int GetWeight(int i, int k, int j) | ||
{ | ||
return weight[i][k] + weight[k][j] + weight[i][j]; | ||
} | ||
|
||
int MinWeightTriangulation(int n) | ||
{ | ||
//对动态规划数组初始化,这里初始化其实可以不做,前面已经初始化过了 | ||
bestweight[n - 1][n - 1] = 0;//下面初始化会漏掉[n-1][n-1]点 | ||
for (int i = 0; i < n - 1; i++) { | ||
bestweight[i][i] = bestweight[i][i + 1] = 0; | ||
} | ||
|
||
//scale代表子问题的规模大小,例如子问题{V0,V1,V2}的规模为2,子问题{V0,V1...V5}的规模为5 | ||
for (int scale = 2; scale < n; scale++) { | ||
//求解子问题的最后一个为n-scale-1,例如scale=2,最后一个子问题为i=6,j+8,{V6,V7,V8} | ||
for (int i = 0; i < n - scale; i++) { | ||
// j 代表当前以 Vi 为起点的子问题的后边界 Vj | ||
int j = i + scale; | ||
|
||
//先处理 k = i+1的情况,这是为了有一开始的初值方便对比,这里也可以选择初始化最大值9999 | ||
bestweight[i][j] = bestweight[i][i + 1] + bestweight[i + 1][j] + GetWeight(i, i + 1, j); | ||
bestpoint[i][j] = i + 1; | ||
|
||
//有了基准值之后,可以开始循环处理k=i+2的情况 | ||
for (int k = i + 2; k < j; k++) { | ||
int temp = bestweight[i][k] + bestweight[k][j] + GetWeight(i, k, j); | ||
if (temp < bestweight[i][j]) { | ||
bestweight[i][j] = temp; | ||
bestpoint[i][j] = k; | ||
} | ||
} | ||
} | ||
} | ||
|
||
//返回右上角的最佳数值。 | ||
return bestweight[0][n - 1]; | ||
} | ||
|
||
void Traceback(int i, int j) | ||
{ | ||
//注意回溯查找的返回条件,i+1=j表示中间没有任何点存在,bestpoint[i][j]内部的值为出始化-1 | ||
if (i+1 == j) | ||
return; | ||
Traceback(i,bestpoint[i][j]); | ||
cout << "V" << i << " -- V" << bestpoint[i][j] << " -- V" << j << " = " << GetWeight(i,bestpoint[i][j],j) << endl; | ||
Traceback(bestpoint[i][j],j); | ||
} |
163 changes: 163 additions & 0 deletions
163
算法设计与分析/作业/Assignment_1/ConsoleApplication1/ConsoleApplication1/ConsoleApplication1.vcxproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup Label="ProjectConfigurations"> | ||
<ProjectConfiguration Include="Debug|Win32"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|Win32"> | ||
<Configuration>Release</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Debug|x64"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|x64"> | ||
<Configuration>Release</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
</ItemGroup> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>{EC05B173-879A-4BAE-A260-C1269A382392}</ProjectGuid> | ||
<Keyword>Win32Proj</Keyword> | ||
<RootNamespace>ConsoleApplication1</RootNamespace> | ||
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
<ImportGroup Label="ExtensionSettings"> | ||
</ImportGroup> | ||
<ImportGroup Label="Shared"> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<PropertyGroup Label="UserMacros" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<LinkIncremental>false</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<LinkIncremental>false</LinkIncremental> | ||
</PropertyGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<ClCompile> | ||
<PrecompiledHeader>Use</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<SDLCheck>true</SDLCheck> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<ClCompile> | ||
<PrecompiledHeader>Use</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<SDLCheck>true</SDLCheck> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<PrecompiledHeader>Use</PrecompiledHeader> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<SDLCheck>true</SDLCheck> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<PrecompiledHeader>Use</PrecompiledHeader> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<SDLCheck>true</SDLCheck> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<Text Include="ReadMe.txt" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="stdafx.h" /> | ||
<ClInclude Include="targetver.h" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClCompile Include="ConsoleApplication1.cpp" /> | ||
<ClCompile Include="stdafx.cpp"> | ||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader> | ||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader> | ||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader> | ||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader> | ||
</ClCompile> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
Oops, something went wrong.