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

相关推荐

04-30 21:35
已编辑
长安大学 C++
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务