python3编译一直RE / 多次分块注释部分代码 对照提交测试 / 发现使用字典根据字符串定位key获取value这一步会导致编译不通过
代码在本地python3编译下没有异常 上传服务器会编译失败 自己摸不着头脑 将这个问题反馈一下
direction_dict: Dict[str, List] = {
'NORTH': [-1, 0],
'WEST': [0, -1],
'SOUTH': [1, 0],
'EAST': [0, 1]
}
for direction in direction_list:
direction_offset = direction_dict[direction]
from typing import Dict, List
import numpy as np
# input
R, C = map(int, input().split())
# init
road_map = np.zeros((R, C))
map_dict = {'.': 0, '*': 1, 'X': -1}
map_reverse_dict = {0: '.', 1: '*', -1: 'X'}
mark_car = map_dict['*']
mark_pass = map_dict['.']
mark_no_pass = map_dict['X']
direction_dict: Dict[str, List] = {
'NORTH': [-1, 0],
'WEST': [0, -1],
'SOUTH': [1, 0],
'EAST': [0, 1]
}
direction_list: List[str] = []
car_starting_index: List[int] = []
# input
for i in range(R):
s = input()
for j in range(C):
road_map[i, j] = map_dict[s[j]]
if road_map[i, j] == mark_car:
car_starting_index = [i, j]
N = int(input())
for i in range(N):
direction_list.append(input())
# process
# 创建停车记录类
class Car_Parking_Records:
records = None
queue_ = None
current_index = None
new_index = None
def __init__(self, m: np.ndarray):
self.records = np.array([m.copy(), m.copy()])
self.queue_ = [[], []]
self.current_index = 0
self.new_index = 1
def get_current(self):
return self.records[self.current_index]
def get_new(self):
return self.records[self.new_index]
def get_queue(self) -> list:
return self.queue_[self.current_index]
def flag_car(self, x, y):
self.records[self.new_index][x, y] = 1
self.queue_[self.new_index].append([x, y])
def remove_car(self, x, y):
self.records[self.current_index][x, y] = 0
def clear_queue(self):
self.queue_[self.current_index] = []
def switch(self):
self.current_index ^= 1
self.new_index = self.current_index ^ 1
# 初始化行车记录对象
records = Car_Parking_Records(road_map)
records.get_new()[car_starting_index[0], car_starting_index[1]] = mark_pass
records.get_queue().append([car_starting_index[0], car_starting_index[1]])
# 遍历当前分析记录 从汽车可能停车的位置 根据方向标记后续可能停车的位置
for direction in direction_list:
direction_offset = direction_dict[direction]
new_record = records.get_new()
queue_ = records.get_queue()
for car_index in queue_:
x = car_index[0]
y = car_index[1]
next_x = x + direction_offset[0]
next_y = y + direction_offset[1]
while 0 <= next_x < R and 0 <= next_y < C:
if (road_map[next_x, next_y] == mark_no_pass
or new_record[next_x, next_y] == mark_car):
break
records.flag_car(next_x, next_y)
next_x += direction_offset[0]
next_y += direction_offset[1]
records.remove_car(x, y)
records.clear_queue()
records.switch()
# 根据最后一次行车分析记录打印
current_record = records.get_current()
for i in range(R):
line = []
for j in range(C):
line.append(map_reverse_dict[current_record[i, j]])
print(''.join(line))