网易互娱第二批笔试 20200905(AI研究工程师)
个人感觉网易互娱笔试不是那种算法上的难,主要是考察编程能力,解决具体问题。
1,自动售货机(100%)
一开始写错了一个地方,就是应该在取出货到时候立刻记录当前所取货物的价格。写错不要紧,关键是这个错误在给的例子上还能通过。。。后来只能肉眼debug。这里有点浪费时间了。
这题主要就是用字典记录左右手状态,用栈模拟售货机。
while True:
try:
n,m = map(int,input().split())
val = list(map(int,input().split()))
stk = [[val[i]] for i in range(n)]
for i in range(m):#PEOPLE
ki=int(input())
res = 0
dic = {'left':0,'right':0}#money
for t in range(ki):#OP
op = list(input().split())#
op0,op1 = op[0],op[1]
if len(op) == 2:
res+= dic[op0]
dic[op0] = 0
continue
op2 = int(op[2])
if op1=='take':
dic[op0] = stk[ op2-1 ][-1]
if len(stk[ op2-1 ])>1:
stk[ op2-1 ].pop()
continue
if op1=='return':
stk[ op2-1 ].append(dic[op0])
dic[op0] = 0
continue
if dic['left']!=0:
res+=dic['left']
if dic['right']!=0:
res+=dic['right']
print(res)
except:
break 2,动画绘制(0%) 这题我看完决定放最后写,最后写完测试用例可以通过,但是0%,主要是我写的代码没有考虑i,j,a,b复数的情况,也没时间写了。。菜啊。。。
代码还是放一下,可以不看,主要是以基底像素为基础,暴力循环判断需不需要修改。
while True: try: t = int(input()) for _ in range(t): w,h = map(int,input().split()) wh = [] for i in range(h): wh.append(list(input())) p,q = map(int,input().split()) pq = [] for i in range(q): pq.append(list(input())) i,j,a,b = map(int,input().split()) wh0 = [['.']*(w+1)] mat = [['.']*(w+1)] for iii in range(h): mat.append(['.']+wh[iii]) wh0.append(['.']+wh[iii]) res = 0 while i<=h and j<=w: for ii in range(1,h+1): for jj in range(1,w+1): if i<=ii<i+q and j<=jj<j+p:# if mat[ii][jj] == pq[ii-i][jj-j]: continue mat[ii][jj] = pq[ii-i][jj-j] res+=1 elif mat[ii][jj] != wh0[ii][jj]: mat[ii][jj] = wh0[ii][jj] res+=1 i+=a j+=b print(res) except: break3,迷宫探秘(90%)
还记得之前写过某场的笔试,有一道题也是迷宫,感觉很显然也是bfs,但就是ac不了,不知道为啥哦。。我没试dfs因为感觉求最短路的话,dfs需要全部遍历完,感觉复杂度可能更高。
这里主要用字典记录可达点,做bfs。
step = {0:(1,0),1:(-1,0),2:(0,-1),3:(0,1)}
while True:
try:
t = int(input())
for _ in range(t):#ex
n = int(input())
x,y = 0,0
dic = {}
for st in range(n):#save
dx,f = map(int,input().split())
if f==1:
x+=step[dx][0]
y+=step[dx][1]
dic[(x,y)]=1
#bfs
res = 0
if x==0 and y==0:
print(0)
continue
queue = [(0,0)]
dic[(0,0)]=0
flag = 0
while queue:
lens = len(queue)
res+=1
for _ in range(lens):
a,b = queue.pop(0)
for aa,bb in {(1,0),(0,1),(-1,0),(0,-1)}:
l,r = a+aa,b+bb
if l==x and r==y:
flag = 1
break
elif ((l,r) in dic) and dic[(l,r)] == 1:
queue.append((l,r))
dic[(x,y)] = 0
if flag ==1:
break
if flag ==1:
break
print(res)
except:
break
作为小白记录一下笔试,代码写的不专业,轻轻喷。