Files
CSCI-1200/labs/recursion/checkpoint3.cpp
2025-03-12 12:09:28 -04:00

122 lines
3.7 KiB
C++

#include <fstream>
#include <iostream>
#include <list>
#include <vector>
class Point {
public:
Point(int x0, int y0) : x(x0), y(y0) {}
int x, y;
};
enum GRID_STATUS { GRID_CLEAR, GRID_BLOCKED };
void read_grid(std::istream& istr,
std::vector<std::vector<GRID_STATUS>>& blocked_grid,
int& start_x, int& start_y) {
int x, y;
int max_x = 0, max_y = 0;
std::list<Point> blocked_points;
while ((istr >> x >> y) && !(x == 0 && y == 0)) {
blocked_points.push_back(Point(x, y));
if (x > max_x) max_x = x;
if (y > max_y) max_y = y;
}
istr >> start_x >> start_y;
if (start_x > max_x) max_x = start_x;
if (start_y > max_y) max_y = start_y;
std::vector<GRID_STATUS> one_row_of_ys(max_y + 1, GRID_CLEAR);
std::vector<std::vector<GRID_STATUS>> empty_grid(max_x + 1, one_row_of_ys);
blocked_grid = empty_grid;
for (const auto& p : blocked_points) {
blocked_grid[p.x][p.y] = GRID_BLOCKED;
}
}
// Modified print_grid to show the path with '$' symbols
void print_grid(const std::vector<std::vector<GRID_STATUS>>& blocked_grid,
const std::vector<std::vector<bool>>& path_grid) {
std::cout << "Grid with path marked (X = blocked, $ = path, . = clear):\n";
for (unsigned int y = 0; y < blocked_grid[0].size(); ++y) {
for (unsigned int x = 0; x < blocked_grid.size(); ++x) {
if (path_grid[x][y]) {
std::cout << " $";
} else if (blocked_grid[x][y] == GRID_BLOCKED) {
std::cout << " X";
} else {
std::cout << " .";
}
}
std::cout << std::endl;
}
}
std::list<Point> find_path(const std::vector<std::vector<GRID_STATUS>>& blocked_grid, int x, int y) {
// no path
if (x < 0 || y < 0 || blocked_grid[x][y] == GRID_BLOCKED) {
return std::list<Point>();
}
// Base case
if (x == 0 && y == 0) {
std::list<Point> path;
path.push_back(Point(0, 0));
return path;
}
// Try moving left first
if (x > 0) {
std::list<Point> path_left = find_path(blocked_grid, x - 1, y);
if (!path_left.empty()) {
path_left.push_front(Point(x, y));
return path_left;
}
}
// If left fails, try moving up
if (y > 0) {
std::list<Point> path_up = find_path(blocked_grid, x, y - 1);
if (!path_up.empty()) {
path_up.push_front(Point(x, y));
return path_up;
}
}
// No path found
return std::list<Point>();
}
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " grid-file" << std::endl;
return 1;
}
std::ifstream istr(argv[1]);
if (!istr) {
std::cerr << "Could not open " << argv[1] << std::endl;
return 1;
}
std::vector<std::vector<GRID_STATUS>> blocked_grid;
int start_x, start_y;
read_grid(istr, blocked_grid, start_x, start_y);
// Find one path
std::list<Point> path = find_path(blocked_grid, start_x, start_y);
if (path.empty()) {
std::cout << "No legal path exists.\n";
} else {
std::cout << "Path found:\n";
for (const auto& p : path) {
std::cout << "(" << p.x << ", " << p.y << ") ";
}
std::cout << "\n";
// Create path grid for marking
std::vector<std::vector<bool>> path_grid(blocked_grid.size(),
std::vector<bool>(blocked_grid[0].size(), false));
for (const auto& p : path) {
path_grid[p.x][p.y] = true;
}
// Print grid with path
print_grid(blocked_grid, path_grid);
}
return 0;
}