岛周边 - 罚款时间限制 - python

发布于 2025-02-12 07:28:05 字数 1721 浏览 4 评论 0原文

问题

您获得了行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.enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2025-02-19 07:28:05

恰好有一个岛

==>

每个陆区都连接到每个Opther Land Tell,

因此您可以简化算法

  • 搜索单元格,以便订购,例如,左上列列列行,行沿行右下 - 右下 - 在第一次找到陆区时停止。
  • 搜索邻居在水牢房时追踪陆细胞的回程 - 记录所有发现的陆细胞

there is exactly one island

==>

every land cell is connected to every opther land cell

So you can simplify your algorithm

  • Search cells in order e.g. top left column by column row by row to bottom right - stop when first land cell found.
  • Search neighbors for land cells back tracking when water cell - recording all land cells found
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文