题解 | #HJ17 坐标移动#

坐标移动

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

import re

def calculate_final_position(moves):
    # 初始坐标
    x, y = 0, 0
    # 方向映射
    direction_map = {'A': (-1, 0), 'D': (1, 0), 'W': (0, 1), 'S': (0, -1)}

    # 正则表达式匹配合法坐标
    pattern = re.compile(r'^([ADWS])(\d{1,2})$')

    for move in moves.split(';'):
        match = pattern.match(move)
        if match:
            direction, step = match.groups()
            dx, dy = direction_map[direction]
            x += dx * int(step)
            y += dy * int(step)

    return x, y

# 输入
input_string = input()
# 计算最终坐标
final_position = calculate_final_position(input_string)
# 输出结果
print(f"{final_position[0]},{final_position[1]}")

全部评论

相关推荐

点赞 评论 收藏
分享
06-26 22:20
门头沟学院 Java
码农索隆:让你把简历发给她,她说一些套话,然后让你加一个人,说这个人给你改简历,然后开始卖课
我的求职精神状态
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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