Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return the maximum enemies you can kill using one bomb.
The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed.
Note that you can only put the bomb at an empty cell.

Example:

For the given grid

 E
E W E
E return . (Placing a bomb at (,) kills enemies)

思路:扫描矩阵,用rowCount表示该点所在行内有多少敌人可见。用colCount[i]表示该点所在列i内有多少敌人可见。然后该点可见的敌人为两者之和。

初始时两值为0.

当我们位于行首或者该行上一格为墙,那么在该行向右扫描直到行尾或者遇见一堵墙,更新rowCount。

当我们位于列首或者该列上一格为墙,那么在该列向下扫描直到列尾或者遇见一堵墙,更新colCount[i]。

时间复杂度O(mn),空间复杂度O(N)。

 class Solution {
public:
int maxKilledEnemies(vector<vector<char>>& grid) {
if (grid.size() == ) return ;
int rowCount = , height = grid.size(), width = grid[].size(), res = ;
vector<int> colCount(width, );
for (int i = ; i < height; i++) {
for (int j = ; j < width; j++) {
if (!j || grid[i][j-] == 'W') {
rowCount = ;
for (int k = j; k < width && grid[i][k] != 'W'; k++)
rowCount += grid[i][k] == 'E';
}
if (!i || grid[i-][j] == 'W') {
colCount[j] = ;
for (int k = i; k < height && grid[k][j] != 'W'; k++)
colCount[j] += grid[k][j] == 'E';
}
if (grid[i][j] == '')
res = std::max(res, rowCount + colCount[j]);
}
}
return res;
}
};

Bomb Enemy -- LeetCode的更多相关文章

  1. [LeetCode] Bomb Enemy 炸弹人

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

  2. LeetCode 361. Bomb Enemy

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

  3. [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 ...

  4. Leetcode: Bomb Enemy

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

  5. 【LeetCode】361. Bomb Enemy 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetco ...

  6. leetcode 361.Bomb Enemy(lintcode 553. Bomb Enemy)

    dp 分别计算从左到右.从右到左.从上到下.从下到上4个方向可能的值,然后计算所有为‘0’的地方的4个方向的值的最大值 https://www.cnblogs.com/grandyang/p/5599 ...

  7. Bomb Enemy

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

  8. Bomb Enemy 炸弹人

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

  9. 361. Bomb Enemy

    这个题确实不会..只能想到naive的做法,不过那样应该是O(n³),不会满足要求. 看TAG是DP,那应该是建立DP[][]记录每点可炸的情况.一个点如果左边/上边是墙,或者左边/上边是边界,就要重 ...

随机推荐

  1. 「6月雅礼集训 2017 Day8」route

    [题目大意] 给出平面上$n$个点,求一条连接$n$个点的不相交的路径,使得转换的方向符合所给长度为$n-2$的字符串. $n \leq 5000$ [题解] 考虑取凸包上一点,然后如果下一个是‘R' ...

  2. 【bzoj】1927 [Sdoi2010]星际竞速

    [算法]最小费用最大流 [题解]跟滑雪略有类似,同样因为可以重复所以不是最小路径覆盖. 连向汇的边容量为1足矣,因为一个点只会出去一次(路径结束). bzoj 1927 [Sdoi2010]星际竞速 ...

  3. UIView显示时遮挡导航栏的方法

    [self.navigationController.view:addSubview];

  4. 如果你也想写个完整的 Vue 组件项目

    1.一个完整的组件项目需要什么? 必要的: 组件构建方式 ( webpack / rollup 之类 ),并提供至少一个主流的输出格式 (ESModule) Demo 及 Demo 源码 文档,可以是 ...

  5. 关于auto-keras训练cnn模型

    # 我在训练自己的人脸分类模型的时候发现图片的维度不能太高,经过很多次测试过后觉得一般人脸图片分为28*28大小训练的效果比较好.建议在使用其训练自己的物体识别模型的时候,尽量把图片压缩到28*28# ...

  6. node.js 开发命令行工具 发布npm包

    新建一个文件夹“nodecmd”: 在nodecmd下新建文件夹bin: 在bin文件夹下新建JavaScript文件hello.js #!/usr/bin/env node console.log( ...

  7. python2 处理urllib/urllib2错误并打印源码

    import urllib2 import urllib url = 'http://localhost/12.php' data = {} data['cmd']='whoami' data = u ...

  8. linux irq 自动探测

    前言 编写驱动的时候,经常会用到中断,这时候我们在驱动初始化时就得申请中断,那么问题来了,中断号是多少呢?以前的中断号在板级相关的头文件里面已经静态定义好了,bsp的代码在内核启动过程也会根据那个帮我 ...

  9. python实战===用python对比两张图片的不同

    from PIL import Image from PIL import ImageChops def compare_images(path_one, path_two, diff_save_lo ...

  10. [How to]简单易用的拷贝Mac文件路径方法

    效果: 在你想拷贝路径的文件夹或者文件上右键会出现 copy path 选项! 实现: 1.打开finder的的Automator组件 2.选择[服务]选项,点击[选取]按钮 3.搜索操作项目中[拷贝 ...