Guest User

Untitled

a guest
Jun 30th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.91 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <map>
  4. #include <algorithm>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. const char FLOOR = '1' ;
  11. const char WALL  = '0' ;
  12. const char GOAL  = 'G' ;
  13.  
  14. const int MapWidth = 25, MapHeight = 25;
  15. //       Y pos      X pos
  16. char Map[MapHeight][MapWidth] = {
  17.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', 'G'},
  18.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  19.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  20.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  21.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  22.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  23.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  24.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  25.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  26.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  27.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  28.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  29.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  30.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  31.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  32.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  33.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  34.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  35.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  36.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  37.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  38.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  39.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  40.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  41.     { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'}
  42. };
  43.  
  44. struct Pos
  45. {
  46.     short x,y;
  47.     Pos operator + ( Pos p ) const { return Pos(x+p.x,y+p.y); }
  48.     bool operator < ( Pos p ) const { return ( y==p.y ) ? (x<p.x) : (y<p.y) ; }
  49.     bool operator != ( Pos p ) const { return ( y!=p.y ) || (x!=p.x) ; }
  50.     Pos(short x=0,short y=0) : x(x), y(y) {}
  51. };
  52.  
  53. bool valid(Pos p) { return p.x>=0 && p.x<MapWidth && p.y>=0 && p.y<MapHeight; }
  54.  
  55. enum Dir { d_beg, d_up=d_beg, d_rg, d_dn, d_lf, d_end };
  56.  
  57. Pos deltas[d_end] = { {0,-1}, {+1,0}, {0,+1}, {-1,0} };
  58.  
  59. Dir& operator ++ ( Dir& d ) { d = (Dir) ( 1+(int)d ) ; return d; }
  60.  
  61. Dir other(Dir d)
  62. {
  63.     switch(d)
  64.     {
  65.     case d_up: return d_dn;
  66.     case d_rg: return d_lf;
  67.     case d_dn: return d_up;
  68.     case d_lf: return d_rg;
  69.     default: return d_end;
  70.     }
  71. }
  72.  
  73. struct SearchMapItem
  74. {
  75.     bool traversble;
  76.     bool goal;
  77.     bool visited;
  78.     int cost_here;
  79.     Dir came_from;
  80.     bool paths[d_end];
  81. };
  82.  
  83. map<Pos,SearchMapItem> search_map;
  84. typedef map<Pos,SearchMapItem>::iterator SMII;
  85.  
  86. void MakeMap()
  87. {
  88.     search_map.clear();
  89.     Pos p;
  90.     for(p.y=0;p.y<MapHeight;++p.y) for(p.x=0;p.x<MapWidth;++p.x)
  91.     {
  92.         SearchMapItem smi;
  93.         smi.visited = false;
  94.         smi.cost_here = -1;
  95.         smi.came_from = d_end;
  96.         if( Map[p.y][p.x] == WALL )
  97.         {
  98.             smi.traversble = false;
  99.         }
  100.         else if( Map[p.y][p.x] == GOAL )
  101.         {
  102.             smi.traversble = true;
  103.             smi.goal = true;
  104.         }
  105.         else if( Map[p.y][p.x] == FLOOR )
  106.         {
  107.             smi.traversble = true;
  108.             smi.goal = false;
  109.             for( Dir d = d_beg; d != d_end; ++d )
  110.             {
  111.                 Pos p2 = p + deltas[d];
  112.                 smi.paths[d] = valid(p2) && (Map[p2.y][p2.x] != WALL) ;
  113.             }
  114.         }
  115.         search_map[p] = smi;
  116.     }
  117. }
  118.  
  119. void FindGoalFrom( Pos start )
  120. {
  121.     vector<SMII> found;
  122.  
  123.     {
  124.         SMII smii = search_map.find(start);
  125.  
  126.         if(smii==search_map.end()) { cout << "starting outside map\n"; return; }
  127.         if(smii->second.goal) { cout << "already at target\n"; return; }
  128.         if(!smii->second.traversble) { cout << "starting in a wall\n"; return; }
  129.  
  130.         smii->second.visited = true;
  131.         smii->second.cost_here = 0;
  132.         found.push_back(smii);
  133.     }
  134.  
  135.     int cost_so_far = 0;
  136.     bool did_find = false;
  137.  
  138.     while(!did_find)
  139.     {
  140.  
  141.         vector<SMII> candidates;
  142.  
  143.         for( SMII smii : found )
  144.         {
  145.             for( Dir d = d_beg; d != d_end; ++d )
  146.             {
  147.                 if( ! smii->second.paths[d] ) continue;
  148.                 Pos p = smii->first + deltas[d];
  149.                 if(!valid(p)) continue;
  150.                 SMII cand = search_map.find(p);
  151.                 if(cand==search_map.end()) continue;
  152.                 if(cand->second.visited) continue;
  153.                 cand->second.visited = true;
  154.                 cand->second.came_from=d;
  155.                 candidates.push_back(cand);
  156.             }
  157.         }
  158.  
  159.         ++cost_so_far;
  160.  
  161.         found.clear();
  162.         if( candidates.empty() ) break;
  163.  
  164.         for( SMII smii : candidates )
  165.         {
  166.             smii->second.visited = true;
  167.             smii->second.cost_here = cost_so_far;
  168.             found.push_back(smii);
  169.             if( smii->second.goal ) { did_find = true; break; }
  170.         }
  171.  
  172.     }
  173.  
  174.     if( ! did_find ) { cout << "no goal reachable\n"; return; }
  175.  
  176.     SMII pos = found.back();
  177.  
  178.     vector<Dir> path;
  179.  
  180.     while( pos->first != start )
  181.     {
  182.         Dir d = pos->second.came_from;
  183.         path.push_back(d);
  184.         Pos p = pos->first + deltas[ other(d) ];
  185.         pos = search_map.find(p);
  186.     }
  187.  
  188.     for( auto itr = path.rbegin(); itr != path.rend(); ++itr )
  189.     {
  190.         const char* dir_names[] = { "Up", "Right", "Down", "Left" } ;
  191.         cout << "Walk " << dir_names[*itr] << endl;
  192.     }
  193.     cout << "Then you are at goal in total " << to_string(cost_so_far) << " steps" << endl;
  194. }
  195.  
  196. int main()
  197. {
  198.     MakeMap();
  199.     FindGoalFrom( Pos(5,9) );
  200.     cout << "\ndone\n";
  201. }
Advertisement
Add Comment
Please, Sign In to add comment