def get_adjustment(current_mod_rest, ins): if current_mod_rest == 0 and ins == "G": return -1 elif current_mod_rest == 0 and ins == "P": return 1 elif current_mod_rest == 1 and ins == "L": return -1 elif current_mod_rest == 1 and ins == "G": return 1 elif current_mod_rest == 2 and ins == "L": return 1 elif current_mod_rest == 2 and ins == "D": return -1 elif current_mod_rest == 3 and ins == "P": return -1 elif current_mod_rest == 3 and ins == "D": return 1 return 0 class Board: def __init__(self): n, m = [int(e) for e in input().split()] self.spiral_i = None self.M = [] for i in range(n): self.M.append(list(input())) _, self.instructions = input(), list(input()) self.mov_dict = { "G": [ [(i, j) for i in range(n)] for j in range(m) ], "D": [ [(i, j) for i in range(n - 1, -1, -1)] for j in range(m) ], "P": [ [(i, j) for j in range(m - 1, -1, -1)] for i in range(n) ], "L": [ [(i, j) for j in range(m)] for i in range(n) ] } def show(self): for line in self.M: print("".join(line)) def move_by(self, moves): for move_l in moves: pre_ind = 0 for ind in range(len(move_l)): i, j = move_l[ind] if self.M[i][j] == ".": continue if ind == pre_ind: pre_ind += 1 continue pre_i, pre_j = move_l[pre_ind] pre_ind += 1 self.M[pre_i][pre_j] = self.M[i][j] self.M[i][j] = "." def move(self, c): self.move_by(self.mov_dict[c]) @staticmethod def counter_directions(p1, p2): return ( (p1 in "LP" and p2 in "GD") or (p2 in "LP" and p1 in "GD") ) def early_simplify(self): for i in range(len(self.instructions) - 1): p1, p2 = self.instructions[i:i+2] if self.counter_directions(p1, p2): self.move(p1) self.move(p2) concat = f"{p1}{p2}" if concat in ["LD", "DL"]: self.spiral_i = 0 if concat in ["PD", "DP"]: self.spiral_i = 1 if concat in ["PG", "GP"]: self.spiral_i = 2 if concat in ["LG", "GL"]: self.spiral_i = 3 self.instructions = self.instructions[i+2:] return self.move(self.instructions[-1]) self.show() exit(0) def current_mod_rest(self, i): return (10 ** 6 + i) % 4 def calculate_end_spiral(self): self.end_spiral_i = self.spiral_i for ins in self.instructions: self.end_spiral_i += get_adjustment( self.current_mod_rest(self.end_spiral_i), ins ) def __str__(self): res = "" for line in self.M: res += "".join(line) return res def inc_spiral(self): for ins in "GDLP": if get_adjustment(self.current_mod_rest(self.spiral_i), ins) == 1: self.move(ins) self.spiral_i += 1 return assert(False) def dec_spiral(self): for ins in "GDLP": if get_adjustment(self.current_mod_rest(self.spiral_i), ins) == -1: self.move(ins) self.spiral_i -= 1 return assert(False) def get_cycle_length(self): pre_spiral_i = self.spiral_i begin_state = str(self) while True: self.inc_spiral() if str(self) == begin_state: res = self.spiral_i - pre_spiral_i self.spiral_i = pre_spiral_i return res def solve(self): for ins in self.instructions: b.move(ins) return b.early_simplify() b.calculate_end_spiral() # cycle_l = b.get_cycle_length() # while True: # prop1 = self.end_spiral_i - cycle_l # prop2 = self.end_spiral_i + cycle_l # if abs(self.end_spiral_i) <= min(abs(prop1), abs(prop2)): # break # self.end_spiral_i = prop1 if abs(prop1) < abs(self.end_spiral_i) else prop2 while self.spiral_i != self.end_spiral_i: # print(self.spiral_i, self.end_spiral_i) if self.spiral_i < self.end_spiral_i: self.inc_spiral() else: self.dec_spiral() b = Board() b.solve() b.show()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | def get_adjustment(current_mod_rest, ins): if current_mod_rest == 0 and ins == "G": return -1 elif current_mod_rest == 0 and ins == "P": return 1 elif current_mod_rest == 1 and ins == "L": return -1 elif current_mod_rest == 1 and ins == "G": return 1 elif current_mod_rest == 2 and ins == "L": return 1 elif current_mod_rest == 2 and ins == "D": return -1 elif current_mod_rest == 3 and ins == "P": return -1 elif current_mod_rest == 3 and ins == "D": return 1 return 0 class Board: def __init__(self): n, m = [int(e) for e in input().split()] self.spiral_i = None self.M = [] for i in range(n): self.M.append(list(input())) _, self.instructions = input(), list(input()) self.mov_dict = { "G": [ [(i, j) for i in range(n)] for j in range(m) ], "D": [ [(i, j) for i in range(n - 1, -1, -1)] for j in range(m) ], "P": [ [(i, j) for j in range(m - 1, -1, -1)] for i in range(n) ], "L": [ [(i, j) for j in range(m)] for i in range(n) ] } def show(self): for line in self.M: print("".join(line)) def move_by(self, moves): for move_l in moves: pre_ind = 0 for ind in range(len(move_l)): i, j = move_l[ind] if self.M[i][j] == ".": continue if ind == pre_ind: pre_ind += 1 continue pre_i, pre_j = move_l[pre_ind] pre_ind += 1 self.M[pre_i][pre_j] = self.M[i][j] self.M[i][j] = "." def move(self, c): self.move_by(self.mov_dict[c]) @staticmethod def counter_directions(p1, p2): return ( (p1 in "LP" and p2 in "GD") or (p2 in "LP" and p1 in "GD") ) def early_simplify(self): for i in range(len(self.instructions) - 1): p1, p2 = self.instructions[i:i+2] if self.counter_directions(p1, p2): self.move(p1) self.move(p2) concat = f"{p1}{p2}" if concat in ["LD", "DL"]: self.spiral_i = 0 if concat in ["PD", "DP"]: self.spiral_i = 1 if concat in ["PG", "GP"]: self.spiral_i = 2 if concat in ["LG", "GL"]: self.spiral_i = 3 self.instructions = self.instructions[i+2:] return self.move(self.instructions[-1]) self.show() exit(0) def current_mod_rest(self, i): return (10 ** 6 + i) % 4 def calculate_end_spiral(self): self.end_spiral_i = self.spiral_i for ins in self.instructions: self.end_spiral_i += get_adjustment( self.current_mod_rest(self.end_spiral_i), ins ) def __str__(self): res = "" for line in self.M: res += "".join(line) return res def inc_spiral(self): for ins in "GDLP": if get_adjustment(self.current_mod_rest(self.spiral_i), ins) == 1: self.move(ins) self.spiral_i += 1 return assert(False) def dec_spiral(self): for ins in "GDLP": if get_adjustment(self.current_mod_rest(self.spiral_i), ins) == -1: self.move(ins) self.spiral_i -= 1 return assert(False) def get_cycle_length(self): pre_spiral_i = self.spiral_i begin_state = str(self) while True: self.inc_spiral() if str(self) == begin_state: res = self.spiral_i - pre_spiral_i self.spiral_i = pre_spiral_i return res def solve(self): for ins in self.instructions: b.move(ins) return b.early_simplify() b.calculate_end_spiral() # cycle_l = b.get_cycle_length() # while True: # prop1 = self.end_spiral_i - cycle_l # prop2 = self.end_spiral_i + cycle_l # if abs(self.end_spiral_i) <= min(abs(prop1), abs(prop2)): # break # self.end_spiral_i = prop1 if abs(prop1) < abs(self.end_spiral_i) else prop2 while self.spiral_i != self.end_spiral_i: # print(self.spiral_i, self.end_spiral_i) if self.spiral_i < self.end_spiral_i: self.inc_spiral() else: self.dec_spiral() b = Board() b.solve() b.show() |