【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 ...
随机推荐
- oracle 的使用
一. docker 模式下进入数据库 ubuntu@jiang:~$ sudo docker ps -a sudo: unable to resolve host jiang CONTAINER ID ...
- Hibernate的优点与缺点
Hibernate优点: 1.对象化.人员以面相对象的思想来操作数据库.Hibernate支持许多面向对象的特性,如组合,继承,多态等. 2.更好的移植性.对于不同的数据库,开发者只需要使用相同的数据 ...
- demo_service
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit ...
- 每天一个linux命令:touch(9)
touch touch命令有两个功能:一是用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来:二是用来创建新的空文件 格式 touch [选项] [文件] 参数 ...
- eureka学习(二)
eureka服务端创建好后,现在我们让eureka客户端(也就是服务提供者)注册到eureka上去. 首先加入依赖包: <!--将微服务provider注册到eureka--> <d ...
- Linux 应用程序编程基础
一个计算机应用程序在内存中可以分成两个部分:存放代码的代码段和存放数据的数据段.代码段存放用户编写的代码;数据段存放栈和堆. 相关内存管理函数 #include <stdlib.h> vo ...
- AcWing 229. 新NIM游戏 (线性基+博弈论)打卡
题目:https://www.acwing.com/problem/content/description/231/ 题意:给出n堆石子,然后第一回合,A玩家可以随便拿多少堆石子,第二回合B玩家随便拿 ...
- ARM系列处理器的分类
1.ARM ARM即以英国ARM(Advanced RISC Machines)公司的内核芯片作为CPU,同时附加其他外围功能的嵌入式开发板,用以评估内核芯片的功能和研发各科技类企业的产品. ARM ...
- PHP-模拟请求和操作响应
模拟请求 fsockopen <?php // 建立连接 $link = fsockopen('localhost', '80'); define('CRLF', "\r\n" ...
- 82、TensorFlow教你如何构造卷积层
''' Created on 2017年4月22日 @author: weizhen ''' import tensorflow as tf #通过tf.get_variable的方式创建过滤器的权重 ...