// depth first search from vertex 0
// simple-minded
using namespace std;
#include <iostream>
#include <stack>

void dfs();
int getAdjacent(bool visited[],int current);
const int n = 7;
int matrix[n][n] = {0,1,0,0,0,0,1,
                 1,0,0,1,0,0,0,
                 0,0,0,0,1,1,0,
                 0,1,0,0,1,1,1,
                 0,0,1,1,0,0,1,
                 0,0,1,1,0,0,0,
                 1,0,0,1,1,0,0};

int main ()
{

   dfs();
   system("pause");
   return 0;
}


void dfs() 
{
   bool visited [n] = {false,false,false,false,false,false,false};
   stack <int> s;  

   int current = 0;  // start from vertex 0
   visited [current] = true;
   s.push(current);
   cout << char(current+'a') << endl;

   while (!s.empty())
   { 
       int current = getAdjacent(visited,s.top());
       if (current == -1) // no adjacent unvisited vertices
       {
          if (!s.empty()) 
            s.pop();
       }
       else
       {
          s.push(current);
          visited[current] = true;
          cout << char(current+'a') << endl;
       }
   }
} 


int getAdjacent(bool visited[],int current)
{
   // return next unvisited adjacent vertex to
   // current or -1 if none exists
   for (int col= 0; col < n; col++)
      if (matrix[current][col]==1 && !visited[col])
         return col;
   return -1; 
}
