题解 | 坐标移动

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

from collections import defaultdict
import sys

for line in sys.stdin:
    a = line.split()[0]
    #print((a))
    num=0
    for i in a:
        if(';'==i):
            num+=1
    ind=0
    x=[0,0]
    while(num):
        for j in range(len(a)):
            if a[j]==';':
                num-=1
                if(ind<j):
                    b=a[ind:j+1]
                    #print('b:',b)
                    judge=0
                    dic=defaultdict(int)
                    for s in  b :
                        if s in ['A','D','W','S']:
                            dic[s]+=1
                            judge+=1
                            #print('state ',s>='A',s <='Z',s not in ['A','D','W','S'],s,s>='A' and s <='Z' and s not in ['A','D','W','S'])
                        if s>='A' and s <='Z' and s not in ['A','D','W','S']:
                            judge=0
                            #print('s',s)
                            break
                        
                        #print('judge',judge,b,s)
                    if judge==1:
                        if b[0]=='A':
                            dx=[-1,0]
                        elif b[0]=='D':
                            dx=[1,0]
                        elif b[0]=='W':
                            dx=[0,1]
                        elif b[0]=='S':
                            dx=[0,-1]
                        
                        dist=int(b[1:(len(b)-1)])
                        #print(dist)
                        if dist>0 and dist<100:
                            x[0]+=dx[0]*dist
                            x[1]+=dx[1]*dist
                            #print(x)
                    #print(b)
                ind=j+1
    print(f'{x[0]},{x[1]}')
            

感觉我写的好麻烦啊,用了有一个小时才写出来,感觉代码很乱。

全部评论

相关推荐

程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
01-15 22:54
武汉大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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