Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. п»ї#include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <queue>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.     ifstream input("input.txt");
  11.     ofstream output("output.txt");
  12.  
  13.     int size;
  14.     int i = 0;
  15.  
  16.     input >> size;
  17.  
  18.     if (size == 1) {
  19.         output << 0;
  20.         return 0;
  21.     }
  22.  
  23.     vector<vector<bool>> isPassable(size, vector<bool>(size, false));
  24.     vector<vector<int>> isChecked(size, vector<int>(size, 0));
  25.  
  26.     string temp;
  27.     getline(input, temp);
  28.  
  29.     while (!input.eof()) {
  30.         getline(input, temp);
  31.         if (temp == "") break;
  32.         for (int j = 0; j < size; j++) {
  33.             if (temp[j] == '.') {
  34.                 isPassable[i][j] = true;
  35.             }
  36.         }
  37.         ++i;
  38.     }
  39.  
  40.     if (!isPassable[0][0]) {
  41.         output << -1;
  42.         return 0;
  43.     }
  44.  
  45.     queue <pair<int, int>> graph;
  46.     graph.push(make_pair(0, 0));
  47.  
  48.     while (!graph.empty()) {
  49.         pair<int, int> point = graph.front();
  50.         graph.pop();
  51.         if (point.first != 0 && isPassable[point.first - 1][point.second] && isChecked[point.first - 1][point.second] == 0) {
  52.             isChecked[point.first - 1][point.second] = isChecked[point.first][point.second] + 1;
  53.             graph.push(make_pair(point.first - 1, point.second));
  54.         }
  55.         if (point.second != 0 && isPassable[point.first][point.second - 1] && isChecked[point.first][point.second - 1] == 0) {
  56.             isChecked[point.first][point.second - 1] = isChecked[point.first][point.second] + 1;
  57.             graph.push(make_pair(point.first, point.second - 1));
  58.         }
  59.         if (point.first != size - 1 && isPassable[point.first + 1][point.second] && isChecked[point.first + 1][point.second] == 0) {
  60.             isChecked[point.first + 1][point.second] = isChecked[point.first][point.second] + 1;
  61.             graph.push(make_pair(point.first + 1, point.second));
  62.         }
  63.         if (point.second != size - 1 && isPassable[point.first][point.second + 1] && isChecked[point.first][point.second + 1] == 0) {
  64.             isChecked[point.first][point.second + 1] = isChecked[point.first][point.second] + 1;
  65.             graph.push(make_pair(point.first, point.second + 1));
  66.         }
  67.     }
  68.  
  69.     if (isChecked[size - 1][size - 1] == 0) {
  70.         output << -1;
  71.         return 0;
  72.     }
  73.  
  74.     output << isChecked[size - 1][size - 1];
  75.     input.close();
  76.     output.close();
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement