题目很简单的,我就不翻译了,简单来说就是储存每一行的总数,类似于状态压缩
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define MAX(a,b) a>b?a:b 4 5 int Cow_Map[351][351]; 6 7 void Search(const int); 8 9 int main(void) 10 { 11 int N, i, line; 12 while (~scanf("%d", &N)) 13 { 14 Cow_Map[0][0] = 0; 15 for (line = 1; line <= N; line++)//读图 16 for (i = 0; i < line; i++) 17 scanf("%d", &Cow_Map[line][i]); 18 Search(N); 19 } 20 return 0; 21 } 22 23 void Search(const int N) 24 { 25 int line, i, ans = 0; 26 for (line = 2; line <= N; line++) 27 { 28 Cow_Map[line][0] = Cow_Map[line][0] + Cow_Map[line - 1][0]; 29 for (i = 1; i < line - 1; i++) 30 Cow_Map[line][i] = (MAX(Cow_Map[line - 1][i - 1], Cow_Map[line - 1][i])) + Cow_Map[line][i]; 31 Cow_Map[line][line - 1] = Cow_Map[line][line - 1] + Cow_Map[line - 1][line - 2]; 32 } 33 for (i = 0; i < N; i++) 34 ans = MAX(Cow_Map[N][i], ans); 35 printf("%d\n", ans); 36 }
时间: 2024-10-10 00:05:14