岛周边 - 罚款时间限制 - python
问题
您获得了行x col网格,代表一个地图,其中网格[i] [j] = 1代表土地,网格[i] [j] = 0代表水。
网格细胞水平/垂直连接(不是对角线)。网格完全被水包围,恰好有一个岛(即一个或多个连接的陆细胞)。
该岛没有“湖泊”,这意味着内部的水与岛上的水没有连接。一个单元格是一个具有侧长1的正方形。网格是矩形,宽度和高度不超过100。确定岛的周长。
示例1:
输入:grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] 输出:16 说明:周长是上图中的16个黄色条纹。
代码:
IslandPerimeter()是解决方案函数
def islandPerimeter(self, grid: List[List[int]]) -> int:
v=[[0,0]]
q=[[0,0]]
c=0
return self.bfs(grid,v,q,c)
def bfs(self,grid,v,q,c):
if q==[]:
return c
i=q[0][0]
j=q[0][1]
if grid[i][j]:
c+=4
if i-1 > -1:
if [i-1,j] not in v:
q.append([i-1,j])
v.append([i-1,j])
if grid[i-1][j] and grid[i][j]:
c-=1
if j-1 > -1:
if [i,j-1] not in v:
q.append([i,j-1])
v.append([i,j-1])
if grid[i][j-1] and grid[i][j]:
c-=1
try:
a=grid[i+1][j]
if [i+1,j] not in v:
q.append([i+1,j])
v.append([i+1,j])
if grid[i+1][j] and grid[i][j]:
c-=1
except:
pass
try:
a=grid[i][j+1]
if [i,j+1] not in v:
q.append([i,j+1])
v.append([i,j+1])
if grid[i][j+1] and grid[i][j]:
c-=1
except:
pass
del q[0]
return self.bfs(grid,v,q,c)
q = bfs
v =访问阵列
i =行j =列
j =列
C =最终答案
我从(0,0)单元格开始,并使用BFS穿越了图。对于每个岛屿细胞,我将计数增加了4,对于每个岛屿细胞,如果相邻的细胞是一个岛屿细胞,那么对于每个这样的岛屿细胞,我都将计数减少1。 但是我得到的时间限制超过了错误。
Question
You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Example 1:
Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image above.
My code:
islandPerimeter() is the solution function
def islandPerimeter(self, grid: List[List[int]]) -> int:
v=[[0,0]]
q=[[0,0]]
c=0
return self.bfs(grid,v,q,c)
def bfs(self,grid,v,q,c):
if q==[]:
return c
i=q[0][0]
j=q[0][1]
if grid[i][j]:
c+=4
if i-1 > -1:
if [i-1,j] not in v:
q.append([i-1,j])
v.append([i-1,j])
if grid[i-1][j] and grid[i][j]:
c-=1
if j-1 > -1:
if [i,j-1] not in v:
q.append([i,j-1])
v.append([i,j-1])
if grid[i][j-1] and grid[i][j]:
c-=1
try:
a=grid[i+1][j]
if [i+1,j] not in v:
q.append([i+1,j])
v.append([i+1,j])
if grid[i+1][j] and grid[i][j]:
c-=1
except:
pass
try:
a=grid[i][j+1]
if [i,j+1] not in v:
q.append([i,j+1])
v.append([i,j+1])
if grid[i][j+1] and grid[i][j]:
c-=1
except:
pass
del q[0]
return self.bfs(grid,v,q,c)
q=queue for bfs
v=visited array
i=row
j=column
c=final answer
I have started from (0,0) cell and traversed the graph using bfs. For every island cell I have increased the count by 4 and for every island cell if the adjacent cell is an island cell then for every such island cell I have reduced the count by 1.
But I am getting time limit exceeded error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恰好有一个岛
==>
每个陆区都连接到每个Opther Land Tell,
因此您可以简化算法
there is exactly one island
==>
every land cell is connected to every opther land cell
So you can simplify your algorithm