Python Algorithm Practice for Blue Bridge Cup Competition - Set 2
1. Minefield Crossing
Approach: BFS traversal using a queue.
import sys
n = int(input())
grid = [input().split() for _ in range(n)]
visited = [[0] * n for _ in range(n)]
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
queue = []
end_x, end_y = 0, 0
for i in range(n):
for j in range(n):
if grid[i][j] == 'A':
queue.append((i, j, '', 0))
visited[i][j] = 1
if grid[i][j] == 'B':
end_x, end_y = i, j
def bfs():
global end_x, end_y
while queue:
x, y, prev_char, steps = queue.pop(0)
for dx, dy in directions:
new_x, new_y = x + dx, y + dy
if new_x < 0 or new_x >= n or new_y < 0 or new_y >= n:
continue
if visited[new_x][new_y] == 1 or grid[new_x][new_y] == prev_char:
continue
visited[new_x][new_y] = 1
if new_x == end_x and new_y == end_y:
return steps + 1
queue.append((new_x, new_y, grid[new_x][new_y], steps + 1))
print(bfs())
2. Child Admiartion Circle
Approach: DFS traversal to detect cycles.
import sys
sys.setrecursionlimit(1000000)
n = int(input())
f = [0] + list(map(int, input().split()))
visited = [0] * (n + 1)
max_cycle_length = 0
def dfs(node, depth):
global max_cycle_length
if visited[node]:
max_cycle_length = max(max_cycle_length, depth - visited[node])
return
visited[node] = depth
dfs(f[node], depth + 1)
for i in range(1, n + 1):
if not visited[i]:
dfs(i, 1)
print(max_cycle_length)
3. Watch Adjustment
Approach: Dynamic programming to minimize key presses.
N, K = map(int, input().split())
dp = [i for i in range(N)]
for i in range(N):
val = (i * K) % N
dp[val] = min(i, dp[val])
for i in range(1, N):
dp[i] = min(dp[i - 1] + 1, dp[i - K] + 1, dp[i])
print(max(dp))
4. Soup Combination
Approach: Mathematical analysis using GCD and dynamic programming.
import math
N = 10000
dp = [0] * N
n = int(input())
a = [int(input()) for _ in range(n)]
g = a[0]
for i in range(n):
g = math.gcd(g, a[i])
if g == 1:
dp[0] = 1
for i in range(n):
for j in range(a[i], N):
dp[j] = max(dp[j], dp[j - a[i]])
print(N - sum(dp))
else:
print('INF')
5. Log Statistics
Approach: Dictionary-based grouping and interval counting.
N, D, K = map(int, input().split())
logs = {}
for _ in range(N):
timestamp, post_id = map(int, input().split())
logs.setdefault(post_id, []).append(timestamp)
hot_posts = []
for post_id, timestamps in logs.items():
timestamps.sort()
valid = False
for start_time in timestamps:
count = sum(1 for t in timestamps if start_time <= t < start_time + D)
if count >= K:
valid = True
break
if valid:
hot_posts.append(post_id)
for pid in sorted(hot_posts):
print(pid)
6. Gold Coins
Approach: Iterative calculation of coin distribution.
k = int(input())
i = 1
total = 0
while k >= i:
k -= i
total += i * i
i += 1
if k != 0:
total += k * i
print(total)
7. Magic Square
Approach: Fill magic square using specific moveemnt rules.
n = int(input())
magic_square = [[0] * n for _ in range(n)]
magic_square[0][n // 2] = 1
x, y = 0, n // 2
for i in range(2, n * n + 1):
if x == 0 and y != n - 1:
x, y = n - 1, y + 1
elif x != 0 and y == n - 1:
x, y = x - 1, 0
elif x == 0 and y == n - 1:
x, y = x + 1, y
else:
if magic_square[x - 1][y + 1] == 0:
x, y = x - 1, y + 1
else:
x, y = x + 1, y
magic_square[x][y] = i
for row in magic_square:
print(*row)