You are given a rectangle with its bottom-left vertex at the origin of the cartesian plane and the top-right vertex at (X,Y)
. You are at the top-left corner and need to reach the bottom-right corner.
There are N
obstacles in the form of circles. You cannot pass through any circle. You cannot move on the edge of the rectangle or a circle. Find out if it is possible to reach the destination.
Note: Centers of all circles lie within the rectangle.
code in cpp
Assistant
To solve this problem, we can model the rectangle and obstacles as a grid and perform a BFS (Breadth-First Search) to determine if there's a path from the starting point to the destination while avoiding the obstacles and boundaries.
Here's the step-by-step approach:
Grid Representation: Discretize the rectangle into a 2D grid. Each cell in the grid represents a position in the rectangle.
Mark Obstacles: For each circle, we determine which grid cells it covers and mark them as blocked.
Avoid Boundaries: Since we cannot be on the rectangle boundaries, we exclude the outermost cells from consideration.
BFS Traversal: Start BFS from the start cell (just inside the rectangle) and explore neighboring cells. Move to adjacent cells that are not blocked and not visited before.
Check for Destination: If we reach the end cell (just inside the rectangle at the opposite corner), then a path exists.
Efficiency Considerations: We use an acceptable grid resolution to balance between computational efficiency and accuracy.
Below is the C++ code implementing the above approach:
cpp
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
// Grid dimensions
const int GRID_SIZE = 1000;
// Directions for BFS (up, down, left, right, and diagonals)
const int dx[8] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1};
struct Circle {
double x, y, r;
};
int main() {
double X, Y;
int N;
cin >> X >> Y;
cin >> N;
vector<Circle> circles(N);
for (int i = 0; i < N; i++) {
cin >> circles[i].x >> circles[i].y >> circles[i].r;
}
// Grid cell size
double dx_cell = X / GRID_SIZE;
double dy_cell = Y / GRID_SIZE;
// Grid representation
vector<vector<bool>> grid(GRID_SIZE, vector<bool>(GRID_SIZE, false));
// Mark boundaries as blocked (we cannot be on edges)
for (int i = 0; i < GRID_SIZE; i++) {
grid[i][0] = true;
grid[i][GRID_SIZE - 1] = true;
}
for (int j = 0; j < GRID_SIZE; j++) {
grid[0][j] = true;
grid[GRID_SIZE - 1][j] = true;
}
// Mark cells covered by circles as blocked
for (const auto& circle : circles) {
int i_min = max(1, (int)((circle.x - circle.r) / dx_cell));
int i_max = min(GRID_SIZE - 2, (int)((circle.x + circle.r) / dx_cell));
int j_min = max(1, (int)((circle.y - circle.r) / dy_cell));
int j_max = min(GRID_SIZE - 2, (int)((circle.y + circle.r) / dy_cell));
for (int i = i_min; i <= i_max; i++) {
for (int j = j_min; j <= j_max; j++) {
double cell_center_x = i * dx_cell + dx_cell / 2.0;
double cell_center_y = j * dy_cell + dy_cell / 2.0;
double dist_sq = (cell_center_x - circle.x) * (cell_center_x - circle.x) +
(cell_center_y - circle.y) * (cell_center_y - circle.y);
if (dist_sq < circle.r * circle.r) {
grid[i][j] = true;
}
}
}
}
// BFS from the start cell
int start_i = 1; // Just inside the rectangle on the left
int start_j = GRID_SIZE - 2; // Just inside the rectangle at the top
int end_i = GRID_SIZE - 2; // Just inside the rectangle on the right
int end_j = 1; // Just inside the rectangle at the bottom
queue<pair<int, int>> q;
vector<vector<bool>> visited(GRID_SIZE, vector<bool>(GRID_SIZE, false));
if (grid[start_i][start_j]) {
cout << "No" << endl;
return 0;
}
q.push({start_i, start_j});
visited[start_i][start_j] = true;
bool found = false;
while (!q.empty()) {
auto [i, j] = q.front();
q.pop();
if (i == end_i && j == end_j) {
found = true;
break;
}
for (int dir = 0; dir < 8; dir++) {
int ni = i + dx[dir];
int nj = j + dy[dir];
if (ni >= 1 && ni < GRID_SIZE - 1 && nj >= 1 && nj < GRID_SIZE - 1) {
if (!grid[ni][nj] && !visited[ni][nj]) {
visited[ni][nj] = true;
q.push({ni, nj});
}
}
}
}
if (found) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
Explanation:
Input: The rectangle dimensions X, Y, and the number of circles N.
Circles: We store the circles' positions and radii.
Grid: We define a grid of size GRID_SIZE x GRID_SIZE to represent the rectangle.
Boundaries: We mark the boundary cells as blocked.
Obstacles: We mark the grid cells covered by the circles as blocked by checking if the cell centers are within any circle.
BFS: We perform BFS from the start cell. If we reach the end cell, we output Yes; otherwise, we output No.
Note:
Efficiency: This approach discretizes the problem and is efficient for reasonable grid sizes and numbers of circles.
Precision: We should ensure that the grid cell size balances between computational efficiency and the need for accuracy in representing obstacles.
Constraints: For large rectangles or many circles, the GRID_SIZE may need adjustment.