" 在二维数组中寻找鞍点(Saddle Point)的算法可以分为以下几个步骤:
1. 初始化一个空字典`dp`,用于存储每个元素的上下界信息。
2. 遍历二维数组,对于每个元素`arr[i][j]`:
a. 如果`dp[i][j]`已经存在,跳过。
b. 计算当前元素的上下界信息:`lower[i][j]`为当前元素左上方和右上方的最大值,`upper[i][j]`为当前元素右下方和左下方的最小值。
c. 如果`lower[i][j] < arr[i][j] < upper[i][j]`,则`arr[i][j]`是一个鞍点,将坐标`(i, j)`加入结果列表`ans`中。
d. 更新`dp[i][j]`为当前元素的上下界信息。
3. 返回结果列表`ans`。
以下是Python实现:
```python
def find_saddle_points(arr):
rows, cols = len(arr), len(arr[0])
lower = [[0] * cols for _ in range(rows)]
upper = [[0] * cols for _ in range(rows)]
dp = [[0] * cols for _ in range(rows)]
ans = []
for i in range(rows):
for j in range(cols):
if dp[i][j] != 0:
continue
for a, b in pairwise((-1, 0, 1, 0, -1)):
x, y = i + a, j + b
if 0 <= x < rows and 0 <= y < cols:
lower[i][j] = max(lower[i][j], arr[x][y])
upper[i][j] = min(upper[i][j], arr[x][y])
if lower[i][j] < arr[i][j] < upper[i][j]:
ans.append((i, j))
dp[i][j] = lower[i][j], upper[i][j]
return ans
```
其中`pairwise`函数用于生成相邻元素的元组,可以根据需要自行实现。"