阿里巴巴笔试编程题题解
作者:尹斗俊喊你学习了啊
链接:https://www.nowcoder.com/discuss/80763
来源:牛客网
#实习##春招#
链接:https://www.nowcoder.com/discuss/80763
来源:牛客网
算法岗
花了二十分钟调试了摄像头,选择题全靠作为女生的第六感解决的2333
(1)求八个兵阵的士兵个数的最大最小值
思路:dfs+记忆化搜索
import sys if True: rows = int(raw_input()) cols = int(raw_input()) matrix = [[0 for j in range(cols)] for i in range(cols)] for i in range(rows): line = raw_input().split() for j in range(cols): matrix[i][j] = int(line[j]) disvisited = [[True for j in range(cols)] for i in range(rows)] min_ = sys.maxint max_ = -sys.maxint def dfs(x,y): dx = [0,0, 1,1, 1,-1,-1,-1] dy = [1,-1,0,-1,1,0 ,-1,1] tmp = matrix[x][y] disvisited[x][y] = False for k in range(8): nx = x+dx[k] ny = y+dy[k] if nx>=0 and nx<rows and ny>=0 and ny<cols and disvisited[nx][ny] and matrix[nx][ny]: tmp+=dfs(nx,ny) return tmp for i in range(rows): for j in range(cols): if disvisited[i][j] and matrix[i][j]: ans = dfs(i,j) min_ = min(min_,ans) max_ = max(max_,ans) print max_ print min_
(2)求最大的旅行路径数
思路:节点i到节点j存在路径的话,那么节点i的旅游路径数+=从节点j出发的路径数+1
ret = dict() def solved(i): if i in ret: return ret[i] ans = 0 for j in range(n): if a[i][j]: ans += 1+solved(j) ret[i] = ans return ans n = int(raw_input()) l = int(raw_input().split()[0]) a = [[0 for i in range(n)] for j in range(n)] for k in range(l): line = raw_input().split() start = int(line[0]) end = int(line[1]) a[start][end] = 1 for i in range(n): solved(i) print sorted(ret.items(),key = lambda x:x[1],reverse=True)[0][1]