def color_analysis(colors):
n = len(colors)
grid = [list(row) for row in colors]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
def dfs(x, y, grid, visited):
stack = [(x, y)]
visited[x][y] = True
color = grid[x][y]
while stack:
cx, cy = stack.pop()
for i in range(4):
nx = cx + dx[i]
ny = cy + dy[i]
if 0 <= nx < n and 0 <= ny < n:
if not visited[nx][ny] and grid[nx][ny] == color:
visited[nx][ny] = True
stack.append((nx, ny))
def count(grid):
visited = [[False]*n for _ in range(n)]
cnt = 0
for i in range(n):
for j in range(n):
if not visited[i][j]:
dfs(i, j, grid, visited)
cnt += 1
return cnt
normal = count(grid)
weak = []
for row in grid:
new_row = []
for c in row:
if c == "R":
new_row.append("G")
else:
new_row.append(c)
weak.append(new_row)
colorblind = count(weak)
return normal, colorblind
colors = ["RRRGG", "GGBBB", "BBBRR"]
print(color_analysis(colors))
To embed this project on your website, copy the following code and paste it into your website's HTML: