test/3L.py
2026-07-01 19:46:51 +03:00

70 lines
No EOL
1.4 KiB
Python

import sys
data = sys.stdin.read().split()
if not data:
exit()
n = int(data[0])
m = int(data[1])
ans = []
for i in range(n):
ans.extend(list(data[2 + i]))
q = [0] * (n * m)
head = 0
tail = 0
for c in range(m):
if ans[c] == '.':
ans[c] = '^'
q[tail] = c
tail += 1
for c in range(m):
pos = (n - 1) * m + c
if ans[pos] == '.':
ans[pos] = 'v'
q[tail] = pos
tail += 1
for r in range(n):
pos = r * m
if ans[pos] == '.':
ans[pos] = '<'
q[tail] = pos
tail += 1
for r in range(n):
pos = r * m + (m - 1)
if ans[pos] == '.':
ans[pos] = '>'
q[tail] = pos
tail += 1
while head < tail:
pos = q[head]
head += 1
r = pos // m
c = pos % m
if r > 0:
npos = pos - m
if ans[npos] == '.':
ans[npos] = 'v'
q[tail] = npos
tail += 1
if r < n - 1:
npos = pos + m
if ans[npos] == '.':
ans[npos] = '^'
q[tail] = npos
tail += 1
if c > 0:
npos = pos - 1
if ans[npos] == '.':
ans[npos] = '>'
q[tail] = npos
tail += 1
if c < m - 1:
npos = pos + 1
if ans[npos] == '.':
ans[npos] = '<'
q[tail] = npos
tail += 1
out = []
for r in range(n):
out.append("".join(ans[r * m : (r + 1) * m]))
print("\n".join(out))