-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count Luck
63 lines (46 loc) · 1.4 KB
/
Count Luck
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
import math
import os
import random
import re
import sys
def countLuck(matrix, k):
d = {}
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j] in ('.', '*', 'M'):
d[(i, j)] = matrix[i][j]
if matrix[i][j] == 'M':
q = [(i, j, [0])]
v = [(i, j)]
while q:
i, j, p = q.pop(0)
if d[(i, j)] == '*':
if p[0] == k:
return 'Impressed'
else:
return 'Oops!'
n = len(v)
p = list(p)
for x, y in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
x += i
y += j
if (x, y) in d and (x, y) not in v:
v.append((x, y))
q.append((x, y, p)
if (len(v) - n) > 1:
p[0] += 1
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input().strip())
for t_itr in range(t):
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
m = int(first_multiple_input[1])
matrix = []
for _ in range(n):
matrix_item = input()
matrix.append(matrix_item)
k = int(input().strip())
result = countLuck(matrix, k)
fptr.write(result + '\n')
fptr.close()