标签:
题目:http://hihocoder.com/problemset/problem/1037
样例输入
5 //迷宫的层数 2 6 4 1 2 8 4 0 9 6 6 5 5 3 6
思路
动态规划
maze[i, j] = max(maze[i-1, j], maze[i-1, j-1])
源码
1 #include <iostream> 2 using namespace std; 3 4 static int maze[100][100];//存储到达每一个房间收集到的最多券 5 static int data[100][100];//存储每一个房间的券 6 7 int main() 8 { 9 int maxLevel, i = 0, max = 0; 10 cin >> maxLevel; 11 12 for (i = 0; i < maxLevel; i++) 13 for (int j = 0; j <= i; j++) 14 cin >> data[i][j]; 15 //由低层到高层计算 16 maze[0][0] = data[0][0]; 17 for (int i = 1; i < maxLevel; i++) 18 { 19 for (int j = 0; j <= i; j++) 20 { 21 if (j > 0 && maze[i - 1][j] < maze[i - 1][j - 1]) 22 maze[i][j] = maze[i - 1][j - 1]; 23 else 24 maze[i][j] = maze[i - 1][j]; 25 maze[i][j] += data[i][j]; 26 } 27 } 28 //找出最后一层中券最多的房间 29 for (i = 0; i < maxLevel; i++) 30 { 31 int tmp = maxLevel - 1; 32 if (max < maze[tmp][i]) 33 max = maze[tmp][i]; 34 } 35 cout << max << endl; 36 return 0; 37 }
标签:
原文地址:http://www.cnblogs.com/coolqiyu/p/5700899.html