from queue import Queue
H,W = map(int, input().split())
grid = []
for i in range(H):
inp = input()
row = [char for char in inp]
grid.append(row)
def adjs(loc):
x,y = loc[0], loc[1]
possibleAdjs = [(x+1,y), (x-1,y), (x,y+1), (x,y-1)]
return [adj for adj in possibleAdjs if validAdj(adj)]
def validAdj(adj):
x,y = adj[0],adj[1]
return x < len(grid) and y < len(grid[0]) and x > -1 and y > -1
def BFS(animal):
newQueue = Queue()
while not Q.empty():
nextLoc = Q.get()
neighbors = adjs(nextLoc)
for neighbor in neighbors:
if neighbor not in visited and grid[neighbor[0]][neighbor[1]] != '.':
if grid[neighbor[0]][neighbor[1]] != animal:
newQueue.put(neighbor)
else:
Q.put(neighbor)
visited.add(neighbor)
return newQueue
start = (0,0)
visited = set()
visited.add(start)
Q = Queue()
Q.put(start)
currAnimal = grid[0][0]
numAnimals = 0
while( not Q.empty()):
Q = BFS(currAnimal)
numAnimals += 1
if currAnimal == 'F':
currAnimal = 'R'
else:
currAnimal = 'F'
print(numAnimals)
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2094 ms |
21600 KB |
Time limit exceeded |
2 |
Correct |
29 ms |
3712 KB |
Output is correct |
3 |
Correct |
42 ms |
3840 KB |
Output is correct |
4 |
Execution timed out |
2083 ms |
20300 KB |
Time limit exceeded |
5 |
Correct |
317 ms |
8296 KB |
Output is correct |
6 |
Correct |
29 ms |
3692 KB |
Output is correct |
7 |
Correct |
42 ms |
3820 KB |
Output is correct |
8 |
Correct |
87 ms |
4076 KB |
Output is correct |
9 |
Correct |
46 ms |
3948 KB |
Output is correct |
10 |
Correct |
439 ms |
8040 KB |
Output is correct |
11 |
Correct |
559 ms |
8776 KB |
Output is correct |
12 |
Correct |
1187 ms |
15888 KB |
Output is correct |
13 |
Correct |
309 ms |
8168 KB |
Output is correct |
14 |
Correct |
312 ms |
8168 KB |
Output is correct |
15 |
Execution timed out |
2087 ms |
21848 KB |
Time limit exceeded |
16 |
Execution timed out |
2082 ms |
21592 KB |
Time limit exceeded |
17 |
Correct |
1583 ms |
18436 KB |
Output is correct |
18 |
Execution timed out |
2088 ms |
20252 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
314 ms |
6220 KB |
Output is correct |
2 |
Execution timed out |
2095 ms |
34416 KB |
Time limit exceeded |
3 |
Execution timed out |
2085 ms |
142392 KB |
Time limit exceeded |
4 |
Execution timed out |
2091 ms |
50048 KB |
Time limit exceeded |
5 |
Execution timed out |
2097 ms |
88008 KB |
Time limit exceeded |
6 |
Execution timed out |
2098 ms |
146016 KB |
Time limit exceeded |
7 |
Correct |
285 ms |
6220 KB |
Output is correct |
8 |
Correct |
313 ms |
6220 KB |
Output is correct |
9 |
Correct |
421 ms |
8804 KB |
Output is correct |
10 |
Correct |
191 ms |
5352 KB |
Output is correct |
11 |
Correct |
188 ms |
5856 KB |
Output is correct |
12 |
Correct |
350 ms |
5096 KB |
Output is correct |
13 |
Execution timed out |
2087 ms |
34596 KB |
Time limit exceeded |
14 |
Execution timed out |
2090 ms |
28816 KB |
Time limit exceeded |
15 |
Execution timed out |
2096 ms |
25444 KB |
Time limit exceeded |
16 |
Execution timed out |
2088 ms |
26464 KB |
Time limit exceeded |
17 |
Execution timed out |
2098 ms |
51808 KB |
Time limit exceeded |
18 |
Execution timed out |
2100 ms |
48868 KB |
Time limit exceeded |
19 |
Execution timed out |
2094 ms |
49888 KB |
Time limit exceeded |
20 |
Execution timed out |
2097 ms |
46552 KB |
Time limit exceeded |
21 |
Execution timed out |
2097 ms |
96376 KB |
Time limit exceeded |
22 |
Execution timed out |
2088 ms |
87748 KB |
Time limit exceeded |
23 |
Execution timed out |
2088 ms |
82040 KB |
Time limit exceeded |
24 |
Execution timed out |
2088 ms |
92400 KB |
Time limit exceeded |
25 |
Execution timed out |
2078 ms |
140852 KB |
Time limit exceeded |
26 |
Execution timed out |
2096 ms |
116168 KB |
Time limit exceeded |
27 |
Execution timed out |
2098 ms |
145124 KB |
Time limit exceeded |
28 |
Execution timed out |
2091 ms |
145888 KB |
Time limit exceeded |
29 |
Execution timed out |
2096 ms |
145828 KB |
Time limit exceeded |
30 |
Execution timed out |
2093 ms |
142836 KB |
Time limit exceeded |
31 |
Execution timed out |
2094 ms |
101580 KB |
Time limit exceeded |
32 |
Execution timed out |
2097 ms |
147200 KB |
Time limit exceeded |