更改2D数组中某些索引内的值

时间:2017-03-25 15:28:32

标签: arrays list logic 2d edit

有人可以帮助我解决如何在2d阵列中删除周围1s内的字母'W'的逻辑。 外面的'W'将保留,不应该改变。

更可能的是,这就像一个寻路,如果没有办法走向'W',它将被删除或改为0的值。

起点在右侧。

From:
            { W, W, W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}
            { 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0}
            { 0, 1, 1, 0, W, W, W, 0, 1, 1, 1, 1, 0}
            { 0, 1, 1, 0, w, w, w, 0, 1, 1, 1, 1, 0}
            { 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0}
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

To:
            { W, W, W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}
            { 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0}
            { 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0}
            { 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0}
            { 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0}
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

From:
            { W, W, W, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}
            { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}
            { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            { 0, 1, 1, 0, W, W, W, 0, 0, 0, 0, 0, 0}
            { 0, 1, 1, 0, w, w, w, 0, 0, 0, 0, 0, 0}
            { 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

    To:
            { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0}
            { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0}
            { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}
            { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            { 1, 1, 1, 0, W, W, W, 0, 0, 0, 0, 0, 0}
            { 1, 1, 1, 0, w, w, w, 0, 0, 0, 0, 0, 0}
            { 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

1 个答案:

答案 0 :(得分:0)

您可以从左向右移动以检查边界属性:

  

0,1是边界的起点

     

1,0这是边界的终点

现在使用此属性,您可以找到边界内外的W。找到W的1的内部边界后,您可以轻松地将这些W替换为0。

这是python实现。

W = 'W'                                            #suppose
data = [[ W, W, W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
        [ 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0],
        [ 0, 1, 1, 0, W, W, W, 0, 1, 1, 1, 1, 0],
        [ 0, 1, 1, 0, W, W, W, 0, 1, 1, 1, 1, 0],
        [ 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0],
        [ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
        [ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
        [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

size_x = len(data[0])        # Horizontal Size
size_y = len(data)           # Vertical Size

# Original data array
print ''
for x in data:
    print x
print ''

prop = 0                       # 0 for outside and 1 for inside the boundary
for i in range(size_y):
    for j in range(size_x-1):
    first = data[i][j]
    second = data[i][j+1]
    if(first==0 and second==1):
        prop = 0
    elif(first==1 and second == 0):
        prop = 1

    if(prop == 1):
        if(data[i][j]=='W'):
            data[i][j]=0

                                # Result Data array
for x in data:
    print x

这是最终输出:

enter image description here