2020-08-28 依图科技算法笔试 A
这里出现了相对复杂的输入输出,特此记录
具体题目可上牛客搜索
输入:2 表示 2 组测试用例
5 8 中的 5 表示矩阵的边长,8 表示每组测试用例每一步的操作:
2
5 8
L
R
G 10
P
R
R
G 10
P
5 8
L
R
G 10
P
R
R
G 10
P
输出:
Case #1:
0 0
0 4
Case #2:
0 0
0 4
import sys
def robotWalk(pos, n, com):
if com[0] == 'L':
pos['rot'] -= 90
elif com[0] == 'R':
pos['rot'] += 90
elif com[0] == 'G':
num_walk = int(com[1])
if pos['rot'] % 360 == 0:
if pos['x'] + num_walk <= n - 1:
pos['x'] += num_walk
else:
pos['x'] = n - 1
elif pos['rot'] % 360 == 90:
if pos['y'] + num_walk <= n - 1:
pos['y'] += num_walk
else:
pos['y'] = n - 1
elif pos['rot'] % 360 == 180:
if pos['x'] - num_walk >= 0:
pos['x'] -= num_walk
else:
pos['x'] = 0
elif pos['rot'] % 360 == 270:
if pos['y'] - num_walk >= 0:
pos['y'] -= num_walk
else:
pos['y'] = 0
if __name__ == "__main__":
T = int(sys.stdin.readline().strip())
for t in range(T):
print("Case #%d:" % (t + 1))
# [x, y, rotation]
start_pos = {
'x': 0, 'y': 0, 'rot': -90}
n, m = list(map(int, input().split(" ")))
for i in range(m):
# 读取每一行
line = sys.stdin.readline().strip()
# 把每一行的数字分隔后转化成int列表
com = list(map(str, line.split()))
# print("com:", com)
robotWalk(start_pos, n, com)
if com[0] == 'P':
print(start_pos['x'], start_pos['y'], end='\n')