[HackerRank][Python3] DFS: Connected Cell in a Grid
2018. 7. 21. 21:12 |
프로그래밍/HackerRank
Problem :
https://www.hackerrank.com/challenges/ctci-connected-cell-in-a-grid/problem
My Solution :
#!/usr/bin/env python3
def dfs(grid, x, y):
if x not in range(len(grid)) or y not in range(len(grid[0])):
return 0
if not grid[x][y]:
return 0
count = 1
grid[x][y] = 0
count += dfs(grid, x+1, y+1)
count += dfs(grid, x+1, y)
count += dfs(grid, x+1, y-1)
count += dfs(grid, x, y+1)
count += dfs(grid, x, y-1)
count += dfs(grid, x-1, y+1)
count += dfs(grid, x-1, y)
count += dfs(grid, x-1, y-1)
return count
def maxRegion(grid):
M = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
M = max(M, dfs(grid, i, j))
return M
n = int(input())
m = int(input())
grid = []
for _ in range(n):
grid.append(list(map(int, input().rstrip().split())))
res = maxRegion(grid)
print(res)
'프로그래밍 > HackerRank' 카테고리의 다른 글
| [HackerRank][Python3] Merge Sort: Counting Inversions (0) | 2018.07.22 |
|---|---|
| [HackerRank][Python3] Roads and Libraries (0) | 2018.07.22 |
| [HackerRank][Python3] BFS: Shortest Reach in a Graph (0) | 2018.07.22 |
| [HackerRank][Python3] Find the nearest clone (0) | 2018.07.22 |
| [HackerRank][Python3] Friend Circle Queries (0) | 2018.07.21 |
| [HackerRank][Python3] Time Complexity: Primality (0) | 2018.07.21 |
| [HackerRank][Python3] Recursion: Davis' Staircase (0) | 2018.07.21 |
| [HackerRank][Python3] Recursion: Fibonacci Numbers (0) | 2018.07.20 |
최근에 달린 댓글 최근에 달린 댓글