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)的更多相关文章

  1. Java实现 LeetCode 529 扫雷游戏(DFS)

    529. 扫雷游戏 让我们一起来玩扫雷游戏! 给定一个代表游戏板的二维字符矩阵. 'M' 代表一个未挖出的地雷,'E' 代表一个未挖出的空方块,'B' 代表没有相邻(上,下,左,右,和所有4个对角线) ...

  2. 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 ...

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

  4. Java for LeetCode 047 Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  5. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  6. 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 ...

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

  8. Java for LeetCode 090 Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  9. 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. ...

随机推荐

  1. LeetCode--Array--Remove Element && Search Insert Position(Easy)

    27. Remove Element (Easy)# 2019.7.7 Given an array nums and a value val, remove all instances of tha ...

  2. Boosting算法总结(ada boosting、GBDT、XGBoost)

    把之前学习xgb过程中查找的资料整理分享出来,方便有需要的朋友查看,求大家点赞支持,哈哈哈 作者:tangg, qq:577305810 一.Boosting算法 boosting算法有许多种具体算法 ...

  3. JDBC14 ORM03 JavaBean封装

    Javabean对象封装一条信息(推荐) 让JavaBean的属性名和类型尽量和数据库保持一致 一条记录对应一个对象,将这些查询到的对象放到容器中(List) 表信息如下 List封装多条信息 Con ...

  4. 设计模式GOF23大纲

    创建型模式: 单例模式,工厂模式,抽象工厂模式 结构型模式: 适配器模式,桥接模式,装饰模式,组合模式,外观模式,享元模式,代理模式 行为型模式: 模板方法模式,命令模式,迭代器模式,观察者模式,中介 ...

  5. [hdu3631]背包或中途相遇法

    暴力的背包: #pragma comment(linker, "/STACK:10240000,10240000") #include <iostream> #incl ...

  6. git --添加多个文件

    今天测试,发现之前写的auto testcase,有好多发生了改变,因此需要修改脚本重新上传至git当中. 对好几个test case script 进行了修改,之前只是一个一个的修改,这次是多个,经 ...

  7. 编写HTML和CSS几点心得

    HTML代码优化 表单域用fieldset包起来,并用legend说明其用途(注意在css初始化的时候把fieldset的border设为0,把legend的display设为none) 每个inpu ...

  8. 一个学习 Koa 源码的例子

    作者: MarkLin 学习目标: 原生 node 封装 中间件 路由 Koa 原理 一个 nodejs 的入门级 http 服务代码如下, // index.js const http = requ ...

  9. Appium自动化(9) - appium元素定位的快速入门

    如果你还想从头学起Appium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1693896.html 快速入门栗子:boss直聘 app ...

  10. EOS基础全家桶(十二)智能合约IDE-VSCode

    简介 上一篇我们介绍了EOS的专用IDE工具EOS Studio,该工具的优势是简单,易上手,但是灵活性低,且对系统资源开销大,依赖多,容易出现功能异常.那么我们开发人员最容易使用的,可能还是深度定制 ...