0

Can someone help me with the logic on how to remove the letter 'W' inside the surrounding 1s in a 2d array. The outside 'W' will remain and shouldn't be change.

More likely, this is like a pathfinding, if there is no way towards the 'W', it will be deleted or change to the value of 0.

The starting point is at the right side.

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}
javurtez
  • 51
  • 2

1 Answers1

0

You can move from left to right to check boundary properties which are :

0, 1 which is the start of the boundary

1,0 which is the end of the boundary

Now using this property you can find out the W's inside or outside the boundary. After finding W's inside boundary of 1's you can easily replace these W's to 0.

Here is python implementation.

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

Here is the final output:

enter image description here

Community
  • 1
  • 1
Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84