Advertisement
EgorS2000

Untitled

Nov 10th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <string>
  5. #include <deque>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     ifstream IN;
  11.     ofstream OUT;
  12.     IN.open("in.txt");
  13.     string str = "";
  14.  
  15.     string buffer = "";
  16.     int count_of_rows = 0, count_of_cols = 0;
  17.    
  18.     getline(IN, str);
  19.     str += ' ';
  20.     for (int i = 0; i < str.size(); i++)
  21.     {
  22.         if (str[i] != ' ')
  23.             buffer += str[i];
  24.         if (str[i] == ' ')
  25.         {
  26.             bool check = false;
  27.            
  28.             if ((count_of_rows == 0) && (check == false))
  29.                 count_of_rows = stoi(buffer);
  30.             else if ((count_of_cols == 0) && (check == false))
  31.                 count_of_cols = stoi(buffer);
  32.             buffer = "";
  33.         }
  34.     }
  35.     buffer = "";
  36.  
  37.     vector<vector<int>> matrix(count_of_rows, vector<int>(count_of_cols));
  38.  
  39.     for (int i = 0; i < count_of_rows; i++)
  40.     {
  41.         int counter = 0;
  42.  
  43.         getline(IN, str);
  44.         str += " ";
  45.         for (int j = 0; j < str.size(); j++)
  46.         {
  47.             if (str[j] != ' ')
  48.             {
  49.                 buffer += str[j];
  50.             }
  51.             if (str[j] == ' ')
  52.             {
  53.                 int elem = stoi(buffer);
  54.                 matrix[i][counter] = elem;
  55.                 counter++;
  56.                 buffer = "";
  57.             }
  58.         }
  59.     }
  60.     buffer = "";
  61.    
  62.     int x_s = 0, y_s = 0, x_e = 0, y_e = 0;
  63.  
  64.     getline(IN, str);
  65.     str += " ";
  66.     for (int i = 0; i < str.size(); i++)
  67.     {
  68.         bool check = false;
  69.        
  70.         if (str[i] != ' ')
  71.             buffer += str[i];
  72.         if (str[i] == ' ')
  73.         {
  74.             if((x_s == 0) && (check == false))
  75.                 x_s = stoi(buffer);
  76.             else if(y_s == 0 && check == false)
  77.                 y_s = stoi(buffer);
  78.             else if(x_e == 0 && check == false)
  79.                 x_e = stoi(buffer);
  80.             else if(y_e == 0 && check == false)
  81.                 y_e = stoi(buffer);
  82.             buffer = "";
  83.         }
  84.     }
  85.     IN.close();
  86.  
  87.     vector< pair<int,int>> steps = { {-2, -1}, {-2, 1}, {2, 1}, {2, -1}, {1, 2}, {-1, 2},{1, -2}, {-1, -2} };
  88.  
  89.     deque<pair<int, int>> q;
  90.  
  91.     matrix[(long)x_s - 1][(long)y_s - 1] = 1;
  92.  
  93.     q.push_back(make_pair(x_s - 1, y_s - 1));
  94.     int result = -1;
  95.     bool check = false;
  96.     int j = 0;
  97.  
  98.     while (true)
  99.     {
  100.         for (int i = 0; i < steps.size(); i++)
  101.         {
  102.             if ((0 <= q[j].first + steps[i].first) && (q[j].first + steps[i].first < count_of_rows))
  103.             {
  104.                 if ((0 <= q[j].second + steps[i].second) && (q[j].second + steps[i].second < count_of_cols))
  105.                 {
  106.                     int cur_x = (q[j].first) + (steps[i].first);
  107.                     int cur_y = (q[j].second) + (steps[i].second);
  108.  
  109.                     if (matrix[cur_x][cur_y] == 0)
  110.                     {
  111.                         q.push_back(make_pair(cur_x, cur_y));
  112.                         matrix[cur_x][cur_y] = matrix[q[j].first][q[j].second] + 1;
  113.                         if (cur_x == x_e - 1 && cur_y == y_e - 1)
  114.                         {
  115.                             result = matrix[cur_x][cur_y] - 1;
  116.                             check = true;
  117.                             break;
  118.                         }
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.         q.pop_front();
  124.  
  125.         if (check == true || q.empty())
  126.         {
  127.             break;
  128.         }
  129.     }
  130.     OUT.open("out.txt");
  131.     if (x_s == x_e && y_s == y_e)
  132.     {
  133.         result = 0;
  134.     }
  135.     if (result == -1)
  136.     {
  137.         OUT << "No";
  138.     }
  139.     else
  140.     {
  141.         OUT << result;
  142.     }
  143.     OUT.close();
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement