Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8.     ifstream fin("maze.in");
  9.     ofstream fout("maze.out");
  10.  
  11.     setlocale(LC_ALL, "Russian");
  12.    
  13.     int rows, cols;
  14.     int counter = 0;
  15.  
  16.     queue <int> queuex;
  17.     queue <int> queuey;
  18.  
  19.     fin >> rows >> cols;
  20.     rows += 2;
  21.     cols += 2;
  22.  
  23.     int** array = new int* [rows]();
  24.  
  25.     for (int i = 0; i < rows; i++) {
  26.         array[i] = new int[cols] ();
  27.     }
  28.  
  29.     int xstart, ystart, xend, yend;
  30.     fin >> xstart >> ystart;
  31.     fin >> xend >> yend;
  32.  
  33.     for (int j = 1; j < rows - 1; j++) {
  34.         for (int i = 1; i < cols - 1; i++) {
  35.             fin >> array[j][i];
  36.         }
  37.     }
  38.  
  39.     queuex.push(xstart);
  40.     queuey.push(ystart);
  41.     array[xstart][ystart] = 2;
  42.     while (!queuex.empty()) {
  43.         if (array[queuex.front()][queuey.front() + 1] == 1) {
  44.             array[queuex.front()][queuey.front() + 1] = array[queuex.front()][queuey.front()] + 1;
  45.             queuex.push(queuex.front());
  46.             queuey.push(queuey.front() + 1);
  47.         }
  48.         if (array[queuex.front()][queuey.front() - 1] == 1) {
  49.             array[queuex.front()][queuey.front() - 1] = array[queuex.front()][queuey.front()] + 1;
  50.             queuex.push(queuex.front());
  51.             queuex.push(queuey.front() - 1);
  52.         }
  53.         if (array[queuex.front() - 1][queuey.front()] == 1) {
  54.             array[queuex.front() - 1][queuey.front()] = array[queuex.front()][queuey.front()] + 1;
  55.             queuex.push(queuex.front() - 1);
  56.             queuey.push(queuey.front());
  57.         }
  58.         if (((array[queuex.front() + 1][queuey.front()] == 1)) ) {
  59.             array[queuex.front() + 1][queuey.front()] = array[queuex.front()][queuey.front()] + 1;
  60.             queuex.push(queuex.front() + 1);
  61.             queuey.push(queuey.front());
  62.         }
  63.         if (queuex.front() == xend && queuey.front() == yend) {
  64.             counter = 1;
  65.             break;
  66.         }
  67.         queuex.pop();
  68.         queuey.pop();
  69.     }
  70.  
  71.     if (counter == 0) {
  72.         fout << -1;
  73.     }
  74.     else {
  75.         vector <int> vecx;
  76.         vector <int> vecy;
  77.         int distance = array[xend][yend];
  78.         fout << distance - 2 << endl;
  79.         int tempx = xend;
  80.         int tempy = yend;
  81.         vecx.push_back(xend);
  82.         vecy.push_back(yend);
  83.  
  84.         while (distance != 1)
  85.         {
  86.             if (array[tempx + 1][tempy] == distance && (tempx != rows || tempx != cols)) {
  87.                 vecx.push_back(tempx + 1);
  88.                 vecy.push_back(tempy);
  89.                     tempx += 1;
  90.                 continue;
  91.             }
  92.             if (array[tempx - 1][tempy] == distance && (tempx != rows || tempx != cols ) ) {
  93.                 vecx.push_back(tempx - 1);
  94.                 vecy.push_back(tempy);
  95.                 tempx -= 1;
  96.                 continue;
  97.             }
  98.             if (array[tempx][tempy + 1] == distance && (tempy != rows || tempy != cols)) {
  99.                 vecx.push_back(tempx);
  100.                 vecy.push_back(tempy + 1);
  101.                     tempy += 1;
  102.                 continue;
  103.             }
  104.             if (array[tempx][tempy - 1] == distance && (tempy != rows || tempy != cols)) {
  105.                 vecx.push_back(tempx);
  106.                 vecy.push_back(tempy - 1);
  107.                 tempy -= 1;
  108.                 continue;
  109.             }
  110.             --distance;
  111.         }
  112.  
  113.         for(int i = vecx.size() - 1; i >= 0; i--){
  114.             fout << vecx[i] << " " << vecy[i] << endl;
  115.         }
  116.        
  117.     }
  118.  
  119.     for (int i = 0; i < rows; i++) {
  120.         delete[] array[i];
  121.     }
  122.     delete[] array;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement