【leetcode】959. Regions Cut By Slashes
题目如下:
In a N x N
grid
composed of 1 x 1 squares, each 1 x 1 square consists of a/
,\
, or blank space. These characters divide the square into contiguous regions.(Note that backslash characters are escaped, so a
\
is represented as"\\"
.)Return the number of regions.
Example 1:
Input:
[
" /",
"/ "
]
Output: 2
Explanation: The 2x2 grid is as follows:
![]()
Example 2:
Input:
[
" /",
" "
]
Output: 1
Explanation: The 2x2 grid is as follows:
![]()
Example 3:
Input:
[
"\\/",
"/\\"
]
Output: 4
Explanation: (Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.)
The 2x2 grid is as follows:
![]()
Example 4:
Input:
[
"/\\",
"\\/"
]
Output: 5
Explanation: (Recall that because \ characters are escaped, "/\\" refers to /\, and "\\/" refers to \/.)
The 2x2 grid is as follows:
![]()
Example 5:
Input:
[
"//",
"/ "
]
Output: 3
Explanation: The 2x2 grid is as follows:
![]()
Note:
1 <= grid.length == grid[0].length <= 30
grid[i][j]
is either'/'
,'\'
, or' '
.
解题思路:“小样,你以为穿个马甲我就不认识你了”。如下图,每个square有以下三种状态,同时给这三种状态定义如何转换成3*3的矩阵,在矩阵中,连续的1表示斜杠。如果把grid中所有的square都进行矩阵转换,那么得到的将是一个由0和1组成的 3*len(grid) * 3*len(grid)的矩阵,这个题目就变成了 求岛的数量 的题目。接下来就是DFS/BFS能做的事了。
代码如下:
class Solution(object):
def regionsBySlashes(self, grid):
"""
:type grid: List[str]
:rtype: int
"""
visit = []
newGrid = []
for i in grid:
visit.append([0]*len(i)*3)
visit.append([0] * len(i)*3)
visit.append([0] * len(i) * 3)
newGrid.append([0]*len(i)*3)
newGrid.append([0] * len(i)*3)
newGrid.append([0] * len(i) * 3) for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == '/':
#newGrid[2*i][2*j+1] = newGrid[2*i+1][2*j] = 1
newGrid[3*i][3*j+2] = newGrid[3*i+1][3*j+1] = newGrid[3*i+2][3*j] = 1
elif grid[i][j] == '\\':
#newGrid[2*i][2*j] = newGrid[2*i+1][2*j+1] = 1
newGrid[3*i][3*j] = newGrid[3*i + 1][3*j + 1] = newGrid[3*i+2][3*j+2] = 1 direction = [(0,1),(0,-1),(1,0),(-1,0)]
res = 0
for i in range(len(newGrid)):
for j in range(len(newGrid[i])):
if visit[i][j] == 1 or newGrid[i][j] == 1:
continue
queue = [(i,j)]
visit[i][j] = 1
res += 1
while len(queue) > 0:
x,y = queue.pop(0)
#visit[x][y] = 1
for (x1,y1) in direction:
nextX = x + x1
nextY = y + y1
if nextX >= 0 and nextX < len(newGrid) and nextY >= 0 and nextY < len(newGrid)\
and newGrid[nextX][nextY] == 0 and visit[nextX][nextY] == 0:
visit[nextX][nextY] = 1
queue.append((nextX,nextY))
return res
【leetcode】959. Regions Cut By Slashes的更多相关文章
- 【LeetCode】959. Regions Cut By Slashes 由斜杠划分区域(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 日期 题目地址:https://leetcod ...
- LC 959. Regions Cut By Slashes
In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank space. Th ...
- LeetCode 959. Regions Cut By Slashes
原题链接在这里:https://leetcode.com/problems/regions-cut-by-slashes/ 题目: In a N x N grid composed of 1 x 1 ...
- 【leetcode】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
- 【LeetCode】并查集 union-find(共16题)
链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence (2018年11月22日,开始解决hard题) 给 ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- RGBA的值0-255范围如何转换成0-1范围
这样一个rgba(1,0,0,1) 如果我们要把它转换成 0-255范围 就是rgb分别乘以255 就是 rgba(255,0,0,1) 0-255转0-1范围 如 rgba(34,56,56,1)转 ...
- 在Anaconda环境下使用Jupyter Notebook
!!!Anaconda 和 Jupyter Notebook 在 zsh 环境下不能正常使用! 启动建立的 Anaconda 环境 安装 nb_conda:conda install nb_conda ...
- sparksql 自定义用户函数(UDF)
自定义用户函数有两种方式,区别:是否使用强类型,参考demo:https://github.com/asker124143222/spark-demo 1.不使用强类型,继承UserDefinedAg ...
- 【Zookeekper】分布锁Curator
有序节点:假如当前有一个父节点为/lock,我们可以在这个父节点下面创建子节点:zookeeper提供了一个可选的有序特性,例如我们可以创建子节点“/lock/node-”并且指明有序,那么zooke ...
- Ververica Platform-阿里巴巴全新Flink企业版揭秘
摘要:2019云栖大会大数据 & AI专场,阿里巴巴资深技术专家王峰带来“Ververica Platform-阿里巴巴全新Flink企业版揭秘”的演讲.本文主要从Ververica由来开始谈 ...
- 服务化改造的云上利器 | 阿里云 EDAS 重大升级发布
11月22日,广东云栖大会企业级互联网架构专场上,阿里云发布了全新版本的企业级分布式应用服务EDAS. 新版本增强了对主流微服务框架的原生支持,实现SpringCloud & Dubbo用户代 ...
- 升级ceph
参考文档 https://blog.51cto.com/jerrymin/2140258 https://www.virtualtothecore.com/en/upgrade-ceph-cluste ...
- MySQL图形化管理工具之Navicat安装以及激活
软件以及激活包下载地址 1. 安装navicat 双击navicat_trial_11.1.20.0.1449226634.exe,一路下一步安装(记住安装目录) 2. 激活 双击PatchNavic ...
- Python基础教程(004)--Python的设计哲学
前言 Python已经成为了一门流行的编程语言. 知识点 1,优雅 2,明确 3,简单 Python开发者的哲学是:用一种方法,最好是只有一种方法来做一件事. 如果面临多种选择,Python开发者都会 ...
- AcWing 214. Devu和鲜花 (容斥)打卡
Devu有N个盒子,第i个盒子中有AiAi枝花. 同一个盒子内的花颜色相同,不同盒子内的花颜色不同. Devu要从这些盒子中选出M枝花组成一束,求共有多少种方案. 若两束花每种颜色的花的数量都相同,则 ...