动态规划问题初涉

  

1
/* 2 *动态递归算法 3 *Q1:从以下三角形找出一条路径使得值最大 4 */ 5 #include <iostream> 6 using namespace std; 7 #define MAX (5) 8 int array[MAX+1][MAX+1]; 9 10 /* 11 *从屏幕获取有效数据 12 *N:column of array 13 */ 14 void get_data(int N) 15 { 16 int i,j; 17 for (i=1;i<=N;i++) 18 { 19 for (j=1;j<=i;j++) 20 { 21 cin >> array[i][j]; 22 } 23 } 24 } 25 26 27 28 /* 29 *动态规划递归算法 30 *该算法的主要思路: 31 *从一开始 已知第一行的最大值 剩余的问题就是求第一行以下的最大值 于是利用递归算法 32 *i,j: 33 */ 34 int max_sum(int i,int j,int N) 35 { 36 static int x; 37 static int y; 38 if ( i == N) 39 { 40 return array[i][j]; 41 } 42 else 43 { 44 x = max_sum(i+1,j,N); 45 y = max_sum(i+1,j+1,N); 46 return (x>y?x:y) + array[i][j]; 47 } 48 } 49 50 51 52 int main(void) 53 { 54 int N; 55 cout << "Input Coloum:" << endl; 56 cin >> N; 57 cout << "Input datas per column:" << endl; 58 get_data(N); 59 cout <<"Max_Sum:"; 60 cout<<max_sum(1,1,N)<<endl; 61 return 0; 62 } 63 64 65 66 /* 67 *总结:上述算法中存在重复计算的问题 68 *因此对于数据量较大的话会出现时间超时的情况 69 *下面设计一种不重复计算的情况 70 */ 71 72 73 74 75 /* 76 *动态递归算法 77 *Q1:从以下三角形找出一条路径使得值最大 78 */ 79 #include <iostream> 80 using namespace std; 81 #define MAX (5) 82 int array[MAX+1][MAX+1]; 83 int _max[MAX+1][MAX+1]; //带有记忆性的动态搜索规划 84 /* 85 *从屏幕获取有效数据 86 *N:column of array 87 */ 88 void get_data(int N) 89 { 90 int i,j; 91 for (i=1;i<=N;i++) 92 { 93 for (j=1;j<=i;j++) 94 { 95 cin >> array[i][j]; 96 _max[i][j] = -1; 97 } 98 } 99 } 100 101 102 103 /* 104 *动态规划递归算法 105 *该算法的主要思路: 106 *从一开始 已知第一行的最大值 剩余的问题就是求第一行以下的最大值 107 *于是利用递归算法 每次求得下一行的最大值即可 108 *递归停止的条件就是 遇到最后一行 109 *i,j: 110 */ 111 int max_sum(int i,int j,int N) 112 { 113 static int x; 114 static int y; 115 //当前点的最大路径已经被求出,不需要再求,直接返回。 116 if ( _max[i][j] != -1 ) 117 { 118 return _max[i][j]; 119 } 120 if ( i == N) 121 { 122 return array[i][j]; 123 } 124 else 125 { 126 x = max_sum(i+1,j,N); 127 y = max_sum(i+1,j+1,N); 128 _max[i][j] = (x>y?x:y) + array[i][j]; 129 return _max[i][j]; 130 } 131 } 132 133 134 135 int main(void) 136 { 137 int N; 138 cout << "Input Coloum:" << endl; 139 cin >> N; 140 cout << "Input datas per column:" << endl; 141 get_data(N); 142 cout <<"Max_Sum:"; 143 cout<<max_sum(1,1,N)<<endl; 144 return 0; 145 } 146 /* 147 *总结:这个程序较好的解决了重复计算的问题 148 *但是现在可以换一个思路,前面是由上到下的计算, 149 *现在可以尝试由下到上的计算。 150 */ 151 #include <iostream> 152 using namespace std; 153 #define MAX (5) 154 int array[MAX+1][MAX+1]; 155 int _max[MAX+1][MAX+1]; //带有记忆性的动态搜索规划 156 /* 157 *从屏幕获取有效数据 158 *N:column of array 159 */ 160 void get_data(int N) 161 { 162 int i,j; 163 for (i=1;i<=N;i++) 164 { 165 for (j=1;j<=i;j++) 166 { 167 cin >> array[i][j]; 168 } 169 } 170 } 171 172 173 174 175 176 int main(void) 177 { 178 int N,i,j; 179 cout << "Input Coloum:" << endl; 180 cin >> N; 181 cout << "Input datas per column:" << endl; 182 get_data(N); 183 cout <<"Max_Sum:"; 184 for (i=1;i<N;i++) 185 { 186 _max[n][j] = _max[n][i]; 187 } 188 189 190 191 for (int i=N-1;i>=1;--i) 192 { 193 for (int j=1;j<=i;++j) 194 { 195 _max[i][j] = ( _max[i+1][j] > _max[i+1][j+1]?_max[i+1][j]:_max[i+1][j+1]) + array[i][j]; 196 } 197 } 198 cout<<_max[1][1]<<endl; 199 return 0; 200 }

 

posted @ 2017-06-18 20:42  XQ25478  阅读(77)  评论(0编辑  收藏  举报