leetcode 361.Bomb Enemy(lintcode 553. Bomb Enemy)
2024-08-26 11:08:59
dp
分别计算从左到右、从右到左、从上到下、从下到上4个方向可能的值,然后计算所有为‘0’的地方的4个方向的值的最大值
https://www.cnblogs.com/grandyang/p/5599289.html
class Solution {
public:
/**
* @param grid: Given a 2D grid, each cell is either 'W', 'E' or '0'
* @return: an integer, the maximum enemies you can kill using one bomb
*/
int maxKilledEnemies(vector<vector<char> > &grid) {
// write your code here
int m = grid.size();
if(m <= )
return ;
int n = grid[].size();
if(n <= )
return ;
vector<vector<int> > left(m,vector<int>(n,)),right(m,vector<int>(n,)),top(m,vector<int>(n,)),down(m,vector<int>(n,));
for(int i = ;i < m;i++){
for(int j = ;j < n;j++){
int tmp;
if(j == || grid[i][j] == 'W')
tmp = ;
else
tmp = left[i][j-];
if(grid[i][j] == 'E')
left[i][j] = tmp + ;
else
left[i][j] = tmp;
}
}
for(int i = ;i < m;i++){
for(int j = n - ;j >= ;j--){
int tmp;
if(j == n - || grid[i][j] == 'W')
tmp = ;
else
tmp = right[i][j+];
if(grid[i][j] == 'E')
right[i][j] = tmp + ;
else
right[i][j] = tmp;
}
}
for(int j = ;j < n;j++){
for(int i = ;i < m;i++){
int tmp;
if(i == || grid[i][j] == 'W')
tmp = ;
else
tmp = top[i-][j];
if(grid[i][j] == 'E')
top[i][j] = tmp + ;
else
top[i][j] = tmp;
}
}
for(int j = ;j < n;j++){
for(int i = m-;i >= ;i--){
int tmp;
if(i == m- || grid[i][j] == 'W')
tmp = ;
else
tmp = down[i+][j];
if(grid[i][j] == 'E')
down[i][j] = tmp + ;
else
down[i][j] = tmp;
}
}
int max_num = ;
for(int i = ;i < m;i++){
for(int j = ;j < n;j++){
if(grid[i][j] == '')
max_num = max(max_num,left[i][j] + right[i][j] + top[i][j] + down[i][j]);
}
}
return max_num;
}
};
最新文章
- 软件测试工作中涉及的Linux命令整理
- Android之adb命令
- centos svn快速搭建
- poj2392 Space Elevator(多重背包)
- 大数加法(STL list)
- 关于Integer比较问题
- ASP.NET Core教程【二】从保存数据看Razor Page的特有属性与服务端验证
- java的jdk
- ScheduledThreadPoolExecutor Usage
- Android 一个日历控件的实现代码
- Linux sudoers
- Utterance-Wise Recurrent Dropout And Iterative Speaker Adaptation For Robust Monaural Speech Recognition
- 一起来学习linux创建用户useradd命令
- React高级教程(es6)——(1)JSX语法深入理解
- JSON数据的解析和生成(Swift)
- 特性(C# 和 Visual Basic)
- Java如何删除数组中的元素?
- WPF 中的 NameScope
- CentOS7通过 yum安装路径查询方法
- net与树莓派的情缘-安装与卸载MySql(五)
热门文章
- 铁力项目mysql异常处理过程记录
- Spring -07 -AOP [面向切面编程] - 使用注解@+ AspectJ 方式实现环绕/前/后等通知 -超简洁 --静态代理/动态代理{JDK/cglib}
- 图解TCP/IP笔记
- 浏览器缓存控制 以及 在url框中回车、F5 和 Ctrl + F5的区别
- win32gui.EnumWindows
- gdb, pdb笔记
- Mac上搭建Python集成环境
- Tensorflow细节-P84-梯度下降与批量梯度下降
- 最新新浪长连接转为短连接的API与请求示例
- Oracle - 合并查询数据项