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(['...', '.#.', '...']))- A. 6
- B. 2
- C. 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
- What is printed? python def cnt(a, k): pre = {0: 1} s = ans = 0 for x in a: s += x ans += pre.get(s - k, 0) pre[s] = pre.get(s, 0) + 1…
- What is printed? python def twotx(p): b1 = b2 = float('-inf') s1 = s2 = 0 for x in p: b1 = max(b1, -x) s1 = max(s1, b1 + x) b2 = max(b2…
- What is printed? python def tile(n): dp = [0] (n + 1) dp[0] = 1 for i in range(1, n + 1): dp[i] = dp[i - 1] + (dp[i - 2] if i >= 2 else 0)…
- A program sorts the whole list again for each of 10^5 queries on a list of 10^5 items. Which statement about running it in CPython is…
- What is printed? python from collections import Counter def mw(s, t): need = Counter(t) have = {} formed = 0 req = len(need) l = 0 best =…
- What is printed? python import bisect board = [] for name, score in [('a', 5), ('b', 9), ('c', 5)]: bisect.insort(board, (-score, name))…