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

leetcode 361.Bomb Enemy(lintcode 553. Bomb Enemy)的更多相关文章

  1. LeetCode 361. Bomb Enemy

    原题链接在这里:https://leetcode.com/problems/bomb-enemy/description/ 题目: Given a 2D grid, each cell is eith ...

  2. [LeetCode] 361. Bomb Enemy 炸敌人

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  3. leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)

    914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...

  4. [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈

    Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, ...

  5. UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)

    Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...

  6. Unity3D学习笔记——选择Enemy

    一.步骤: 1.创建三个Cube,并将这三个Cube的Cube的Tag设为Enemy 2.导入第一人称视角的资源 3.创建名为Targeting的C#脚本 4.编写Targeting脚本,并将它附到第 ...

  7. csapp lab2 bomb 二进制炸弹《深入理解计算机系统》

    bomb炸弹实验 首先对bomb这个文件进行反汇编,得到一个1000+的汇编程序,看的头大. phase_1: 0000000000400ef0 <phase_1>: 400ef0: 48 ...

  8. 2017"百度之星"程序设计大赛 - 复赛1001&&HDU 6144 Arithmetic of Bomb【java大模拟】

    Arithmetic of Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. LostRoutes项目日志——敌人精灵Enemy解析

    Enemy类在Enemy.js中,类Enemy类继承自PhysicsSprite,以便于可以使用物理引擎中的一些特性. 原版的Enemy.js: var Enemy = cc.PhysicsSprit ...

随机推荐

  1. Apache的虚拟主机

    一.虚拟主机的分类 基于IP的虚拟主机:一台服务器,多个ip,搭建多个网站 基于端口的虚拟主机:一台服务器,一个ip,利用不同端口,搭建多个网站 基于域名的虚拟主机:一台服务器,一个ip,多个域名,搭 ...

  2. Springboot中自已测试

    签到的测试,需要传入日期,签到7天可获得更多的积分, 构造7天前的签到记录,重写签到方法,进行构造数据 import cn.com.acxiom.coty.api.ws.bean.dto.PointD ...

  3. 《你们都是魔鬼吗》第八次团队作业:第四天Alpha冲刺

    <你们都是魔鬼吗>第八次团队作业:Alpha冲刺 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 你们都是魔鬼吗 作业学习目标 完成最 ...

  4. HihoCoder1236 Scores

    Scores 求五维偏序(≤).强制在线. \(n,q\le 50000\) 烂大街的题解 考虑如果我们能用bitset找到每一维有哪些比询问点小,然后把每一维的结果取交集,得到的就是答案了. 然后对 ...

  5. 通过php安装Imagick扩展给动态gif图片打水印

    通过php安装Imagick扩展给动态gif图片打水印 一直以来php处理图片都是以gd为主流,直到近些年Imagick的使用才渐渐变多. gd通常用来缩放图片,给图片打水印等基本功能,对于复杂效果如 ...

  6. logstash-output-jdbc使用

    项目需要,使用logstash定时读取log文件,并插入mysql数据库中,output使用logstash-output-jdbc插件.该插件不是默认安装的,需要使用命令:bin/logstash- ...

  7. Java - Annotation使用

    本文转载于(这个写的很好):https://www.cnblogs.com/be-forward-to-help-others/p/6846821.html Annotation Annotation ...

  8. php自定义函数之参数的引用

    在变量这个函数中,我们学习了变量的引用,我们来回顾一下知识:大理石平台支架 <?php $a = 10; $b = &$a; $a = 100; echo $a.'---------'. ...

  9. SpringMVC的数据效验

    Spring MVC本身没有数据校验的功能,它使用Hibernate的校验框架来完成. 1.导入pom节点 <!-- https://mvnrepository.com/artifact/org ...

  10. 转发大神nginx配置详解

    序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...