SubGraphAdaptors with edge checking functionality.
Improved grid_graph_demo
1 #include <lemon/grid_graph.h>
2 #include <lemon/graph_adaptor.h>
3 #include <lemon/graph_to_eps.h>
10 using namespace lemon;
14 ifstream in("grid_graph_demo.in");
16 in >> width >> height;
18 in >> start_x >> start_y;
20 in >> stop_x >> stop_y;
22 GridGraph graph(width, height);
23 GridGraph::Node start = graph(start_x - 1, start_y - 1);
24 GridGraph::Node stop = graph(stop_x - 1, stop_y - 1);
25 GridGraph::NodeMap<bool> filter(graph);
27 for (int j = 0; j < height; ++j) {
29 for (int i = 0; i < width; ++i) {
31 filter[graph(i, j)] = (c == '.');
35 typedef NodeSubGraphAdaptor<GridGraph,
36 GridGraph::NodeMap<bool> > FilteredGraph;
38 FilteredGraph filtered(graph, filter);
40 Bfs<FilteredGraph> bfs(filtered);
41 std::cout << "The length of shortest path: " <<
42 bfs.run(start, stop) << std::endl;
44 GridGraph::NodeMap<xy<double> > coord(graph);
45 for (int i = 0; i < graph.width(); ++i) {
46 for (int j = 0; j < graph.height(); ++j) {
47 coord[graph(i, j)] = xy<double>( i * 10.0, j * 10.0);
51 FilteredGraph::EdgeMap<Color> color(filtered, Color(0.0, 0.0, 0.0));
53 for (GridGraph::Node node = stop;
54 node != start; node = bfs.predNode(node)) {
55 color[bfs.pred(node)] = Color(1.0, 0.0, 0.0);
58 graphToEps(filtered, "grid_graph.eps").scaleToA4().
60 copyright("(C) 2005 LEMON Project").
68 std::cout << "The shortest path is written to grid_graph.eps" << std::endl;