Advertisement
Guest User

Занятие 26.06.2019

a guest
Jun 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. #include <iostream> // Для консольного ввода вывода
  2. #include <vector> //Для использования векторов (динамических массивов)
  3. //#include "windows.h"
  4. #include <ctime> //Таймер для генератора случайных чисел
  5. #include <algorithm>
  6. #include <set>
  7. #include <iomanip>
  8. #include <queue>
  9.  
  10. using namespace std; // Пространство имён по умолчанию
  11.  
  12.  
  13. struct point
  14. {
  15.     int x;
  16.     int y;
  17. };
  18.  
  19. vector<vector<bool>> field;
  20. vector<vector<int>> answer;
  21.  
  22.  
  23. vector<vector<int>> answer2;
  24.  
  25. queue < point > _buf[2];
  26.  
  27. queue< point > _now;
  28. queue< point > _next;
  29.  
  30. int h, w;
  31.  
  32. vector<point> cheek(point p0)
  33. {
  34.     vector<point> ret;
  35.     vector<int> x = { -1, 0, 1, 0 };
  36.     vector<int> y = { 0, -1, 0, 1 };
  37.     int i; point p;
  38.     for (i = 0; i < x.size(); ++i)
  39.     {
  40.         p.x = p0.x + x[i];
  41.         p.y = p0.y + y[i];
  42.  
  43.         if ((p.x < 0) || (p.x >= w)) continue;
  44.         if ((p.y < 0) || (p.y >= h)) continue;
  45.  
  46.         if ((field[p.y][p.x]) && (answer[p.y][p.x] == -1))
  47.         {
  48.             answer[p.y][p.x] = answer[p0.y][p0.x] + 1;
  49.             ret.push_back(p);
  50.         }
  51.  
  52.     }
  53.  
  54.     return (ret);
  55. }
  56.  
  57. void requ(point p0, int step)
  58. {
  59.     answer2[p0.y][p0.x] = step;
  60.  
  61.     vector<int> x = { -1, 0, 1, 0 };
  62.     vector<int> y = { 0, -1, 0, 1 };
  63.     int i; point p;
  64.     for (i = 0; i < x.size(); ++i)
  65.     {
  66.         p.x = p0.x + x[i];
  67.         p.y = p0.y + y[i];
  68.  
  69.         if ((p.x < 0) || (p.x >= w)) continue;
  70.         if ((p.y < 0) || (p.y >= h)) continue;
  71.  
  72.         if ((field[p.y][p.x]) && (answer2[p.y][p.x] == -1))
  73.         {
  74.             requ(p, step + 1);
  75.         }
  76.  
  77.     }
  78.  
  79.  
  80. }
  81.  
  82.  
  83.  
  84.  
  85. int main()
  86. {
  87.     freopen("input.txt", "r", stdin);
  88.     freopen("output.txt", "w", stdout);
  89.  
  90.     point p, p0;
  91.     char ch;
  92.     int i ;
  93.  
  94.     //Высота ширина и начальная точка обхода
  95.     cin >> h >> w >> p0.x >> p0.y;
  96.    
  97.     field.resize(h); answer.resize(h); answer2.resize(h);
  98.     for (p.y = 0; p.y < h; ++p.y)
  99.     {
  100.         field[p.y].resize(w); answer[p.y].resize(w); answer2[p.y].resize(w);
  101.         for (p.x = 0; p.x < w; ++p.x)
  102.         {
  103.             answer[p.y][p.x] = -1;
  104.             answer2[p.y][p.x] = -1;
  105.             cin >> ch;
  106.             field[p.y][p.x] = (ch == '*');
  107.         }
  108.     }  
  109.  
  110.     answer[p0.y][p0.x] = 0;
  111.  
  112.     /*
  113.     //Поиск в ширину (хуже)
  114.  
  115.     _now.push(p0);
  116.  
  117.     while (_now.size()>0 || _next.size()>0)
  118.     {
  119.         if (i % 2 == 0)
  120.         {
  121.             while (_now.size() > 0)
  122.             {
  123.                 for (const auto& it: cheek(_now.front()) )
  124.                 {
  125.                     _next.push(it);
  126.                 }
  127.                 _now.pop();
  128.             }
  129.             ++i;
  130.         }
  131.         else
  132.         {
  133.             while (_next.size() > 0)
  134.             {
  135.                 for (const auto& it : cheek(_next.front()))
  136.                 {
  137.                     _now.push(it);
  138.                 }
  139.                 _next.pop();
  140.             }
  141.             ++i;
  142.         }
  143.        
  144.     }*/
  145.  
  146.     //Поиск в ширину
  147.     i = 0;
  148.     _buf[0].push(p0);
  149.  
  150.     while (_buf[0].size()+_buf[1].size()>0)
  151.     {
  152.        
  153.         while (_buf[i % 2].size() > 0)
  154.         {
  155.             for (const auto& it : cheek(_buf[i % 2].front()))
  156.             {
  157.                 _buf[(i+1) % 2].push(it);
  158.             }
  159.             _buf[i % 2].pop();
  160.         }
  161.         ++i;
  162.  
  163.     }
  164.  
  165.     cout << "Вывод результата поиска в ширину" << endl;
  166.     for (p.y = 0; p.y < h; ++p.y)
  167.     {
  168.         for (p.x = 0; p.x < w; ++p.x)
  169.         {
  170.             cout<<answer[p.y][p.x]<<"\t";
  171.         }
  172.         cout << endl;
  173.     }
  174.  
  175.     cout << endl;
  176.  
  177.     //Поиск в глубину
  178.     requ(p0, 0);
  179.  
  180.  
  181.     cout << "Вывод результата поиска в глубину" << endl;
  182.     for (p.y = 0; p.y < h; ++p.y)
  183.     {
  184.         for (p.x = 0; p.x < w; ++p.x)
  185.         {
  186.             cout << answer2[p.y][p.x] << "\t";
  187.         }
  188.         cout << endl;
  189.     }
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement