8.13美团笔试,大佬能帮忙看看哪里错了吗

题目是:机器人打扫房间。一个n*m的矩阵,给出一系列指令,可以向上下左右打扫。打扫完成输出Yes和用了几条指令,没完成输出No和还有几个单元没完成。
n, m, k = map(int, input().split())
order = list(input())
# 边界处理
if n==m==1:
    print('Yes')
    print(0)
if k==0:
    if m*n>1:
        print('No')
        print(n*m-1)

visited = [[0]*m for _ in range(n)]
visited[0][0] = 1
rest_pos = n*m-1
cur_pos = (0, 0)
for idx in range(len(order)):
    each_order = order[idx]
    if each_order=='W':
        new_pos = (cur_pos[0]-1, cur_pos[1])
    elif each_order=='A':
        new_pos = (cur_pos[0], cur_pos[1]-1)
    elif each_order=='S':
        new_pos = (cur_pos[0]+1, cur_pos[1])
    elif each_order=='D':
        new_pos = (cur_pos[0], cur_pos[1]+1)
    cur_pos = new_pos
    if visited[new_pos[0]][new_pos[1]] == 0:
        rest_pos -= 1
        visited[new_pos[0]][new_pos[1]] == 1
        if rest_pos==0:
            # 说明清理已经完成
            print('Yes')
            print(idx+1)
            break
if rest_pos>0:
    print('No')
    print(rest_pos)
        
非常感谢!
#美团笔试#
全部评论
visited[new_pos[0]][new_pos[1]] == 1 这里
点赞
送花
回复 分享
发布于 2022-08-13 23:24

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务