华为笔试
第一题 45
第二题 200
第三题 60
话说第一题优化想了好久就是TLE
------第一题单调栈能a
------更新下第二题中庸行者
# If you need to import additional packages or classes, please import here.
def func():
m, n = map(int, input().split())
mat = []
for _ in range(m):
l = list(map(int, input().split()))
mat.append(l)
path = []
def dfs(i, j, k):
if not 0 <= i < m or not 0 <= j < n or k == m * n:
return 0
if not mat[i][j]: return 0
if k >= 2:
if path[:][-1] > mat[i][j] and path[:][-2] > path[:][-1]:
return 0
if path[:][-1] < mat[i][j] and path[:][-2] < path[:][-1]:
return 0
if path[:][-1] == mat[i][j]:
return 0
path.append(mat[i][j])
tmp = mat[i][j]
mat[i][j] = ''
ans = max(dfs(i - 1, j, k + 1), dfs(i + 1, j, k + 1), dfs(i, j - 1, k + 1), dfs(i, j + 1, k + 1)) + 1
mat[i][j] = tmp
path.pop()
return ans
res = 0
for i in range(m):
for j in range(n):
ans = dfs(i, j, 0)
if ans > res:
res = ans - 1 # 移动次数=path长度-1
print(res)
if __name__ == "__main__":
func()
#华为求职进展汇总#
