题目如下:

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. 关于Python的post请求报504错误

    这是个奇葩的问题,我也是奇葩的研究了好几天,最后发现,哈,原来是这个原因,在此记录下曲折的心路历程 接口Content-Type没有,body用的是postman中的raw数据,格式是text 程序如 ...

  2. Python--字符编码、文字处理、函数

    了解字符编码的知识储备 我们日常用到的文本编辑器有nodepad++,pycharm,word等等,用他们存取文件的过程大致类似,需要知道打开编辑器就打开了启动了一个进程,是在内存中的,所以在编辑器编 ...

  3. 06-图2 Saving James Bond - Easy Version(25 分)

    This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...

  4. 2019牛客多校第五场H - subsequence 2 拓扑

    H - subsequence 2 题意 要你使用前\(m\)个小写字母构造一个长度为\(n\)的字符串 有\(m*(m-1)/2\)个限制条件: \(c_{1} .c_{2}. len\):表示除去 ...

  5. ThinkPHP示例:图片上传

    ThinkPHP示例之图片上传,包括图片上传.数据库保存.缩略图生成以及图片水印功能演示.首先需要下载框架核心,然后把示例解压到Web根目录下面,并修改入口文件中的框架入口文件的位置.导入示例目录下面 ...

  6. PyCharm 默认快捷键

    1.编辑(Editing) Ctrl + Space    基本的代码完成(类.方法.属性)Ctrl + Alt + Space  快速导入任意类Ctrl + Shift + Enter    语句完 ...

  7. 浏览器 url 编码

    1.问题的由来 : http://www.ruanyifeng.com/blog/2010/02/url_encoding.html 2.网络标准RFC 1738做了硬性规定: 只有字母和数字[0-9 ...

  8. 探索Redis设计与实现8:连接底层与表面的数据结构robj

    本文转自互联网 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial ...

  9. leetcode上的一些栈、队列问题

    20-有效的括号 思路:主要考察栈的一些基本操作,像push()(将数据压入栈顶).top()(取栈顶的数据但不删除).pop()(直接删除栈顶的元素).empty()(判断栈是否为空).这题就是先把 ...

  10. Linux虚拟机网络连接的三种方式

    Bridge桥接模式.NAT模式.Host-only仅主机模式: 桥接模式:虚拟机使用真实网卡进行通信,配置简单:只要和真实计算机在同一个网段内,就可以直接通信:局域网内如果有其他计算机,也可以进行访 ...