ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
from collections import deque
def count_paths(g):
    R, C = len(g), len(g[0])
    dist = [[-1] * C for _ in range(R)]
    ways = [[0] * C for _ in range(R)]
    dist[0][0] = 0
    ways[0][0] = 1
    q = deque([(0, 0)])
    while q:
        r, c = q.popleft()
        for nr, nc in ((r + 1, c), (r, c + 1)):
            if nr < R and nc < C and g[nr][nc] == '.':
                if dist[nr][nc] == -1:
                    dist[nr][nc] = dist[r][c] + 1
                    q.append((nr, nc))
                if dist[nr][nc] == dist[r][c] + 1:
                    ways[nr][nc] += ways[r][c]
    return ways[R - 1][C - 1]
print(count_paths(['...', '.#.', '...']))
  1. A. 6
  2. B. 2
  3. C. 4
  4. D. 3

Correct answer: B. 2

Only right and down moves are allowed, and the centre is blocked. Of the six monotone routes, four pass through the centre, so two remain, and BFS counts them by layers.

More Python Programming questions

Exams that ask this topic