959. Regions Cut By Slashes

题目来源:959. Regions Cut By Slashes  

https://leetcode.com/problems/regions-cut-by-slashes/

 自我感觉难度/真实难度:             写题时间时长:
 题意:
 分析:思路就是把每一个符号位置,扩展成3*3的小格子,然后把自己的边界设为1就可以了,最后使用数区域的方法就可以了,DFS一下,看有多少区域

 

 自己的代码:
import numpy
class Solution(object):
    def regionsBySlashes(self, g):
        """
        :type grid: List[str]
        :rtype: int
        """
        self.l=len(g)*3
        count=0
        b=numpy.zeros((self.l,self.l))
        for i in range(self.l/3):
            for j in range(self.l/3):
                if g[i][j] == '/':
                    b[i * 3][j * 3 + 2] = b[i * 3 + 1][j * 3 + 1] = b[i * 3 + 2][j * 3] = 1 
                if g[i][j] == '\\':
                    b[i * 3][j * 3] = b[i * 3 + 1][j * 3 + 1] = b[i * 3 + 2][j * 3 + 2] = 1;
        for i in range(self.l):
            for j in range(self.l):
                if b[i][j]==0:
                    count+=1
                    self.one(i,j,b)
        return count
    
    def one(self,x,y,a):
        left_x=x-1
        right_x=x+1
        up_y=y-1
        down_y=y+1
        if 0<=x<self.l and 0<=y<self.l and a[x,y]==0:
            a[x][y]=1
            self.one(left_x,y,a)
            self.one(right_x,y,a)
            self.one(x,up_y,a)
            self.one(x,down_y,a)
        else:
            return 
            
        
            

代码还是要讲求简介

 
代码效率/结果:

 优秀代码:

class Solution(object):
    def regionsBySlashes(self, grid):
        """
        :type grid: List[str]
        :rtype: int
        """
        # colors = [down, right]
        N = len(grid)
        mapping = collections.defaultdict(set)
        colors = [[i,0] for i in xrange(N)]
        color = N-1
        for row in grid:
#            print colors, row
            for i, ch in enumerate(row):
                if i > 0:
                    right = colors[i-1][1] 
                else:
                    color += 1
                    right = color
                down = colors[i][0]
#                print i, ch, right, down
                if ch == ' ':
                    if right != down:
                        mapping[right].add(down)
                        mapping[ down].add(right)
                    colors[i] = [down, down]
                elif ch == '/':
                    if right != down:
                        mapping[right].add(down)
                        mapping[ down].add(right)
                    color += 1
                    colors[i] = [color, color]
                else:
                    colors[i] = [right, down]       
        
#        print colors

        color += 1
#        print color, mapping
        visited, count = [False] * color, 0
        for item in xrange(color):
            if visited[item]: continue
            count += 1
            level, visited[item] = [item], True
            while level:
                co = level.pop()
                for c in mapping[co]:
                    if not visited[c]:
                        level.append(c)
                        visited[c] = True
        
        return count
            

44m

但是目前还没有看懂这么写出来的

 

代码效率/结果:
 自己优化后的代码:

 反思改进策略:

转载于:https://www.cnblogs.com/captain-dl/p/10887778.html

`cv2.grabCut()`函数是一个图像分割算法,它可以将图像分成前景和背景两部分。通常情况下,我们希望将感兴趣的对象(前景)从背景中分离出来,以便在后续的处理中更好地进行分析。 下面是`cv2.grabCut()`函数的语法和参数: ```python cv2.grabCut(img, mask, rect, bgdModel, fgdModel, iterCount, mode=None) ``` 其中: - `img`:要分割的输入图像。 - `mask`:指定前景、背景和未确定区域(可能是前景或背景)的掩码图像。在函数执行后,掩码图像将被修改以反映分割结果。 - `rect`:一个矩形框,用于指定前景区域的大致位置。它应该是一个四元组`(x,y,w,h)`,其中`(x,y)`是矩形框的左上角坐标,`w`和`h`是矩形框的宽度和高度。 - `bgdModel`和`fgdModel`:这些参数是抓取算法的内部模型,需要提供两个大小为`(1,65)`的NumPy数组。在函数执行后,这些数组将被修改以反映算法的学习结果。 - `iterCount`:指定算法执行的迭代次数。 - `mode`:指定算法执行的模式,可以是`cv2.GC_INIT_WITH_RECT`或`cv2.GC_INIT_WITH_MASK`之一。如果使用前一个选项,则算法将使用`rect`参数指定的矩形框作为前景区域的初始估计。如果使用后一个选项,则算法将使用`mask`参数指定的掩码图像作为前景和背景区域的初始估计。 下面是一个例子,演示如何使用`cv2.grabCut()`函数来分割一张图像: ```python import cv2 import numpy as np img = cv2.imread('input.jpg') mask = np.zeros(img.shape[:2], np.uint8) # specify the foreground and background regions rect = (50, 50, 300, 500) # get the grabcut algorithm's internal models bgdModel = np.zeros((1,65), np.float64) fgdModel = np.zeros((1,65), np.float64) # run the grabcut algorithm cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT) # modify the mask to include the probable foreground and background regions mask[(mask==cv2.GC_PR_BGD) | (mask==cv2.GC_BGD)] = 0 mask[(mask==cv2.GC_PR_FGD) | (mask==cv2.GC_FGD)] = 1 # apply the mask to the original image result = cv2.bitwise_and(img, img, mask=mask) cv2.imshow('Input Image', img) cv2.imshow('Grabcut Result', result) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在这个例子中,我们首先读入了一张图像`input.jpg`。然后,我们创建了一个大小与输入图像相同的掩码图像,并将其全部初始化为0(未知区域)。接下来,我们指定了一个矩形框,用于指定前景区域的大致位置。然后,我们创建了两个大小为`(1,65)`的NumPy数组,用于存储抓取算法的内部模型。接下来,我们运行了`cv2.grabCut()`函数,将输入图像、掩码图像、矩形框和内部模型作为参数传递给它。在函数执行后,我们修改了掩码图像,以便它包含概率前景和背景区域的像素。最后,我们使用`cv2.bitwise_and()`函数将掩码应用于原始图像,并将结果显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值