2024 J5 - Harvest Waterloo

Normalgraph theorysimple math

1 Solution Available

Solution 1

PYTHON
1# By Daniel Zhang, Pinetree Secondary
2
3from collections import deque
4
5r = int(input())
6c = int(input())
7
8
9def valid(node):
10    x, y = node
11    if 0 <= x < r:
12        if 0 <= y < c:
13            if not vis[x][y]:
14                if patch[x][y] != "*":
15                    return True
16    return False
17
18def neighbours(node):
19    x, y = node
20    return [[x+1, y], [x-1, y], [x, y+1], [x, y-1]]
21
22patch = []
23vis = []
24
25for i in range(r):
26    patch.append(list(input()))
27    vis.append([False] * c)
28
29a = int(input())
30b = int(input())
31
32queue = deque([[a, b]])
33score = 0
34vis[a][b] = True
35cur = patch[a][b]
36if cur == "S":
37    score += 1
38if cur == "M":
39    score += 5
40if cur == "L":
41    score += 10
42    
43while queue:
44    node = queue.popleft()
45    
46    for n in neighbours(node):
47        if valid(n):
48            a, b = n[0], n[1]
49            vis[a][b] = True
50            cur = patch[a][b]
51            if cur == "S":
52                score += 1
53            if cur == "M":
54                score += 5
55            if cur == "L":
56                score += 10
57
58            queue.append(n)
59
60print(score)

Test Cases

Select a test case to view input and output