题解 | 坐标移动
坐标移动
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]}')
感觉我写的好麻烦啊,用了有一个小时才写出来,感觉代码很乱。
查看3道真题和解析