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