Search code examples
c++liststlbacktrackingmaze

How does the iterator works in following program


The following program is solving maze without recursion. Program is working but I'm unable to figure out how the iterator iter works.

there are two comments line 19 and line 21. I tried printing the value of iterator its giving me

{1,0,1,0,1,0,3,4,3,4}

and

{1,0,2,1,0,2,1,3,0,4,3,5,4,3,5,7,4,8}`

respectively.

The simpleMaze in main() shows the connections between elements and path of maze. As 0 is connected to (1,3); 1 is connected to (0,2) and so on.

Iterator is used for traversing then why this program is giving such sequence also I'm unable to understand the flow of function solveMaze.

#include <bits/stdc++.h>

using namespace std;

list<int> solveMaze(list<int> maze[],int start,int finish) {
    unordered_set<int> visited;
    list<int> path;

    path.push_back(start);
    int currentPoint=start;
    visited.insert(currentPoint);

    while(path.back()!= finish && path.empty() == false)     {
        list<int>::iterator iter = maze[currentPoint].begin();
        bool foundOutlet = false;
        cout<<*iter<<"\n";     //line 19
        while(iter!=maze[currentPoint].end() && (foundOutlet== false)) {
        cout<<*iter<<"\n";   //line 21
            if(visited.count(*iter)==0) {
                foundOutlet = true;
            }
            else {
                iter++;
            }
        }
        if(foundOutlet)
        {
            path.push_back(*iter);
            visited.insert(*iter);
        }
        else
        path.pop_back();
        currentPoint= path.back();
    }

    return path;
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    list <int> simpleMaze[9],path;
    simpleMaze[0].push_back(1);
    simpleMaze[0].push_back(3);

    simpleMaze[1].push_back(0);
    simpleMaze[1].push_back(2);

    simpleMaze[2].push_back(1);

    simpleMaze[3].push_back(0);
    simpleMaze[3].push_back(4);
    simpleMaze[3].push_back(6);


    simpleMaze[4].push_back(3);
    simpleMaze[4].push_back(5);
    simpleMaze[4].push_back(7);

    simpleMaze[5].push_back(4);

    simpleMaze[6].push_back(3);

    simpleMaze[7].push_back(4);
    simpleMaze[7].push_back(8);

    simpleMaze[8].push_back(7);


    path = solveMaze(simpleMaze,0,8);

    list<int>::iterator it;
    for(it=path.begin();it!=path.end();++it)
    {
        cout<<*it<<"\n";
    }
    return 0;

}

The solution of the maze is

0 3 4 7 8


Solution

  • At line 19, you print out the first position adjacent to the "current" position.

    At line 21 you are printing out the locations connected to the "current" position that you consider. You only visit a position going forward if you have never visited it before.

    If there are no unvisited "next" places, you know that the solution doesn't involve going through the "current" location, so you go back one step, and try later connections from there.

    Following it through a few steps:

    Start 
     At 0
      See 1, it is unvisited, go there
     At 1
      See 0, it is visited
      See 2, it is unvisited, go there
     At 2
      See 1, it is visited
      No more locations at 2, backtrack
     At 1
      See 0, it is visited
      See 2, it is visited
      No more locations at 1, backtrack
     At 0
      See 1, it is visited
      See 3, it is unvisited, go there
    ....