Java实现 LeetCode 803 打砖块 (DFS)
803. 打砖块
我们有一组包含1和0的网格;其中1表示砖块。 当且仅当一块砖直接连接到网格的顶部,或者它至少有一块相邻(4 个方向之一)砖块不会掉落时,它才不会落下。
我们会依次消除一些砖块。每当我们消除 (i, j) 位置时, 对应位置的砖块(若存在)会消失,然后其他的砖块可能因为这个消除而落下。
返回一个数组表示每次消除操作对应落下的砖块数目。
示例 1:
输入:
grid = [[1,0,0,0],[1,1,1,0]]
hits = [[1,0]]
输出: [2]
解释:
如果我们消除(1, 0)位置的砖块, 在(1, 1) 和(1, 2) 的砖块会落下。所以我们应该返回2。
示例 2:
输入:
grid = [[1,0,0,0],[1,1,0,0]]
hits = [[1,1],[1,0]]
输出:[0,0]
解释:
当我们消除(1, 0)的砖块时,(1, 1)的砖块已经由于上一步消除而消失了。所以每次消除操作不会造成砖块落下。注意(1, 0)砖块不会记作落下的砖块。
注意:
网格的行数和列数的范围是[1, 200]。
消除的数字不会超过网格的区域。
可以保证每次的消除都不相同,并且位于网格的内部。
一个消除的位置可能没有砖块,如果这样的话,就不会有砖块落下。
class Solution {
int count=0;
public int[] hitBricks(int[][] grid, int[][] hits) {
int[] ret = new int[hits.length];
// 先把砖块打掉
for(int[] hit : hits){
if(grid[hit[0]][hit[1]] == 1)
grid[hit[0]][hit[1]] = -1;
}
// 贴上最顶层的砖块和与最顶层连接的砖块
for(int k = 0; k < grid[0].length; ++k){
if(grid[0][k] == 1){
isWayToTop(grid, 0, k);
}
}
for(int i = hits.length - 1; i >= 0; --i){
count = 0;
//在删掉之前的情况下,如果能保存的话,就恢复此砖块,因为后面可能有和这个砖块有联系的
//可能导致前面的砖块先over,导致后面的掉了,我们可以把他做恢复模拟
if(canHit(grid, hits[i][0], hits[i][1]) && grid[hits[i][0]][hits[i][1]] == -1){
isWayToTop(grid, hits[i][0], hits[i][1]);
ret[i] = count - 1;
}
}
return ret;
}
public void isWayToTop(int[][] grid, int i, int j){
grid[i][j] = 2;
++ count;
if (i + 1 < grid.length && grid[i + 1][j] == 1) {
isWayToTop(grid, i + 1, j);
}
if (j + 1 < grid[0].length && grid[i][j + 1] == 1) {
isWayToTop(grid, i, j + 1);
}
if (i-1>=0&& grid[i - 1][j] == 1) {
isWayToTop(grid, i - 1, j);
}
if (j - 1 >= 0 && grid[i][j - 1] == 1) {
isWayToTop(grid, i, j - 1);
}
}
public boolean canHit(int [][] grid,int i,int j){
if(i == 0)
return true;
if (i + 1 < grid.length && grid[i + 1][j] == 2) {
return true;
}
if (j + 1 < grid[0].length && grid[i][j + 1] == 2) {
return true;
}
if (i - 1 >= 0 && grid[i - 1][j] == 2) {
return true;
}
if (j - 1 >= 0 && grid[i][j - 1] == 2) {
return true;
}
if(grid[i][j] == -1)
grid[i][j] = 1;
return false;
}
}
Java实现 LeetCode 803 打砖块 (DFS)的更多相关文章
- Java实现 LeetCode 529 扫雷游戏(DFS)
529. 扫雷游戏 让我们一起来玩扫雷游戏! 给定一个代表游戏板的二维字符矩阵. 'M' 代表一个未挖出的地雷,'E' 代表一个未挖出的空方块,'B' 代表没有相邻(上,下,左,右,和所有4个对角线) ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- Java for LeetCode 098 Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- Vue中跨域问题解决方案1
我们需要配置代理.代理可以解决的原因:因为客户端请求服务端的数据是存在跨域问题的,而服务器和服务器之间可以相互请求数据,是没有跨域的概念(如果服务器没有设置禁止跨域的权限问题),也就是说,我们可以配置 ...
- SpringBoot +Vue 前后端分离实例
今天下了Vue,想试一试前后端分离的实现,没想到坑还不少,这里就记录一下我遇到的坑和我的代码: 一.Vue的下载安装:从网上找就好了,没什么问题,除了下载以后,要把镜像库改成淘宝的,要不然太慢了. 二 ...
- 【雕爷学编程】Arduino动手做(63)---TCS3200D颜色识别传感器
37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的.鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为 ...
- vue中使用mixins
Mixins (混合或混入)——定义的是一个对象 1.概念:一种分发Vue组件可复用功能的非常灵活的方式.混入对象可以包含任意组件选项(组件选项:data.watch.computed.methods ...
- dockerfile文档的相关参数
以上是用dockerfile来构建的nginx镜像示例. 一.构建stress压力测试软件镜像 ##(1) 以下是Dockerfile内容(ENTRYPOINT是运行的环境): FROM centos ...
- 我的excel是2003版本的,里边有sheet1、sheet2两个工作表,当使用GetOleDbSchemaTable获取表Schema时,结果是4个,分别为: sheet1 sheet1$ sheet2 sheet2$
原帖地址:http://bbs.csdn.net/topics/310230098 ---------------------------------------------------------- ...
- stm32实现DMX512协议发送与接收(非标)
最近把玩了一下485,期间也接触了dmx512通信协议,该协议主要用于各种舞台灯光的控制当中,进而实现各种光效以及色彩变化.根据标准的512协议,其物理连接与传统上的RS485是完全一致的,并没有什么 ...
- Windows Terminal安装并美化
介绍 Windows Teminal是一款新式.快速.高效.强大的终端应用程序,适用于命令行工具.命令提示符.PowerShell.WSL(Linux子系统)等等的Shell用户,主要功能包括多选项卡 ...
- UIStackView上手教程
https://www.jianshu.com/p/19fbf3ee2840 https://www.cnblogs.com/bokeyuanlibin/p/5693575.html https:// ...
- HDU-6393 Traffic Network in Numazu
题意:给你一个n边n点的无向连通图,两个操作,操作一改变某个边的权值,操作二查询某两个点之间的路径长度. 题解:随便删掉环上一条边搞一棵树出来,因为两点间距离是两点各自到根的距离之和减去2*lca两点 ...