Submission #968227

#TimeUsernameProblemLanguageResultExecution timeMemory
968227ShauryaTheShepRobots (APIO13_robots)C++14
0 / 100
1 ms348 KiB
#include <iostream> #include <queue> #include <vector> #include <tuple> #include <set> using namespace std; const int MAX_W = 10; const int MAX_H = 10; char grid[MAX_H][MAX_W]; int w, h; bool is_inside(int x, int y) { return x >= 0 && x < h && y >= 0 && y < w; } vector<pair<int, int>> get_moves(int x, int y) { vector<pair<int, int>> positions; // Directions: Up, Down, Left, Right int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; for (int d = 0; d < 4; ++d) { int nx = x, ny = y; while (is_inside(nx + dx[d], ny + dy[d]) && grid[nx + dx[d]][ny + dy[d]] != 'x') { nx += dx[d]; ny += dy[d]; } positions.push_back({nx, ny}); } return positions; } int bfs(pair<int, int> robot1, pair<int, int> robot2) { queue<tuple<pair<int, int>, pair<int, int>, int>> q; set<pair<pair<int, int>, pair<int, int>>> visited; q.push(make_tuple(robot1, robot2, 0)); visited.insert(make_pair(robot1, robot2)); while (!q.empty()) { auto current = q.front(); q.pop(); auto r1 = get<0>(current); auto r2 = get<1>(current); int moves = get<2>(current); if (r1 == r2) return moves; auto moves_r1 = get_moves(r1.first, r1.second); auto moves_r2 = get_moves(r2.first, r2.second); for (auto& nr1 : moves_r1) { if (visited.insert(make_pair(nr1, r2)).second) { q.push(make_tuple(nr1, r2, moves + 1)); } } for (auto& nr2 : moves_r2) { if (visited.insert(make_pair(r1, nr2)).second) { q.push(make_tuple(r1, nr2, moves + 1)); } } } return -1; // If no solution is possible } int main() { cin >> w >> h; pair<int, int> robot1, robot2; bool first_robot_found = false; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { cin >> grid[i][j]; if (isdigit(grid[i][j])) { if (!first_robot_found) { robot1 = {i, j}; first_robot_found = true; } else { robot2 = {i, j}; } } } } cout << bfs(robot1, robot2) << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...