GridWorld.h
Go to the documentation of this file.00001
00002 #ifndef INCLUDED_GridWorld_h_
00003 #define INCLUDED_GridWorld_h_
00004
00005 #include <vector>
00006 #include <iostream>
00007 #include <iomanip>
00008 #include <cmath>
00009
00010 class GridWorld {
00011 static const float HCOST;
00012 static const float VCOST;
00013 static const float DCOST;
00014
00015
00016 friend std::istream& operator>>(std::istream& is, GridWorld& gw);
00017
00018 friend std::ostream& operator<<(std::ostream& os, const GridWorld& gw);
00019
00020
00021 std::vector< std::vector<char> > world;
00022
00023 public:
00024
00025 struct State {
00026
00027 State() : r((size_t)-1), c((size_t)-1) {}
00028
00029 State(size_t row, size_t col) : r(row), c(col) {}
00030
00031 size_t hash() const {
00032 size_t s1 = (c << (sizeof(size_t)*8/4*1));
00033 size_t s2 = (r << (sizeof(size_t)*8/4*2));
00034 size_t s3 = (c << (sizeof(size_t)*8/4*3));
00035 return s3+s2+s1+r;
00036 }
00037
00038 bool operator<(const State& other) const { return r<other.r || (r==other.r && c<other.c); }
00039
00040 bool operator==(const State& other) const { return r==other.r && c==other.c; }
00041
00042 friend std::ostream& operator<<(std::ostream& os, const State& st) { return os << st.r << ',' << st.c; }
00043
00044 size_t r;
00045 size_t c;
00046 };
00047
00048
00049 GridWorld() : world() {}
00050
00051
00052 GridWorld(size_t r, size_t c) : world(r,std::vector<char>(c,' ')) {}
00053
00054
00055 bool validate(const State& st) const { return true; }
00056
00057
00058 float heuristic(const State& st, const State& goal) const;
00059
00060
00061
00062 const std::vector<std::pair<float,GridWorld::State> >& expand(const State* parent, const State& st, const State& goal) const;
00063
00064 char& operator()(size_t r, size_t c) { return world[r][c]; }
00065 char operator()(size_t r, size_t c) const { return world[r][c]; }
00066
00067 std::vector<char>& operator[](size_t r) { return world[r]; }
00068 const std::vector<char>& operator[](size_t r) const { return world[r]; }
00069
00070 char& operator[](const State& x) { return world[x.r][x.c]; }
00071 char operator[](const State& x) const { return world[x.r][x.c]; }
00072 };
00073
00074 #endif