#include <iostream>
#include <string>
#include <utility>
#include <vector>
struct position {
int row;
int col;
};
using ogre_map = std::vector<std::string>;
ogre_map sample_maps[] = {
{ // Example input 1
"@@........",
"@@O.......",
".....O.O..",
"..........",
"..O.O.....",
"..O....O.O",
".O........",
"..........",
".....OO...",
".........$"
},
{ // Example input 2
"@@........",
"@@O.......",
".....O.O..",
"..........",
"..O.O.....",
"..O....O.O",
".O........",
"..........",
".....OO.O.",
".........$"
},
{ // Challenge input 1
"$.O...O...",
"...O......",
"..........",
"O..O..O...",
"..........",
"O..O..O...",
"..........",
"......OO..",
"O..O....@@",
"........@@"
},
{ // Challenge input 2
".@@.....O.",
".@@.......",
"..O..O....",
".......O..",
"...O......",
"..........",
".......O.O",
"...O.O....",
".......O..",
".........$"
}
};
bool dfs(ogre_map& map, position pos) {
char old = map[pos.row][pos.col];
auto check_for = [&](char ch) {
return old == ch || map[pos.row][pos.col+1] == ch ||
map[pos.row+1][pos.col] == ch || map[pos.row+1][pos.col+1] == ch;
};
if (old == '&' || check_for('O'))
return false; // already been there, or can't go there
map[pos.row][pos.col] = '&';
if (check_for('$'))
return true; // we found it!
// recurse
if (pos.row > 0 && dfs(map, {pos.row-1, pos.col})) // up
return true;
if (pos.col > 0 && dfs(map, {pos.row, pos.col-1})) // left
return true;
if (pos.row < map.size()-2 && dfs(map, {pos.row+1, pos.col})) // down
return true;
if (pos.col < map[0].size()-2 && dfs(map, {pos.row, pos.col+1})) // right
return true;
// not found
map[pos.row][pos.col] = old;
return false;
}
void biggify(ogre_map& map) {
for (int r = map.size() - 1; r >= 0; --r)
for (int c = map[0].size() - 1; c >= 1; --c)
if ((r > 0 && (map[r-1][c-1] == '&' || map[r-1][c] == '&')) || map[r][c-1] == '&')
map[r][c] = '&';
}
position find_start(const ogre_map& map) {
for (int r = 0; r < map.size(); ++r)
for (int c = 0; c < map[0].size(); ++c)
if (map[r][c] == '@') return {r, c};
return {-1, -1};
}
int main() {
auto& input = sample_maps[2];
position ogre_start = find_start(input);
if (ogre_start.row < 0 || ogre_start.col < 0) {
std::cout << "Is this a joke? You didn't put an ogre anywhere on the map!\n";
std::exit(1);
}
if (dfs(input, ogre_start)) {
biggify(input);
for (auto& row : input) {
std::cout << row << '\n';
}
} else {
std::cout << "No Path\n";
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dXRpbGl0eT4KI2luY2x1ZGUgPHZlY3Rvcj4KCnN0cnVjdCBwb3NpdGlvbiB7CglpbnQgcm93OwoJaW50IGNvbDsKfTsKdXNpbmcgb2dyZV9tYXAgPSBzdGQ6OnZlY3RvcjxzdGQ6OnN0cmluZz47CgpvZ3JlX21hcCBzYW1wbGVfbWFwc1tdID0gewoJeyAvLyBFeGFtcGxlIGlucHV0IDEKCQkiQEAuLi4uLi4uLiIsCgkJIkBATy4uLi4uLi4iLAoJCSIuLi4uLk8uTy4uIiwKCQkiLi4uLi4uLi4uLiIsCgkJIi4uTy5PLi4uLi4iLAoJCSIuLk8uLi4uTy5PIiwKCQkiLk8uLi4uLi4uLiIsCgkJIi4uLi4uLi4uLi4iLAoJCSIuLi4uLk9PLi4uIiwKCQkiLi4uLi4uLi4uJCIKCX0sCgl7IC8vIEV4YW1wbGUgaW5wdXQgMgoJCSJAQC4uLi4uLi4uIiwKCQkiQEBPLi4uLi4uLiIsCgkJIi4uLi4uTy5PLi4iLAoJCSIuLi4uLi4uLi4uIiwKCQkiLi5PLk8uLi4uLiIsCgkJIi4uTy4uLi5PLk8iLAoJCSIuTy4uLi4uLi4uIiwKCQkiLi4uLi4uLi4uLiIsCgkJIi4uLi4uT08uTy4iLAoJCSIuLi4uLi4uLi4kIgoJfSwKCXsgLy8gQ2hhbGxlbmdlIGlucHV0IDEKCQkiJC5PLi4uTy4uLiIsCgkJIi4uLk8uLi4uLi4iLAoJCSIuLi4uLi4uLi4uIiwKCQkiTy4uTy4uTy4uLiIsCgkJIi4uLi4uLi4uLi4iLAoJCS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