loulett

maze_with_A_star

May 19th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <queue>
  6. #include <sstream>
  7. #include <iterator>
  8. #include <functional>
  9. #include <cmath>
  10.  
  11. int INF = 1000000;
  12.  
  13. std::vector<std::vector<int> > maze;
  14. std::vector<std::vector<int> > length;
  15. std::vector<std::vector<int> > visited;
  16. std::priority_queue<std::pair<int, std::pair<int, int> >, std::vector<std::pair<int, std::pair<int, int> > >, std::greater<std::pair<int, std::pair<int, int> > > > queue;
  17.  
  18. int h(int x, int y, int finish_x, int finish_y)
  19. {
  20.     //return 10 * static_cast<int>(ceil(sqrt((x - finish_x)*(x - finish_x) + (y - finish_y)*(y - finish_y))));
  21.     return 10 * (abs(x - finish_x) + abs(y - finish_y));
  22. }
  23.  
  24. void dijkstra(int finish_x, int finish_y, int n)
  25. {
  26.     while (!(queue.empty()))
  27.     {
  28.         int x = (queue.top().second).first;
  29.         int y = (queue.top().second).second;
  30.         queue.pop();
  31.         visited[x][y] = 1;
  32.         int delta[][2] = { {-1, 0}, {1, 0}, {0, 1}, {0, -1} };
  33.         for (int i = 0; i < 4; i++)
  34.         {
  35.             if (x + delta[i][0] < n && y + delta[i][1] < n && x + delta[i][0] > -1 && y + delta[i][1] > -1 &&
  36.                 !(maze[x + delta[i][0]][y + delta[i][1]]) && length[x + delta[i][0]][y + delta[i][1]] > length[x][y] + 1 && !(visited[x + delta[i][0]][y + delta[i][1]]))
  37.             {
  38.                 length[x + delta[i][0]][y + delta[i][1]] = length[x][y] + 1;
  39.                 queue.push(std::make_pair(length[x + delta[i][0]][y + delta[i][1]] + h(x, y, finish_x, finish_y), std::make_pair(x + delta[i][0], y + delta[i][1])));
  40.             }
  41.         }
  42.         if (x == finish_x && y == finish_y)
  43.             return;
  44.     }
  45. }
  46.  
  47. int main()
  48. {
  49.     std::ifstream finp;
  50.     std::string s;
  51.     finp.open("input.txt");
  52.     int n, x1, x2, y1, y2;
  53.     finp >> n;
  54.     maze.resize(n);
  55.     length.resize(n, std::vector<int>(n, INF));
  56.     visited.resize(n, std::vector<int>(n));
  57.     std::getline (finp, s);
  58.     for (int i = 0; i < n; i++)
  59.     {
  60.         std::getline(finp, s);
  61.         std::istringstream buf(s);
  62.         std::istream_iterator<std::string> beg(buf), end;
  63.         std::vector<std::string> arr(beg, end);
  64.  
  65.         for (int j = 0; j < arr.size(); j += 2)
  66.         {
  67.             maze[i].resize(std::stoi(arr[j]));
  68.             maze[i].resize(std::stoi(arr[j + 1]), 1);
  69.         }
  70.         maze[i].resize(n);
  71.     }
  72.     finp >> x1 >> y1;
  73.     finp >> x2 >> y2;
  74.     length[x1][y1] = 0;
  75.     queue.push(std::make_pair(0, std::make_pair(x1, y1)));
  76.     dijkstra(x2, y2, n);
  77.     std::cout << length[x2][y2];
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment