demo/grid_graph_demo.cc
author alpar
Mon, 20 Feb 2006 06:38:18 +0000
changeset 1970 bd88ea06ab69
parent 1875 98698b69a902
permissions -rw-r--r--
Doc changes
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #include <lemon/grid_graph.h>
    20 #include <lemon/graph_adaptor.h>
    21 #include <lemon/graph_to_eps.h>
    22 #include <lemon/bfs.h>
    23 #include <lemon/xy.h>
    24 
    25 #include <iostream>
    26 #include <fstream>
    27 
    28 ///\ingroup demos
    29 ///\file
    30 ///\brief Labirinth example with grid graph.
    31 ///
    32 ///  Labirinth example with grid graph.
    33 ///
    34 /// The input file is:
    35 ///
    36 /// \include grid_graph_demo.in
    37 ///
    38 /// The result:
    39 ///
    40 /// \image html grid_graph_demo.png
    41 /// \image latex grid_graph_demo.eps "The labirinth" width=\textwidth
    42 ///
    43 /// The source:
    44 ///
    45 /// \include grid_graph_demo.cc
    46 
    47 using namespace lemon;
    48 using namespace std;
    49 
    50 int main() {
    51   ifstream in("grid_graph_demo.in");
    52   int width, height;
    53   in >> width >> height;
    54   int start_x, start_y;
    55   in >> start_x >> start_y;
    56   int stop_x, stop_y;
    57   in >> stop_x >> stop_y;
    58 
    59   GridGraph graph(width, height);
    60   GridGraph::Node start = graph(start_x - 1, start_y - 1);
    61   GridGraph::Node stop = graph(stop_x - 1, stop_y - 1);
    62   GridGraph::NodeMap<bool> filter(graph);
    63 
    64   for (int j = 0; j < height; ++j) {
    65     in >> ws;
    66     for (int i = 0; i < width; ++i) {
    67       char c; in >> c;
    68       filter[graph(i, j)] = (c == '.');
    69     }
    70   }
    71 
    72   typedef NodeSubGraphAdaptor<GridGraph,
    73     GridGraph::NodeMap<bool> > FilteredGraph;
    74 
    75   FilteredGraph filtered(graph, filter);
    76 
    77   Bfs<FilteredGraph> bfs(filtered);
    78   std::cout << "The length of shortest path: " << 
    79     bfs.run(start, stop) << std::endl;
    80 
    81   FilteredGraph::EdgeMap<bool> path(filtered, false);
    82   
    83   for (GridGraph::Node node = stop; 
    84        node != start; node = bfs.predNode(node)) {
    85     path[bfs.predEdge(node)] = true;
    86   }
    87   
    88   graphToEps(filtered, "grid_graph_demo.eps").scaleToA4().
    89     title("Grid graph").
    90     copyright("(C) 2006 LEMON Project").
    91     coords(scaleMap(indexMap(graph), 10)).
    92     enableParallel().
    93     nodeScale(0.5).
    94     drawArrows().
    95     edgeColors(composeMap(ColorSet(), path)).
    96     run();
    97   
    98   std::cout << "The shortest path is written to grid_graph_demo.eps" 
    99 	    << std::endl;
   100 
   101   return 0;
   102 }