Перепиши данный код на C++ 17 def build_path(n, m): result = [] x, y = 0, 0 while True:…
- User
Перепиши данный код на C++ 17
def build_path(n, m): result = [] x, y = 0, 0 while True: result.append((x, y)) if y == 0: if x == 0: y += 1 else: x -= 1 elif x < n - 2 or n % 2 == 0: if x % 2 == 0: if y == m - 1: x += 1 else: y += 1 else: if y == 1: if x == n - 1: y -= 1 else: x += 1 else: y -= 1 else: color = ((n + m) % 2) ^ ((n * m) % 2) cur_color = (x + y) % 2 if cur_color == color: y -= 1 elif x == n - 1: x -= 1 else: x += 1 if x == 0 and y == 0: break return result
def occupied(r, c, ar, ac, br, bc): return (r == ar and c == ac) or (r == br and c == bc)
def flip_horizontal(path, sz): for i in range(len(path)): path[i] = (path[i][0], sz - 1 - path[i][1])
def flip_vertical(path, sz): for i in range(len(path)): path[i] = (sz - 1 - path[i][0], path[i][1])
def solve(n, m, ar, ac, br, bc, w): path = build_path(n, m) if n * m % 2 == 1: if occupied(n - 1, m - 1, ar, ac, br, bc): if occupied(0, m - 1, ar, ac, br, bc): flip_horizontal(path, m) else: flip_vertical(path, n) total_w = sum(sum(row) for row in w) pos_start = path.index((ar, ac)) pos_end = path.index((br, bc)) cost = w[path[pos_end][0]][path[pos_end][1]] i = pos_start while i != pos_end: cost += w[path[i][0]][path[i][1]] i = (i + 1) % len(path) dx = 1 if 2 * cost > total_w else -1 i = pos_start while i != pos_end: j = (i + dx + len(path)) % len(path) dirx = path[j][0] - path[i][0] diry = path[j][1] - path[i][1] if dirx == 1: print('D', end='') if dirx == -1: print('U', end='') if diry == 1: print('R', end='') if diry == -1: print('L', end='') i = j print()
def main(): tests = int(input()) for _ in range(tests): n, m, ar, ac, br, bc = map(int, input().split()) w = [list(map(int, input().split())) for _ in range(n)] solve(n, m, ar - 1, ac - 1, br - 1, bc - 1, w)
if name == 'main': main()
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.