#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];
bool visited[MAX_H][MAX_W][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<int, int, int, int, int>> q;
q.push({robot1.first, robot1.second, robot2.first, robot2.second, 0});
visited[robot1.first][robot1.second][robot2.first][robot2.second] = true;
while (!q.empty()) {
auto [r1x, r1y, r2x, r2y, moves] = q.front(); q.pop();
if (r1x == r2x && r1y == r2y) return moves;
vector<pair<int, int>> moves_r1 = get_moves(r1x, r1y);
vector<pair<int, int>> moves_r2 = get_moves(r2x, r2y);
for (auto& [new_r1x, new_r1y] : moves_r1) {
if (!visited[new_r1x][new_r1y][r2x][r2y]) {
visited[new_r1x][new_r1y][r2x][r2y] = true;
q.push({new_r1x, new_r1y, r2x, r2y, moves + 1});
}
}
for (auto& [new_r2x, new_r2y] : moves_r2) {
if (!visited[r1x][r1y][new_r2x][new_r2y]) {
visited[r1x][r1y][new_r2x][new_r2y] = true;
q.push({r1x, r1y, new_r2x, new_r2y, moves + 1});
}
}
}
return -1; // if no solution
}
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;
}
Compilation message
robots.cpp: In function 'int bfs(std::pair<int, int>, std::pair<int, int>)':
robots.cpp:41:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | auto [r1x, r1y, r2x, r2y, moves] = q.front(); q.pop();
| ^
robots.cpp:48:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
48 | for (auto& [new_r1x, new_r1y] : moves_r1) {
| ^
robots.cpp:55:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | for (auto& [new_r2x, new_r2y] : moves_r2) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |