【LeetCode】361. Bomb Enemy 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/bomb-enemy/
题目描述
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: You can only put the bomb at an empty cell.

Example:
Input: [["0","E","0","0"],["E","0","W","E"],["0","E","0","0"]]
Output: 3
Explanation: For the given grid,
0 E 0 0
E 0 W E
0 E 0 0
Placing a bomb at (1,1) kills 3 enemies.
题目大意
请你计算一个炸弹最多能炸多少敌人。
由于炸弹的威力不足以穿透墙体,炸弹只能炸到同一行和同一列没被墙体挡住的敌人。
解题方法
暴力搜索
这是在微软面试遇到的真题,做法其实很直接,就是暴力计算放置在每个空位置上能炸死的一行一列的所有敌人就行了。需要注意的是需要从放炸弹的位置向四个方向进行搜索,并且超出边界或者遇到墙壁就停止。
C++代码如下:
class Solution {
public:
int maxKilledEnemies(vector<vector<char>>& grid) {
if (grid.empty() || grid[0].empty()) return 0;
int res = 0;
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[0].size(); ++j) {
if (grid[i][j] == '0') {
res = max(res, killEnemies(grid, i, j));
}
}
}
return res;
}
int killEnemies(vector<vector<char>>& grid, int x, int y) {
int count = 0;
for (vector<int>& curd : dirs) {
int newx = x;
int newy = y;
while (newx >= 0 && newx < grid.size() && newy >= 0 && newy < grid[0].size()
&& grid[newx][newy] != 'W') {
if (grid[newx][newy] == 'E')
count ++;
newx += curd[0];
newy += curd[1];
}
}
return count;
}
private:
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
};
日期
2019 年 9 月 20 日 —— 是选择中国互联网式加班?还是外企式养生?
【LeetCode】361. Bomb Enemy 解题报告(C++)的更多相关文章
- 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 361.Bomb Enemy(lintcode 553. Bomb Enemy)
dp 分别计算从左到右.从右到左.从上到下.从下到上4个方向可能的值,然后计算所有为‘0’的地方的4个方向的值的最大值 https://www.cnblogs.com/grandyang/p/5599 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- Bedtools如何比较两个参考基因组注释版本的基因?
目录 问题 思路 问题 原问题来自:How to calculate overlapping genes between two genome annotation versions? 其实可分为两个 ...
- Linux—find在指定路径下查找文件或目录
find /指定路径 -name "*filename*" find /指定路径 -name "*filename*" 2>/dev/null ...
- EXCEl-数据透视表按照自定义序列排序
用着感觉挺神奇,也有点奇怪,可能不是很懂里边的原理吧.最后,需要排序的列,应该在数据透视表首列才有效. 参考:https://jingyan.baidu.com/article/bea41d43a53 ...
- nordic 51822 sdk. timer 的使用
它的源代码和头文件分别为app_timer.c/app_timer.h.这是Nordic为我们提供的虚拟定时器,这个定时器不同于硬件上的TIMER,而是基于RTC1实现的一种虚拟定时器,其将定时功能作 ...
- C语言入坑指南-数组之谜
前言 在C语言中,数组和指针似乎总是"暧昧不清",有时候很容易把它们混淆.本文就来理一理数组和指针之间到底有哪些异同. 数组回顾 在分析之前,我们不妨回顾一下数组的知识.数组是可以 ...
- 在Telegraf上报的监控数据中添加固定的标签列
Telegraf作为InfluxData提供的TICK工具栈(由Telegraf, InfluxDB, Chronograf, Kapacitor四个工具的首字母组成)中收集监控数据的一环,功能非常强 ...
- python-3.x- 序列操作
1. list操作 A.添加元素 1 list = ["C++","C", "Java", "Python"] 2 &q ...
- A Child's History of England.10
In the next reign, which was the reign of Edward, surnamed The Elder, who was chosen in council to s ...
- 外网无法访问hdfs文件系统
由于本地测试和服务器不在一个局域网,安装的hadoop配置文件是以内网ip作为机器间通信的ip. 在这种情况下,我们能够访问到namenode机器, namenode会给我们数据所在机器的ip地址供我 ...
- android studio 编译NDK android studio 生成.so文件
详细配置使用请移步:https://www.jianshu.com/p/4c7d9a10933b android studio NDK 编译 第一步: app/build.gradle下面 添加代码: ...