1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <iostream>
#include <vector>
#include <string>
struct vec
{
unsigned x, y;
};
using grid_type = std::vector<std::string>;
vec operator+(vec a, vec b)
{
return{ a.x + b.x, a.y + b.y };
}
vec dir [] = { {0,-1}, {0,1}, {-1, 0}, {1, 0} };
char& at(grid_type& grid, vec pos)
{
return grid[pos.y][pos.x];
}
void display(const grid_type& grid)
{
for (auto& row : grid)
std::cout << row << '\n';
std::cout << '\n';
}
void iterate_path(grid_type& grid, vec pos, unsigned node = 0 )
{
if (at(grid, pos) != ' ')
return;
std::cout << "Row: " << pos.y << ", Col: " << pos.x << '\n';
at(grid, pos) = '0' + node;
display(grid);
std::cin.ignore(1000, '\n');
for (auto dir_vec : dir)
iterate_path(grid, pos + dir_vec, node + 1);
std::cout << "Backtracking..\n";
at(grid, pos) = ' ';
}
int main()
{
std::vector<std::string> grid =
{
"#####",
"# #",
"# #",
"# #",
"######",
};
vec start_pos = { 1,1 };
iterate_path(grid, start_pos);
}
|