"""
https://www.lanqiao.cn/problems/156/learning/?page=1&first_category_id=1&name=%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5
"""
n, m = map(int, input().split())
r, c = map(int, input().split())
# a[i][j]存储螺旋矩阵每个点的值, 如果a[i][j]=0表示没有没填充
a = [[0] * m for i in range(n)]
x, y = 0, 0
value = 1
a[x][y] = value
while value < n * m:
# 向右走
while y + 1 < m and not a[x][y + 1]:
value += 1
y += 1
a[x][y] = value
# 向下走
while x + 1 < n and not a[x + 1][y]:
value += 1
x += 1
a[x][y] = value
# 向左走
while y - 1 >= 0 and not a[x][y - 1]:
value += 1
y -= 1
a[x][y] = value
# 向上走
while x - 1 >= 0 and not a[x - 1][y]:
value += 1
x -= 1
a[x][y] = value
print(a[r - 1][c - 1])
蓝桥杯-螺旋矩阵
最新推荐文章于 2024-11-10 21:30:42 发布