#include #include #include struct Cell { int x { }; int y { }; }; std::vector get_neighbours(Cell const& cell) { std::vector cells { }; for (int dx = -1; dx <= 1; ++dx) { for (int dy = -1; dy <= 1; ++dy) { if (dx == 0 && dy == 0) continue; Cell const next { cell.x + dx, cell.y + dy }; cells.push_back(next); } } return cells; } bool in_bounds(std::vector> const& grid, Cell const& cell) { if (grid.empty()) return false; int const width { static_cast(grid.size()) }; int const height { static_cast(grid[0].size()) }; if (cell.x < 0 || cell.x >= width) return false; if (cell.y < 0 || cell.y >= height) return false; return true; } unsigned find_region_size(std::vector> grid, Cell const& start) { // implementera denna } int main(int argc, char** argv) { if (argc < 2) { std::cerr << "USAGE: " << argv[0] << " GRID" << std::endl; return 1; } std::ifstream ifs { argv[1] }; if (!ifs) { std::cerr << "ERROR: Unable to open " << argv[1] << std::endl; return 2; } unsigned width { }; unsigned height { }; ifs >> width >> height; std::vector> grid(width, std::vector(height)); // läs in hela rutnätet for (unsigned y { 0 }; y < height; ++y) { for (unsigned x { 0 }; x < width; ++x) { bool value { }; ifs >> value; grid[x][y] = value; } } Cell start { }; while (true) { std::cout << "Enter x and y coordinate: "; if (!(std::cin >> start.x >> start.y)) break; unsigned const count { find_region_size(grid, start) }; std::cout << "(" << start.x << ", " << start.y << ") " << "is in cluster of size " << count << std::endl; } }