Recursive maze solver Up for review today is some C++11 code to recursively search a maze for a path to a specified goal. This one shows dead-ends it explored on the way to finding the solution.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

enum { GOAL = '*', SPACE = ' ', WALL = '#', TRIED = '!', USED = '+' };

class maze {
    std::vector&lt;std::string&gt; data;
public:
    char &amp;operator()(size_t x, size_t y) {
        return data[y][x];
    }

    friend std::istream &amp;operator&gt;&gt;(std::istream &amp;is, maze &amp;m) {
        std::string temp;
        while (std::getline(is, temp))
            m.data.push_back(temp);
        return is;
    }

    friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, maze const &amp;m) {
        for (auto const &amp;s : m.data)
            os &lt;&lt; s &lt;&lt; '\n';
        return os;
    }

    size_t y_dim() { return data.size(); }
    size_t x_dim() { return data[0].size(); }
};

bool solve(maze &amp;m, size_t x = 0, size_t y=0) {
    if (x &lt; 0 || y &lt; 0 || x &gt;= m.x_dim() || y &gt;= m.y_dim())
        return false;

    if (m(x, y) == GOAL) 
        return true;

    if (m(x, y) != SPACE)
        return false;

    m(x, y) = USED;

    bool solved 
         = solve(m, x - 1, y)
        || solve(m, x + 1, y)
        || solve(m, x, y - 1)
        || solve(m, x, y + 1);

    if (!solved)
        m(x, y) = TRIED;

    return solved;
}

int main(){
    maze m;
    std::cin &gt;&gt; m;

    solve(m);
    std::cout &lt;&lt; m;     
}


Input is a simple text file of walls, spaces, and a goal, such as:

  #######
#    # ##
# ####  #
#      ##
# ### ###
#####  *#


Searching always commences from position 0, 0 (i.e., the top, left corner).
Try and engage the move constructor:

m.data.push_back(temp);


Because temp is a named variable it will hit the version that uses const&amp; T. To try and get the alternative version that engages the move constructor add std::move

m.data.push_back(std::move(temp));


The input operator does not make any attempt to make sure each line is the same size.

size_t x_dim() { return data[0].size(); }


So the above may not be accurate.
You can add a fake set of cells at the top/bottom/left/right of type 'WALL' then you don't need the test if (x &lt; 0 || y &lt; 0 || x &gt;= m.x_dim() || y &gt;= m.y_dim()) as it can never reach outside the bounds of the maze (this can be forced to be true because it is part of the code and not part of the user input). This means you don't need y_dim() as that is the only use case.

My main issue is that solve() actually mutates the maze object. Some form of visitor pattern may be a more re-usable technique, or solve() would be better as a member of maze().