deba@1680
|
1 |
#include <lemon/grid_graph.h>
|
deba@1680
|
2 |
#include <lemon/graph_adaptor.h>
|
deba@1680
|
3 |
#include <lemon/graph_to_eps.h>
|
deba@1681
|
4 |
#include <lemon/bfs.h>
|
deba@1680
|
5 |
#include <lemon/xy.h>
|
deba@1680
|
6 |
|
deba@1680
|
7 |
#include <iostream>
|
deba@1680
|
8 |
#include <fstream>
|
deba@1680
|
9 |
|
deba@1775
|
10 |
///\ingroup demos
|
deba@1775
|
11 |
///\file
|
deba@1775
|
12 |
///\brief Labirinth example with grid graph.
|
deba@1775
|
13 |
///
|
deba@1775
|
14 |
/// Labirinth example with grid graph.
|
deba@1775
|
15 |
///
|
deba@1775
|
16 |
/// The input file is:
|
deba@1775
|
17 |
///
|
deba@1775
|
18 |
/// \include grid_graph_demo.in
|
deba@1775
|
19 |
///
|
deba@1775
|
20 |
/// The result:
|
deba@1775
|
21 |
///
|
deba@1775
|
22 |
/// \image html grid_graph_demo.png
|
deba@1775
|
23 |
/// \image latex grid_graph_demo.eps "The labirinth" width=\textwidth
|
deba@1775
|
24 |
///
|
deba@1775
|
25 |
/// The source:
|
deba@1775
|
26 |
///
|
deba@1775
|
27 |
/// \include grid_graph_demo.cc
|
deba@1775
|
28 |
|
deba@1680
|
29 |
using namespace lemon;
|
deba@1680
|
30 |
using namespace std;
|
deba@1680
|
31 |
|
deba@1680
|
32 |
int main() {
|
deba@1681
|
33 |
ifstream in("grid_graph_demo.in");
|
deba@1681
|
34 |
int width, height;
|
deba@1681
|
35 |
in >> width >> height;
|
deba@1681
|
36 |
int start_x, start_y;
|
deba@1681
|
37 |
in >> start_x >> start_y;
|
deba@1681
|
38 |
int stop_x, stop_y;
|
deba@1681
|
39 |
in >> stop_x >> stop_y;
|
deba@1681
|
40 |
|
deba@1681
|
41 |
GridGraph graph(width, height);
|
deba@1681
|
42 |
GridGraph::Node start = graph(start_x - 1, start_y - 1);
|
deba@1681
|
43 |
GridGraph::Node stop = graph(stop_x - 1, stop_y - 1);
|
deba@1681
|
44 |
GridGraph::NodeMap<bool> filter(graph);
|
deba@1681
|
45 |
|
deba@1681
|
46 |
for (int j = 0; j < height; ++j) {
|
deba@1681
|
47 |
in >> ws;
|
deba@1681
|
48 |
for (int i = 0; i < width; ++i) {
|
deba@1681
|
49 |
char c; in >> c;
|
deba@1681
|
50 |
filter[graph(i, j)] = (c == '.');
|
deba@1681
|
51 |
}
|
deba@1681
|
52 |
}
|
deba@1681
|
53 |
|
deba@1681
|
54 |
typedef NodeSubGraphAdaptor<GridGraph,
|
deba@1681
|
55 |
GridGraph::NodeMap<bool> > FilteredGraph;
|
deba@1681
|
56 |
|
deba@1681
|
57 |
FilteredGraph filtered(graph, filter);
|
deba@1681
|
58 |
|
deba@1681
|
59 |
Bfs<FilteredGraph> bfs(filtered);
|
deba@1681
|
60 |
std::cout << "The length of shortest path: " <<
|
deba@1681
|
61 |
bfs.run(start, stop) << std::endl;
|
deba@1681
|
62 |
|
deba@1693
|
63 |
FilteredGraph::EdgeMap<bool> path(filtered, false);
|
deba@1681
|
64 |
|
deba@1681
|
65 |
for (GridGraph::Node node = stop;
|
deba@1681
|
66 |
node != start; node = bfs.predNode(node)) {
|
deba@1763
|
67 |
path[bfs.predEdge(node)] = true;
|
deba@1681
|
68 |
}
|
deba@1681
|
69 |
|
deba@1775
|
70 |
graphToEps(filtered, "grid_graph_demo.eps").scaleToA4().
|
deba@1680
|
71 |
title("Grid graph").
|
deba@1680
|
72 |
copyright("(C) 2005 LEMON Project").
|
deba@1693
|
73 |
coords(scaleMap(indexMap(graph), 10)).
|
deba@1680
|
74 |
enableParallel().
|
deba@1693
|
75 |
nodeScale(0.5).
|
deba@1680
|
76 |
drawArrows().
|
deba@1693
|
77 |
edgeColors(composeMap(ColorSet(), path)).
|
deba@1680
|
78 |
run();
|
deba@1681
|
79 |
|
deba@1775
|
80 |
std::cout << "The shortest path is written to grid_graph_demo.eps"
|
deba@1775
|
81 |
<< std::endl;
|
deba@1681
|
82 |
|
deba@1680
|
83 |
return 0;
|
deba@1680
|
84 |
}
|