题目如下:

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. 1 <= grid.length == grid[0].length <= 30
  2. 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的更多相关文章

  1. 【LeetCode】959. Regions Cut By Slashes 由斜杠划分区域(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 日期 题目地址:https://leetcod ...

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

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

  4. 【leetcode】Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  5. 【leetcode】Surrounded Regions(middle)☆

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  6. 【LeetCode】BFS(共43题)

    [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...

  7. 【LeetCode】并查集 union-find(共16题)

    链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence  (2018年11月22日,开始解决hard题) 给 ...

  8. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. 【Flutter学习】之动画实现原理浅析(一)

    一,动画介绍 动画对于App来说,非常的重要.很多App,正是因为有了动画,所以才会觉得炫酷.移动端的动画库有非常的多,例如iOS上的Pop.web端的animate.css.Android端的And ...

  2. 二进制&八进制&十六进制之间的快速转换------ 心算&笔算方法总结

    二进制数                   0&1两种元素: 8进制数                   0-7 八种元素: 十六进制数            0-9,a,b,c,d,e, ...

  3. 自己动手写ORB特征

    通过一些简单的算法修改,使ORB的提取效率加速了5.8倍.编译该程序需要CPU支持SSE指令集. 如果我们能够对特征提取部分进一步并行化处理,则算法还可以有加速的空间. // // Created b ...

  4. 网页head头部meta和link标签使用大全

    <!-- 声明文档使用的字符编码 --> <meta charset="utf-8"> <!-- 声明文档的兼容模式 --> <meta ...

  5. SLA(服务等级协议)

    SLA:Service-Level Agreement的缩写,意思是服务等级协议.是关于网络服务供应商和客户间的一份合同,其中定义了服务类型.服务质量和客户付款等术语. 定义SLA:Service-L ...

  6. Oracle中start with...connect by (prior)子句的用法

    connect by 是结构化查询中用到的,基本语法是:select … from tablenamestart with 条件1connect by 条件2where 条件3; 例:select * ...

  7. leetcode中的一些二分搜索树

    235(最近公共祖先) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left ...

  8. java.lang.String中的replace方法到底替换了一个还是全部替换了。

    你没有看错我说的就是那个最常用的java.lang.String,String可以说在Java中使用量最广泛的类了. 但是我却发现我弄错了他的一个API(也可以说是两个API),这个API是关于字符串 ...

  9. cs224d 作业 problem set2 (一) 用tensorflow纯手写实现sofmax 函数,线性判别分析,命名实体识别

    Hi Dear Today we will use tensorflow to implement the softmax regression and linear classifier algor ...

  10. WebBrowser常用浏览操作

    WebBrowser1.GoHome; //到浏览器默认主页 WebBrowser1.Refresh; //刷新 WebBrowser1.GoBack; //后退 WebBrowser1.GoForw ...