demo/grid_graph_demo.cc
author deba
Fri, 30 Sep 2005 13:10:42 +0000
changeset 1690 7db44a7ab939
parent 1680 4f8b9cee576b
child 1693 269f0cbfbcc8
permissions -rw-r--r--
Bug fix.
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@1680
    10
using namespace lemon;
deba@1680
    11
using namespace std;
deba@1680
    12
deba@1680
    13
int main() {
deba@1681
    14
  ifstream in("grid_graph_demo.in");
deba@1681
    15
  int width, height;
deba@1681
    16
  in >> width >> height;
deba@1681
    17
  int start_x, start_y;
deba@1681
    18
  in >> start_x >> start_y;
deba@1681
    19
  int stop_x, stop_y;
deba@1681
    20
  in >> stop_x >> stop_y;
deba@1681
    21
deba@1681
    22
  GridGraph graph(width, height);
deba@1681
    23
  GridGraph::Node start = graph(start_x - 1, start_y - 1);
deba@1681
    24
  GridGraph::Node stop = graph(stop_x - 1, stop_y - 1);
deba@1681
    25
  GridGraph::NodeMap<bool> filter(graph);
deba@1681
    26
deba@1681
    27
  for (int j = 0; j < height; ++j) {
deba@1681
    28
    in >> ws;
deba@1681
    29
    for (int i = 0; i < width; ++i) {
deba@1681
    30
      char c; in >> c;
deba@1681
    31
      filter[graph(i, j)] = (c == '.');
deba@1681
    32
    }
deba@1681
    33
  }
deba@1681
    34
deba@1681
    35
  typedef NodeSubGraphAdaptor<GridGraph,
deba@1681
    36
    GridGraph::NodeMap<bool> > FilteredGraph;
deba@1681
    37
deba@1681
    38
  FilteredGraph filtered(graph, filter);
deba@1681
    39
deba@1681
    40
  Bfs<FilteredGraph> bfs(filtered);
deba@1681
    41
  std::cout << "The length of shortest path: " << 
deba@1681
    42
    bfs.run(start, stop) << std::endl;
deba@1681
    43
deba@1680
    44
  GridGraph::NodeMap<xy<double> > coord(graph);
deba@1680
    45
  for (int i = 0; i < graph.width(); ++i) {
deba@1680
    46
    for (int j = 0; j < graph.height(); ++j) {
deba@1681
    47
      coord[graph(i, j)] = xy<double>( i * 10.0, j * 10.0);
deba@1680
    48
    }
deba@1680
    49
  }
deba@1681
    50
  
deba@1681
    51
  FilteredGraph::EdgeMap<Color> color(filtered, Color(0.0, 0.0, 0.0));
deba@1681
    52
  
deba@1681
    53
  for (GridGraph::Node node = stop; 
deba@1681
    54
       node != start; node = bfs.predNode(node)) {
deba@1681
    55
    color[bfs.pred(node)] = Color(1.0, 0.0, 0.0);
deba@1681
    56
  }
deba@1681
    57
  
deba@1681
    58
  graphToEps(filtered, "grid_graph.eps").scaleToA4().
deba@1680
    59
    title("Grid graph").
deba@1680
    60
    copyright("(C) 2005 LEMON Project").
deba@1680
    61
    coords(coord).
deba@1680
    62
    enableParallel().
deba@1680
    63
    nodeScale(.45).
deba@1680
    64
    drawArrows().
deba@1681
    65
    edgeColors(color).
deba@1680
    66
    run();
deba@1681
    67
  
deba@1681
    68
  std::cout << "The shortest path is written to grid_graph.eps" << std::endl;
deba@1681
    69
deba@1680
    70
  return 0;
deba@1680
    71
}