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. 一些常见的pro文件配置

    UI_DIR = ./ui #ui文件目录 TARGET = Test #最终生成目标名 DESTDIR = $$PWD/../test #目标生成目录,$$PWD表示当前目录下 DLLDESTDIR ...

  2. 【Hadoop离线基础总结】Apache Hadoop的三种运行环境介绍及standAlone环境搭建

    Apache Hadoop的三种运行环境介绍及standAlone环境搭建 三种运行环境 standAlone环境 单机版的hadoop运行环境 伪分布式环境 主节点都在一台机器上,从节点分开到其他机 ...

  3. 并发工具类——Semaphore

    本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 Semaphore([' seməf :(r)])的主要 ...

  4. zabbix部署与配置

    zabbix部署与配置 1.zabbix的web界面是基于php开发,所以创建lnmp环境来支持web界面的访问 yum install nginx php php-devel php-mysql p ...

  5. javaWeb手动分页步骤

    一:编写实体类@Setter@Getter@ToString@Entity@Repositorypublic class PageBean<T> { private Integer cur ...

  6. Python--oop面向对象的学习1

    类和对象的成员分析 ·类和对象都可以存储成员,成员可以归类为所有,也可以归对象所有 ·类存储成员时使用的是与类关联的一个对象 ·独享存储成员时存储在当前对象中 ·对象访问一个成员,如果对象中没有该成员 ...

  7. 我们为什么推荐在Json中使用string表示Number属性值?

    在这篇简短的文章中,我将解释在使用JSON传输数据时,为什么浮点数或大十进制值应表示为字符串 . long类型引发的诡异情况 长话短说,同事在利用swagger对接后端API时,诡异的发现swagge ...

  8. 如何将你的node服务放到线上服务器

    最近在用node写后端数据处理,以前虽然也用node写数据来进行测试,但是一直都是处于本地使用, 今天想将node作为后端服务来处理数据, 特此,以此博客记录. 第一步,写node 接口, 在本地我们 ...

  9. mysql基本操作汇总

    1.数据库操作 (1)创建数据库 CREATE DATABASE <数据库名>; 例子: CREATE DATABASE IF NOT EXISTS RUNOOB DEFAULT CHAR ...

  10. Netty 中的消息解析和编解码器

    本篇内容主要梳理一下 Netty 中编解码器的逻辑和编解码器在 Netty 整个链路中的位置. 前面我们在分析 ChannelPipeline 的时候说到入站和出站事件的处理都在 pipeline 中 ...