forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [Add] Create count_islands.py Add count_islands problem in bfs section. * [Edit] Add description in count_islands problem Add description in count_islands problem in bfs section. * [Edit] Add examples in count_islands problem Add two examples in count_islands problem in bfs section. * [Edit] Add counting_islands method Add counting_islands method in count_islands.py in bfs section. * [Edit] Modify count_islands problem in bfs Add needed variables. * [Edit] Implement counting_islands method Complete counting_islands method. * [Edit] Add examples Add test cases and testing code. * [Edit] Delete testing code * [Edit] Change method's name Change counting_islands to count_islands in count_islands problem. * [Edit] Add unittest Implement unittest func. of count_islands problem in bfs section. * [Edit] Edit count_islands method While func. is shortened. * [Delete] Delete unittest of count_islands problem This reverts commit 8cd0a66. * [Edit] Add unittest Implement unittest func. of count_islands problem and edit init file.
- Loading branch information
Showing
3 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .count_islands import * | ||
from .maze_search import * | ||
from .shortest_distance_from_all_buildings import * | ||
from .word_ladder import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
This is a bfs-version of counting-islands problem in dfs section. | ||
Given a 2d grid map of '1's (land) and '0's (water), | ||
count the number of islands. | ||
An island is surrounded by water and is formed by | ||
connecting adjacent lands horizontally or vertically. | ||
You may assume all four edges of the grid are all surrounded by water. | ||
Example 1: | ||
11110 | ||
11010 | ||
11000 | ||
00000 | ||
Answer: 1 | ||
Example 2: | ||
11000 | ||
11000 | ||
00100 | ||
00011 | ||
Answer: 3 | ||
Example 3: | ||
111000 | ||
110000 | ||
100001 | ||
001101 | ||
001100 | ||
Answer: 3 | ||
Example 4: | ||
110011 | ||
001100 | ||
000001 | ||
111100 | ||
Answer: 5 | ||
""" | ||
|
||
|
||
def count_islands(grid): | ||
row = len(grid) | ||
col = len(grid[0]) | ||
|
||
num_islands = 0 | ||
visited = [[0] * col for i in range(row)] | ||
directions = [[-1, 0], [1, 0], [0, -1], [0, 1]] | ||
queue = [] | ||
|
||
for i in range(row): | ||
for j, num in enumerate(grid[i]): | ||
if num == 1 and visited[i][j] != 1: | ||
visited[i][j] = 1 | ||
queue.append((i, j)) | ||
while queue: | ||
x, y = queue.pop(0) | ||
for k in range(len(directions)): | ||
nx_x = x + directions[k][0] | ||
nx_y = y + directions[k][1] | ||
if nx_x >= 0 and nx_y >= 0 and nx_x < row and nx_y < col: | ||
if visited[nx_x][nx_y] != 1 and grid[nx_x][nx_y] == 1: | ||
queue.append((nx_x, nx_y)) | ||
visited[nx_x][nx_y] = 1 | ||
num_islands += 1 | ||
|
||
return num_islands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters