Skip to content

Instantly share code, notes, and snippets.

@pruthvi6767
Last active November 21, 2019 17:22
Show Gist options
  • Save pruthvi6767/456e492dc2966b5d93cdfef7f796c4aa to your computer and use it in GitHub Desktop.
Save pruthvi6767/456e492dc2966b5d93cdfef7f796c4aa to your computer and use it in GitHub Desktop.
from collections import deque
s = set()
q = deque()
count = 0
pictures = ['aaabc', 'aabbc', 'aaaac']
#pictures = ['ace', 'acd']
for i in range(len(pictures)):
for j in range(len(pictures[0])):
if (i, j) not in s:
q.append((i, j))
while q:
k, l = q.popleft()
if (k, l) not in s:
if k < len(pictures)-1 and l <= len(pictures[i])-1:
if (k+1, l) not in s and pictures[k][l] == pictures[k+1][l] and (i+1, j) not in q:
q.append((k+1, l))
if k <= len(pictures)-1 and l < len(pictures[i])-1:
if (k, l+1) not in s and pictures[k][l] == pictures[k][l+1] and (k, l+1) not in q:
q.append((k, l+1))
if k > 0 and l > 0 and k <= len(pictures)-1:
if (k-1, l) not in s and pictures[k][l] == pictures[k-1][l] and (i-1, j) not in q:
q.append((k-1, l))
if k > 0 and l > 0 and k <= len(pictures)-1:
if (k, l-1) not in s and pictures[k][l] == pictures[k][l-1] and (i, j-1) not in q:
q.append((k, l-1))
s.add((k, l))
count += 1
print(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment