Dungeon Game
来源:互联网 发布:smtp tls 端口 编辑:程序博客网 时间:2024/06/03 16:45
题目连接: https://leetcode.com/problems/dungeon-game/description/
题目分析: 使用一个数组health[][]记录Knight在每一个方格所需要 的最小health值,使用动态规划,通过动态转换方程:health[i][j] = min(health[i][j + 1], health[i + 1][j]) - dungeon[i][j] <= 0 ? 1 : min(health[i][j + 1], health[i + 1][j]) - dungeon[i][j],从dungeon的右下向左上进行状态的迁移,最终,Knight所需要的最小health值为health[0][0]。
代码:
class Solution {public: int calculateMinimumHP(vector<vector<int> >& dungeon) { int m = dungeon.size(), n = dungeon[0].size(); int health[m][n]; health[m-1][n-1] = dungeon[m-1][n-1] > 0 ? 1 : abs(dungeon[m-1][n-1]) + 1; for (int i = 0; i < n-1; ++i) { int need = health[m-1][i+1] - dungeon[m-1][i]; health[m-1][i] = need <= 0 ? 1 : abs(need); } for (int i = 0; i < m-1; ++i) { int need = health[i+1][n-1] - dungeon[i][n-1]; health[i][n-1] = need <= 0 ? 1 : abs(need); } for (int i = m-2; i >= 0; --i) for (int j = n-2; j >= 0; --j) { int need = min(health[i][j+1], health[i+1][j]) - dungeon[i][j]; health[i][j] = need <= 0 ? 1 : abs(need); } for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) cout << health[i][j] << " "; return health[0][0]; }};
复杂度: O(m*n).
阅读全文
0 0
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Dungeon Game
- Chrome插件控制视频播放速度(爱奇艺、优酷)
- HDFS中的file与block块之间的对应关系举例
- node 触发监听事件
- 波特率与晶振
- 大数据学习系列之一 ----- Hadoop环境搭建(单机)
- Dungeon Game
- css左右摆动动画
- ROS学习笔记2 消息发布与订阅
- Java 数组的复制
- Python学习笔记1
- 5.8
- [SG函数] BZOJ1188: [HNOI2007]分裂游戏
- 【NOIP2017】滚粗记
- ansible安装