Перепиши данный код на Python3 3.5.2 #include <bits/stdc++.h> using namespace std;…
- User
Перепиши данный код на Python3 3.5.2 #include <bits/stdc++.h> using namespace std;
vector<pair<int, int>> buildPath(int n, int m) { vector<pair<int, int>> result; result.reserve(n * m); int x = 0; int y = 0; do { result.push_back({x, y}); if (y == 0) { if (x == 0) { ++y; } else { --x; } } else if (x < n - 2 || n % 2 == 0) { if (x % 2 == 0) { if (y == m - 1) { ++x; } else { ++y; } } else { if (y == 1) { if (x == n - 1) { --y; } else { ++x; } } else { --y; } } } else { int color = ((n + m) % 2) ^ ((n * m) % 2); int curColor = (x + y) % 2; if (curColor == color) { --y; } else if (x == n - 1) { --x; } else { ++x; } } } while (x != 0 || y != 0); return result; }
bool occupied(int r, int c, int ar, int ac, int br, int bc) { return (r == ar && c == ac) || (r == br && c == bc); }
void flipHorizontal(vector<pair<int, int>> &path, int sz) { for (auto &it : path) { it.second = sz - 1 - it.second; } }
void flipVertical(vector<pair<int, int>> &path, int sz) { for (auto &it : path) { it.first = sz - 1 - it.first; } }
void solve(int n, int m, int ar, int ac, int br, int bc, const vector<vector> &w) { vector<pair<int, int>> path = buildPath(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)) { flipHorizontal(path, m); } else { flipVertical(path, n); } } } long long totalW = 0; for (int i = 0; i < (int) w.size(); ++i) { for (auto it : w[i]) { totalW += it; } } int posStart = find(path.begin(), path.end(), make_pair(ar, ac)) - path.begin(); int posEnd = find(path.begin(), path.end(), make_pair(br, bc)) - path.begin(); long long cost = w[path[posEnd].first][path[posEnd].second]; for (int i = posStart; i != posEnd; i = (i + 1) % path.size()) { cost += w[path[i].first][path[i].second]; } int dx = 2 * cost > totalW ? 1 : -1; for (int i = posStart; i != posEnd; i = (i + dx + path.size()) % path.size()) { int j = (i + dx + path.size()) % path.size(); int dirx = path[j].first - path[i].first; int diry = path[j].second - path[i].second; if (dirx == 1) cout << 'D'; if (dirx == -1) cout << 'U'; if (diry == 1) cout << 'R'; if (diry == -1) cout << 'L'; } cout << endl; }
int main() { ios_base::sync_with_stdio(false); cin.tie(0); int tests; cin >> tests; for (int test = 1; test <= tests; ++test) { int n, m, ar, ac, br, bc; cin >> n >> m >> ar >> ac >> br >> bc; vector<vector> w(n, vector(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> w[i][j]; } } solve(n, m, ar - 1, ac - 1, br - 1, bc - 1, w); } return 0; }
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.