leetcode 361.Bomb Enemy(lintcode 553. Bomb Enemy)
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)的更多相关文章
- LeetCode 361. Bomb Enemy
原题链接在这里:https://leetcode.com/problems/bomb-enemy/description/ 题目: Given a 2D grid, each cell is eith ...
- [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 ...
- leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)
914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...
- [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, ...
- 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. ...
- Unity3D学习笔记——选择Enemy
一.步骤: 1.创建三个Cube,并将这三个Cube的Cube的Tag设为Enemy 2.导入第一人称视角的资源 3.创建名为Targeting的C#脚本 4.编写Targeting脚本,并将它附到第 ...
- csapp lab2 bomb 二进制炸弹《深入理解计算机系统》
bomb炸弹实验 首先对bomb这个文件进行反汇编,得到一个1000+的汇编程序,看的头大. phase_1: 0000000000400ef0 <phase_1>: 400ef0: 48 ...
- 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 ...
- LostRoutes项目日志——敌人精灵Enemy解析
Enemy类在Enemy.js中,类Enemy类继承自PhysicsSprite,以便于可以使用物理引擎中的一些特性. 原版的Enemy.js: var Enemy = cc.PhysicsSprit ...
随机推荐
- Matlab Cordic 一个数开方代码,适用FPGA
function [sqrt_value] = calsqrt(a)NormKn = ceil(log2(a)) - 1;fprintf("Normalization input data ...
- nodejs模块化标准
commonjs 导出一个 a.js function add(a, b){ return a+b; } module.exports = add; b.js const add = require( ...
- linux系统编程之文件与io(二)
今天继续学习文件与io,话不多说,开始进入正题: 文件的read和write系统调用: 说明:函数中出现在size_t和ssize_t是针对系统定制的数据类型: 下面以一个实现文件简单拷贝的示 ...
- Samba服务安装
安装Samba服务 1.在可以联网的机器上使用yum工具安装,如果未联网,则挂载系统光盘进行安装. # yum install samba samba-client samba-swat 有依赖关 ...
- sentinel主从切换技术
主从切换技术的方法是: 当主服务器宕机后,需要手动把一台从服务器切换为主服务器, 这就需要人工干预,费事费力,还会造成一段时间内服务不可用. 这不是一种推荐的方式,更多时候,我们优先考虑哨兵模式. 一 ...
- SpringBoot基础及FreeMarker模板
案例springboot_freemarker application.properties配置文件 ###FreeMarker配置 spring.freemarker.template-loader ...
- Codevs 1070 普通递归关系(矩阵乘法)
1070 普通递归关系 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 考虑以下定义在非负整数n上的递归关系 f(n) = f0 ...
- SB的SDOISB记
恩, SDOI考完 考完. day0 : 上车时看myj费劲跨过行李的样子,嘲讽他腿短.很happy 吃饭时和myj吃了羊肉..汤 虽然贵, 但是意外的好吃 继续嘲讽myj不是男人,吃的少 不知怎么的 ...
- flutter 省市区选择器 city_pickers 的简单实用
Github地址:https://github.com/hanxu317317/city_pickers packages地址: https://pub.flutter-io.cn/packages/ ...
- P1378 油滴扩展——搜索小记
P1378 油滴扩展 记得这道题好久以前(好像是上个学期?) 就想做了,但是看着里面的半径边界好像很难处理就没做(主要是当时刚学OI(菜还给自己找借口)): 今天上午一直研究SG函数,做的都自闭了,晚 ...