Given a 2D grid skyMap composed of '1's (clouds) and '0's (clear sky), count the number of clouds. A cloud is surrounded by clear sky, and is formed by connecting adjacent clouds horizontally or vertically. You can assume that all four edges of the skyMap are surrounded by clear sky.
Example
For
skyMap = [['0', '1', '1', '0', '1'],
['0', '1', '1', '1', '1'],
['0', '0', '0', '0', '1'],
['1', '0', '0', '1', '1']]
the output should be countClouds(skyMap) = 2;
For
skyMap = [['0', '1', '0', '0', '1'],
['1', '1', '0', '0', '0'],
['0', '0', '1', '0', '1'],
['0', '0', '1', '1', '0'],
['1', '0', '1', '1', '0']]
the output should be countClouds(skyMap) = 5.
Input/Output
[time limit] 4000ms (py3)
[input] array.array.char skyMap
A 2D grid that represents a map of the sky, as described above.
Guaranteed constraints:
0 ≤ skyMap.length ≤ 300,
0 ≤ skyMap[i].length ≤ 300.
[output] integer
The number of clouds in the given skyMap, as described above.
In [3]:
skyMap = [['0', '1', '1', '0', '1'],
['0', '1', '1', '1', '1'],
['0', '0', '0', '0', '1'],
['1', '0', '0', '1', '1']]
Out[3]:
In [12]:
def get_left(skyMap, i, j):
i = i - 1
if i < 0:
return False
return skyMap[j][i]
def get_right(skyMap, i, j):
i = i + 1
if i > len(skyMap[0]) - 1:
return False
return skyMap[j][i]
def get_up(skyMap, i, j):
j = j - 1
if j < 0:
return False
return skyMap[j][i]
def get_down(skyMap, i, j):
j = j + 1
if j > len(skyMap)-1:
return False
return skyMap[j][i]
def countClouds(skyMap):
pass
# print(get_up(skyMap, 0, 2))
In [ ]: