botgob

Untitled

Mar 18th, 2019
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <queue>
  5.  
  6. using namespace std;
  7.  
  8. class Node {
  9. public:
  10.     int x, y, ln;
  11.     Node();
  12. };
  13. Node::Node() {
  14.  
  15. }
  16.  
  17. bool vis[100][100];
  18. char mp[100][100];
  19. int main() {
  20.     for (int i = 0; i < 100; i++) {
  21.         for (int j = 0; j < 100; j++) {
  22.             mp[i][j] = false;
  23.         }
  24.     }
  25.     Node p;
  26.     p.x = 0;
  27.     p.y = 0;
  28.     p.ln = 0;
  29.     int y = 0;
  30.     int x = 0;
  31.     int e = 0;
  32.     int tot = 0;
  33.     cin >> y >> x >> e;
  34.     for (int i = 0; i < y; i++) {
  35.         string tmp = "";
  36.         cin >> tmp;
  37.         for (int j = 0; j < x; j++) {
  38.             mp[i][j] = tmp[j];
  39.         }
  40.     }
  41.     queue<Node> q;
  42.     q.push(p);
  43.     while (!q.empty() && vis[y][x] == false) {
  44.         Node curr = q.front();
  45.         q.pop();
  46.         if (curr.x + 1 <= x && mp[curr.y][curr.x + 1] != '#' && vis[curr.y][curr.x + 1] == false) {
  47.             Node next;
  48.             next.x = curr.x + 1;
  49.             next.y = curr.y;
  50.             next.ln = curr.ln + 1;
  51.             tot = curr.ln + 1;
  52.             q.push(next);
  53.             vis[curr.y][curr.x + 1] = true;
  54.         }
  55.         if (curr.y + 1 <= y && mp[curr.y + 1][curr.x] != '#' && vis[curr.y + 1][curr.x] == false) {
  56.             Node next;
  57.             next.x = curr.x;
  58.             next.y = curr.y + 1;
  59.             next.ln = curr.ln + 1;
  60.             tot = curr.ln + 1;
  61.             q.push(next);
  62.             vis[curr.y + 1][curr.x] = true;
  63.         }
  64.         if (curr.x - 1 >= 0 && mp[curr.y][curr.x - 1] != '#' && vis[curr.y][curr.x - 1] == false) {
  65.             Node next;
  66.             next.x = curr.x - 1;
  67.             next.y = curr.y;
  68.             next.ln = curr.ln + 1;
  69.             tot = curr.ln + 1;
  70.             q.push(next);
  71.             vis[curr.y][curr.x - 1] = true;
  72.         }
  73.         if (curr.y - 1 >= 0 && mp[curr.y - 1][curr.x] != '#' && vis[curr.y - 1][curr.x] == false) {
  74.             Node next;
  75.             next.x = curr.x;
  76.             next.y = curr.y - 1;
  77.             next.ln = curr.ln + 1;
  78.             tot = curr.ln + 1;
  79.             q.push(next);
  80.             vis[curr.y - 1][curr.x] = true;
  81.         }
  82.     }
  83.     if (vis[y][x] == false) {
  84.         cout << "nej";
  85.     } else {
  86.         cout << tot;
  87.     }
  88. #ifdef _DEBUG
  89.     system("pause");
  90. #endif // _DEBUG
  91. }
Advertisement
Add Comment
Please, Sign In to add comment