Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <windows.h>
  4.  
  5. #include<queue>
  6. #include<stack>
  7.  
  8. using namespace std;
  9.  
  10. struct Edge {
  11.     COORD begin;
  12.     COORD end;
  13. };
  14.  
  15. onMap(COORD c, char **mas, int rows, int cols) {
  16.     if (c.X >= cols || c.Y >= rows) return 0;
  17.     if (c.X < 0 || c.Y < 0) return 0;
  18.     if (mas[c.Y][c.X] == '#') return 0;
  19.     return 1;
  20. }
  21.  
  22. void find_path(char** mas, int rows, int cols) {
  23.     queue<COORD> Queue;
  24.     stack<Edge> Stack;
  25.    
  26.     char ** visited;
  27.     visited = new char*[rows];
  28.     for (int i = 0; i < rows; i++) {
  29.         visited[i] = new char[cols];
  30.         for (int j = 0; j < cols; j++)
  31.             visited[i][j] = 0;
  32.     }
  33.    
  34.     HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
  35.     COORD B, E;
  36.    
  37.     // find init coord in grid
  38.     for (int i = 0; i < rows; i++) {
  39.         for (int j = 0; j < cols; j++) {
  40.             if (mas[i][j] == 'B') {
  41.                 B.X = j;
  42.                 B.Y = i;
  43.             } else if(mas[i][j] == 'E') {
  44.                 E.X = j;
  45.                 E.Y = i;
  46.             }
  47.         }
  48.     }
  49.    
  50.     cout << "Begin: (" << B.X << "; " << B.Y << ")" << endl;
  51.     cout << "End  : (" << E.X << "; " << E.Y << ")" << endl;
  52.    
  53.     Queue.push(B);
  54.    
  55.     bool flag = false;
  56.    
  57.     while (!Queue.empty()) {
  58.         COORD c = Queue.front();
  59.         COORD r;
  60.         Edge e;
  61.         e.begin = c;
  62.         Queue.pop();
  63.         visited[c.Y][c.X] = 2;
  64.         if (c.X == E.X && c.Y == E.Y) {
  65.             flag = true;
  66.             break;
  67.         }
  68.         r.X = c.X - 1;
  69.         r.Y = c.Y;
  70.         if (onMap(r, mas, rows, cols) && visited[r.Y][r.X] == 0) {
  71.                 Queue.push(r);
  72.                 visited[r.Y][r.X] = 1;
  73.                 e.end = r;
  74.                 Stack.push(e);
  75.         }
  76.         r.X = c.X + 1;
  77.         if (onMap(r, mas, rows, cols) && visited[r.Y][r.X] == 0) {
  78.             Queue.push(r);
  79.             visited[r.Y][r.X] = 1;
  80.             e.end = r;
  81.             Stack.push(e);
  82.         }
  83.  
  84.         r.X = c.X;
  85.         r.Y = c.Y - 1;
  86.         if (onMap(r, mas, rows, cols) && visited[r.Y][r.X] == 0) {
  87.             Queue.push(r);
  88.             visited[r.Y][r.X] = 1;
  89.             e.end = r;
  90.             Stack.push(e);
  91.         }
  92.         r.Y = c.Y + 1;
  93.         if (onMap(r, mas, rows, cols) && visited[r.Y][r.X] == 0) {
  94.             Queue.push(r);
  95.             visited[r.Y][r.X] = 1;
  96.             e.end = r;
  97.             Stack.push(e);
  98.         }
  99.     }
  100.    
  101.     if (flag == false) {
  102.         cout << "Path is not found!\n";
  103.         return;
  104.     }
  105.    
  106.     SetConsoleTextAttribute(h, 0xFC);
  107.     COORD req;
  108.     req.X = E.X; req.Y = E.Y;
  109.     while (!Stack.empty()) {
  110.         Edge e = Stack.top();
  111.         Stack.pop();
  112.         if (e.begin.X == B.X && e.begin.Y == B.Y) break;
  113.         if (req.X == e.end.X && req.Y == e.end.Y) {
  114.            
  115.             COORD temp = e.begin;
  116.             temp.Y += 2;
  117.            
  118.             SetConsoleCursorPosition(h, temp);
  119.             req.X = e.begin.X; req.Y = e.begin.Y;
  120.             cout << "*";
  121.         }
  122.     }
  123.  
  124. }
  125.  
  126. int main() {
  127.     fstream f;
  128.     f.open("data/file5.txt", ios::in);
  129.  
  130.     char ** line = new char*[21];
  131.     for (int i = 0; i < 21; ++i) {
  132.         line[i] = new char[21];
  133.     }
  134.  
  135.     int count = 0;
  136.     while(f.getline(line[count], 21)) {
  137.         cout << line[count] << endl;
  138.         count++;
  139.     }
  140.  
  141.     f.close();
  142.     find_path(line, 20, 20);
  143.    
  144.     cin.get();
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement