题解 | 坐标移动

import sys
import re

raw_input = []
for i,line in enumerate(sys.stdin):
    raw_input.append(line.strip())
    if i == 1:
        break

command_dict = {'A':(-1, 0), 'D':(1, 0), 'W':(0, 1), 'S':(0, -1)}
command_regex = re.compile(r'^([ADWS])(\d+)$')
command_lst = raw_input[0].split(';')
pos = [0, 0]
for command in command_lst:
    regex_match = re.match(command_regex, command)
    if regex_match is None:
        continue
    else:
        direction, dist = regex_match.group(1), int(regex_match.group(2))
        if dist > 99 or dist < 1:
            continue
        dx, dy = command_dict[direction]
        pos[0] += dx*dist
        pos[1] += dy*dist
        # print(pos)

print(f'{pos[0]},{pos[1]}')

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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