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;
}
};

最新文章

  1. 软件测试工作中涉及的Linux命令整理
  2. Android之adb命令
  3. centos svn快速搭建
  4. poj2392 Space Elevator(多重背包)
  5. 大数加法(STL list)
  6. 关于Integer比较问题
  7. ASP.NET Core教程【二】从保存数据看Razor Page的特有属性与服务端验证
  8. java的jdk
  9. ScheduledThreadPoolExecutor Usage
  10. Android 一个日历控件的实现代码
  11. Linux sudoers
  12. Utterance-Wise Recurrent Dropout And Iterative Speaker Adaptation For Robust Monaural Speech Recognition
  13. 一起来学习linux创建用户useradd命令
  14. React高级教程(es6)——(1)JSX语法深入理解
  15. JSON数据的解析和生成(Swift)
  16. 特性(C# 和 Visual Basic)
  17. Java如何删除数组中的元素?
  18. WPF 中的 NameScope
  19. CentOS7通过 yum安装路径查询方法
  20. net与树莓派的情缘-安装与卸载MySql(五)

热门文章

  1. 铁力项目mysql异常处理过程记录
  2. Spring -07 -AOP [面向切面编程] - 使用注解@+ AspectJ 方式实现环绕/前/后等通知 -超简洁 --静态代理/动态代理{JDK/cglib}
  3. 图解TCP/IP笔记
  4. 浏览器缓存控制 以及 在url框中回车、F5 和 Ctrl + F5的区别
  5. win32gui.EnumWindows
  6. gdb, pdb笔记
  7. Mac上搭建Python集成环境
  8. Tensorflow细节-P84-梯度下降与批量梯度下降
  9. 最新新浪长连接转为短连接的API与请求示例
  10. Oracle - 合并查询数据项