174. Dungeon Game
来源:互联网 发布:聚合tv软件 编辑:程序博客网 时间:2024/05/18 22:11
这是path sum,机器人走迷宫系列的一道hard 新题。难点在于当前格子的血量计算有好几种情况,实现起来有些复杂。
1. 当前空格是血瓶,那么所需血量就是前面一个格子需要的血量减去血瓶的补给。如果血瓶的补给多余需要的,当前所需血量变为1。
memo[i][j]=max(1, pre-map[i][j]) map[i][j]>=0
2. 当前空格是敌人,所需血量就是前面格子减去打怪兽要损失的血量。
memo[i][j]=pre+abs(map[i][j]) map[i][j]<0
不过再仔细研究一下会发现,map[i][j]是负数的时候加上绝对值,正数的时候减去这个数。合起来其实直接就是pre-map[i][j]
int dungeon(vector<vector<int> > map){ int m=map.size(); int n=map[0].size(); int end = map[m-1][n-1]>=0?1:abs(map[m-1][n-1])+1; vector<vector<int> > memo(m,vector<int>(n,end)); for(int i=m-2;i>=0;--i){ memo[i][n-1]=max(1,memo[i+1][n-1]-map[i][n-1]); } for(int j=n-2;j>=0;--j){ memo[m-1][j]=max(1,memo[m-1][j+1]-map[m-1][j]); } for(int i=m-2;i>=0;--i){ for(int j=n-2;j>=0;--j){ int temp=min(memo[i][j+1],memo[i+1][j]); memo[i][j]=max(1,temp-map[i][j]); } } for(auto i:memo){ for(auto j:i) cout<<j<<" "; cout<<endl; } return memo[0][0]; }
同理和之前几道题一样,我们也只需要一维的中间变量,框架是一样的
int dungeon2(vector<vector<int> > map){ int m=map.size(); int n=map[0].size(); int end = map[m-1][n-1]>=0?1:abs(map[m-1][n-1])+1; vector<int> memo(n,end); for(int j=n-2;j>=0;--j){ memo[j]=max(1,memo[j+1]-map[m-1][j]); } for(int i=m-2;i>=0;--i){ memo[n-1]=max(1,memo[n-1]-map[i][n-1]); for(int j=n-2;j>=0;--j){ int temp=min(memo[j+1],memo[j]); memo[j]=max(1,temp-map[i][j]); } } return memo[0]; }
0 0
- [leetcode] 174.Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- 174. Dungeon Game
- 174. Dungeon Game
- LeetCode-174.Dungeon Game
- [LeetCode] 174. Dungeon Game
- 174. Dungeon Game
- 174. Dungeon Game
- LeetCode 174. Dungeon Game
- LeetCode 174. Dungeon Game
- 174. Dungeon Game
- [LeetCode]174. Dungeon Game
- 174. Dungeon Game
- leetCode 174. Dungeon Game
- 174. Dungeon Game
- Leetcode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- java基础(30)--Java中实现CallBack功能
- 70%的人离职因领导这4宗罪
- 易經大意 461_477
- 指针数组和数组指针
- 进程和线程理解
- 174. Dungeon Game
- git diff
- install MySQLdb
- log4j debug写法
- UIBezierPath
- RMQ问题的ST算法
- 上传到jcenter的步骤
- LeetCode 34. Search for a Range
- mysql如何查看未使用的索引