在一行上输入一个长度
,由大写字母、数字和分号(
)构成的字符串
,代表输入的指令序列。保证字符串中至少存在一个
,且末尾一定为
。
在一行上输出一个两个整数,代表小人最终位置的横纵坐标,使用逗号间隔。
A10;S20;W10;D30;X;A1A;B10A11;;A10;
10,-10
对于这个样例,我们模拟小人的移动过程:
第一个指令
是合法的,向左移动
个单位,到达
点;
第二个指令
是合法的,向下移动
个单位,到达
点;
第三个指令
是合法的,向上移动
个单位,到达
点;
第四个指令
是合法的,向右移动
个单位,到达
点;
第五个指令
不合法,跳过;
第六个指令
不合法,跳过;
第七个指令
不合法,跳过;
第八个指令
不合法,跳过;
第九个指令
是合法的,向左移动
个单位,到达
点。
ABC;AKL;DA1;D001;W023;A100;S00;
0,0
在这个样例中,全部指令均不合法,因此小人不移动。
A00;S01;W2;
0,1
本题已于下方时间节点更新,请注意题解时效性:
1. 2025-05-15 更新题面,新增几组hack数据(暂未进行重测)。
2. 2024-12-16 更新题面。
import sys cmds = input().split(';') def check(cmd: str): if len(cmd) <= 1: return False if not cmd.startswith(("A", "D", "W", "S")): return False if not cmd[1:].isdigit(): return False if not int(cmd[1:]) < 100: return False return True directions = { 'A': [-1, 0], 'D': [1, 0], 'W': [0, 1], 'S': [0, -1] } x, y = 0, 0 for cmd in cmds: # 检查指令的有效性 if check(cmd): dis = int(cmd[1:]) dx, dy = directions[cmd[0]] x += dx * dis y += dy * dis print(f'{x},{y}')
s = input().split(';') x = 0 y = 0 for i in range(len(s)): if len(s[i])<2&nbs***bsp;len(s[i])>3: continue if s[i][0] == 'A'&nbs***bsp;'S'&nbs***bsp;'D'&nbs***bsp;'W': t = s[i][1:] if t.isdigit(): num = int(t) else: continue if s[i][0]=='A': x = x - num elif s[i][0] == 'D': x = x + num elif s[i][0] == 'W': y = y + num elif s[i][0] == 'S': y = y - num else: continue else: continue print(f"{x},{y}")
str1 = str(input()) str1_list = [i for i in str1.split(";") if len(i) != 0] str1_list = [i for i in str1_list if i[0] in ["A", "D", "W", "S"]] str2_list = [] for i in str1_list: try: if int(i[1:]) >= 1 and int(i[1:]) <= 99: str2_list.append(i) except: '' a, b = 0, 0 for i in str2_list: fx, val_ = i[0], int(i[1:]) if fx == "A": a -= val_ if fx == "D": a += val_ if fx == "W": b += val_ if fx == "S": b -= val_ print('{},{}'.format(a,b))
a = input().split(";") lis = ["W","A","S","D"] jihe = [] my_dict = { "W" : 0, "S" : 0, "A" : 0, "D" : 0 } for i in lis : for k in range(1,100): b = "" b = str(i) + str(k) jihe.append(b) for j in a: if j in jihe: c = j[1:] d = j[0] my_dict[d] += int(c) n,m = 0,0 n += my_dict["D"] n -= my_dict["A"] m += my_dict["W"] m -= my_dict["S"] print(str(n)+","+str(m))
instruct = input().split(';') position = [0, 0] for ins in instruct: l = ins[0:1] r = ins[1:] try: steps = eval(r) except: continue if l == 'A': position[0] -= steps elif l == 'S': position[1] -= steps elif l == 'D': position[0] += steps elif l == 'W': position[1] += steps print(position[0],end=',') print(position[1])
cord_list = input().split(';') x, y = 0, 0 for cord in cord_list: if 2 <= len(cord) <= 3 and cord[1:].isdigit(): d, step = cord[0], int(cord[1:]) if d == 'A': x -= step elif d == 'D': x += step elif d == 'W': y += step elif d == 'S': y -= step print(f'{x},{y}')
act=input().split(';') start=[0,0] for i in act: if not 2<=len(i)<=3: continue try: move=i[0] step=int(i[1:]) match (move): case "W": start[1]+=step case "S": start[1]-=step case "A": start[0]-=step case "D": start[0]+=step except: continue print(str(start[0])+','+str(start[1]))
s = input().split(';') x,y = 0,0 for move in s: if len(move) >= 2 and (move[1:]).isdigit(): if move[0] == 'A' : x -= int(move[1:]) elif move[0] == 'D' : x += int(move[1:]) elif move[0] == 'W' : y += int(move[1:]) elif move[0] == 'S' : y -= int(move[1:]) print(f'{x},{y}')