demo/grid_graph_demo.cc
changeset 1681 84e43c7ca1e3
parent 1680 4f8b9cee576b
child 1693 269f0cbfbcc8
     1.1 --- a/demo/grid_graph_demo.cc	Mon Sep 12 09:19:52 2005 +0000
     1.2 +++ b/demo/grid_graph_demo.cc	Mon Sep 12 11:24:54 2005 +0000
     1.3 @@ -1,6 +1,7 @@
     1.4  #include <lemon/grid_graph.h>
     1.5  #include <lemon/graph_adaptor.h>
     1.6  #include <lemon/graph_to_eps.h>
     1.7 +#include <lemon/bfs.h>
     1.8  #include <lemon/xy.h>
     1.9  
    1.10  #include <iostream>
    1.11 @@ -10,20 +11,61 @@
    1.12  using namespace std;
    1.13  
    1.14  int main() {
    1.15 -  GridGraph graph(5, 7);
    1.16 +  ifstream in("grid_graph_demo.in");
    1.17 +  int width, height;
    1.18 +  in >> width >> height;
    1.19 +  int start_x, start_y;
    1.20 +  in >> start_x >> start_y;
    1.21 +  int stop_x, stop_y;
    1.22 +  in >> stop_x >> stop_y;
    1.23 +
    1.24 +  GridGraph graph(width, height);
    1.25 +  GridGraph::Node start = graph(start_x - 1, start_y - 1);
    1.26 +  GridGraph::Node stop = graph(stop_x - 1, stop_y - 1);
    1.27 +  GridGraph::NodeMap<bool> filter(graph);
    1.28 +
    1.29 +  for (int j = 0; j < height; ++j) {
    1.30 +    in >> ws;
    1.31 +    for (int i = 0; i < width; ++i) {
    1.32 +      char c; in >> c;
    1.33 +      filter[graph(i, j)] = (c == '.');
    1.34 +    }
    1.35 +  }
    1.36 +
    1.37 +  typedef NodeSubGraphAdaptor<GridGraph,
    1.38 +    GridGraph::NodeMap<bool> > FilteredGraph;
    1.39 +
    1.40 +  FilteredGraph filtered(graph, filter);
    1.41 +
    1.42 +  Bfs<FilteredGraph> bfs(filtered);
    1.43 +  std::cout << "The length of shortest path: " << 
    1.44 +    bfs.run(start, stop) << std::endl;
    1.45 +
    1.46    GridGraph::NodeMap<xy<double> > coord(graph);
    1.47    for (int i = 0; i < graph.width(); ++i) {
    1.48      for (int j = 0; j < graph.height(); ++j) {
    1.49 -      coord[graph(i, j)] = xy<double>(i * 10.0, j * 10.0);
    1.50 +      coord[graph(i, j)] = xy<double>( i * 10.0, j * 10.0);
    1.51      }
    1.52    }
    1.53 -  graphToEps(graph, "grid_graph.eps").scaleToA4().
    1.54 +  
    1.55 +  FilteredGraph::EdgeMap<Color> color(filtered, Color(0.0, 0.0, 0.0));
    1.56 +  
    1.57 +  for (GridGraph::Node node = stop; 
    1.58 +       node != start; node = bfs.predNode(node)) {
    1.59 +    color[bfs.pred(node)] = Color(1.0, 0.0, 0.0);
    1.60 +  }
    1.61 +  
    1.62 +  graphToEps(filtered, "grid_graph.eps").scaleToA4().
    1.63      title("Grid graph").
    1.64      copyright("(C) 2005 LEMON Project").
    1.65      coords(coord).
    1.66      enableParallel().
    1.67      nodeScale(.45).
    1.68      drawArrows().
    1.69 +    edgeColors(color).
    1.70      run();
    1.71 +  
    1.72 +  std::cout << "The shortest path is written to grid_graph.eps" << std::endl;
    1.73 +
    1.74    return 0;
    1.75  }