This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <queue>
#include <vector>
#include <tuple>
#include <set>
#include <functional>
using namespace std;
const int MAX_W = 10;
const int MAX_H = 10;
const int INF = 10000; // Large enough number to simulate "infinity"
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 simulate(vector<int> &moves, pair<int, int> robot1, pair<int, int> robot2) {
int move_count = 0;
for (int move : moves) {
pair<int, int> new_position;
if (move < 4) { // Moves for robot1
new_position = get_moves(robot1.first, robot1.second)[move];
robot1 = new_position;
} else { // Moves for robot2
new_position = get_moves(robot2.first, robot2.second)[move - 4];
robot2 = new_position;
}
move_count++;
if (robot1 == robot2) {
return move_count;
}
}
return INF;
}
int brute_force(pair<int, int> robot1, pair<int, int> robot2) {
int best = INF;
vector<int> current_moves;
int depth_limit = 10; // Reasonable depth limit based on grid constraints
// A recursive function to explore all possible move sequences up to depth_limit
function<void(int)> explore = [&](int depth) {
if (depth == depth_limit) {
int result = simulate(current_moves, robot1, robot2);
best = min(best, result);
return;
}
for (int i = 0; i < 8; i++) { // 4 directions for each robot
current_moves.push_back(i);
explore(depth + 1);
current_moves.pop_back();
}
};
explore(0);
return best == INF ? -1 : best;
}
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 << brute_force(robot1, robot2) << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |