【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 ...
随机推荐
- 关于云计算三大服务模式LAAS,PAAS,SAAS的含义及区别
根据NIST的权威定义,云计算有SPI,即SAAS,PAAS和LAAS三大服务模式,上层是SAAS,中间层是PAAS,底层是LAAS,一层支撑一层. LAAS(Infrastucture-as-a-S ...
- 【leetcode】982. Triples with Bitwise AND Equal To Zero
题目如下: Given an array of integers A, find the number of triples of indices (i, j, k) such that: 0 < ...
- vi快捷操作
全部删除: 按esc键后,先按gg(到达顶部),然后dG 全部复制: 按esc键后,先按gg,然后ggyG 全选高亮显示: 按esc键后,先按gg,然后ggvG或者ggVG 单行复制: 按esc键后, ...
- javaweb判断当前请求是否为移动设备访问的方法
由于移动端和pc端还是稍微有些区别的,我觉得最好是在一个地儿统一判断,而且不要改动原先的代码,这样可以从一定程度上减少bug的数量.我的想法是首先应该判断当前请求是否为移动端,然后设一个标识到sess ...
- CSS分组和嵌套选择器
CSS 分组 和 嵌套 选择器 分组选择器 在样式表中有很多具有相同样式的元素.直线模组哪家好 h1 { color:green; } h2 { color:green; } p { ...
- mpu6050学习
一.MPU6050初始化 /**************************实现函数******************************************** *函数原型: ...
- 函数————count
count和count_if函数是计数函数,先来看一下count函数:count函数的功能是:统计容器中等于value元素的个数. count(first,last,value); first是容器的 ...
- SQL执行计划详解explain
1.使用explain语句去查看分析结果 如explain select * from test1 where id=1;会出现:id selecttype table type possible_k ...
- HashCode方法整理
哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: 1 public native int hashCode(); 根据 ...
- Django框架(二十五)—— Django rest_framework-路由控制与响应器
路由控制与响应器 一.路由控制 # 1.基本路由: url(r'^publish/$', views.PublishView.as_view()), # 2.半自动路径:views.PublishVi ...