-
Notifications
You must be signed in to change notification settings - Fork 79
/
54.py
101 lines (80 loc) · 2.4 KB
/
54.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# coding:utf-8
'''
最大陆地问题:
给定一个二维数组,数组的值非0即1,0代表海洋,1代表陆地,求该数组代表的区域陆地的最大面积。
'''
'''
深度优先搜索
'''
def maxArea(array):
sizeX = len(array)
sizeY = len(array[0])
sum = 0
for x in range(sizeX):
for y in range(sizeY):
if array[x][y] == 1:
tempSum = dfs(array, x, y)
if tempSum > sum:
sum = tempSum
return sum
def dfs(array, x, y):
sizeX = len(array)
sizeY = len(array[0])
if x < 0 or x >= sizeX or y < 0 or y >= sizeY or array[x][y] != 1:
return 0
sum = 1
array[x][y] = 0
sum += dfs(array, x + 1, y)
sum += dfs(array, x - 1, y)
sum += dfs(array, x, y - 1)
sum += dfs(array, x, y + 1)
return sum
'''
广度优先搜索:
如果发现陆地,就将其标记,然后广度搜索标记并计数与其相邻的点
'''
def bfs(array, x, y, tempSum, sizeX, sizeY):
if x < 0 or y < 0 or x >= sizeX or y >= sizeY:
return 0
array[x][y] = 0
tempSum += 1
queue = []
queue.append([x, y])
while len(queue) != 0:
tempX, tempY = queue[0][0], queue[0][1]
del queue[0]
if tempX + 1 < sizeX and array[tempX + 1][tempY] == 1:
array[tempX + 1][tempY] = 0
tempSum += 1
queue.append([tempX + 1, tempY])
if tempX - 1 >= 0 and array[tempX - 1][tempY] == 1:
array[tempX - 1][tempY] = 0
tempSum += 1
queue.append([tempX - 1, tempY])
if tempY - 1 >= 0 and array[tempX][tempY - 1] == 1:
array[tempX][tempY - 1] = 0
tempSum += 1
queue.append([tempX, tempY - 1])
if tempY + 1 < sizeY and array[tempX][tempY + 1] == 1:
array[tempX][tempY + 1] = 0
tempSum += 1
queue.append([tempX, tempY + 1])
return tempSum
def maxAreaBfs(array):
sizeX = len(array)
sizeY = len(array[0])
sum = 0
for x in range(sizeX):
for y in range(sizeY):
if array[x][y] == 1:
tempSum = bfs(array, x, y, 0, sizeX, sizeY)
if tempSum > sum:
sum = tempSum
return sum
a = [[1, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 0],
[1, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 0, 0],
[1, 0, 1, 0, 0, 0]]
print maxArea(a)