-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathSurface.py
48 lines (37 loc) · 1.45 KB
/
Surface.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
# adopted from https://github.com/JohnPrime/CodinGame/blob/master/Surface/Surface.py
import sys
import math
l = int(input())
h = int(input())
def search(map, x, y):
visit_map = [[False] * l for i in range(h)]
list = [[x, y]]
surface = 0
while len(list) != 0:
cell = list[len(list) - 1]
list.pop()
if visit_map[cell[1]][cell[0]] == False:
visit_map[cell[1]][cell[0]] = True
if map[cell[1]][cell[0]] == 'O':
surface += 1
if cell[0] > 0:
if visit_map[cell[1]][cell[0] - 1] == False and map[cell[1]][cell[0] - 1] == 'O':
list.append([cell[0] - 1, cell[1]])
if cell[0] < l - 1:
if visit_map[cell[1]][cell[0] + 1] == False and map[cell[1]][cell[0] + 1] == 'O':
list.append([cell[0] + 1, cell[1]])
if cell[1] > 0:
if visit_map[cell[1] - 1][cell[0]] == False and map[cell[1] - 1][cell[0]] == 'O':
list.append([cell[0], cell[1] - 1])
if cell[1] < h - 1:
if visit_map[cell[1] + 1][cell[0]] == False and map[cell[1] + 1][cell[0]] == 'O':
list.append([cell[0], cell[1] + 1])
return surface
map = []
for i in range(h):
row = input()
map.append(row)
n = int(input())
for i in range(n):
x, y = [int(j) for j in input().split()]
print(search(map, x, y))