Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- п»ї#include <iostream>
- #include <fstream>
- #include <vector>
- #include <queue>
- #include <string>
- using namespace std;
- int main() {
- ifstream input("input.txt");
- ofstream output("output.txt");
- int size;
- int i = 0;
- input >> size;
- if (size == 1) {
- output << 0;
- return 0;
- }
- vector<vector<bool>> isPassable(size, vector<bool>(size, false));
- vector<vector<int>> isChecked(size, vector<int>(size, 0));
- string temp;
- getline(input, temp);
- while (!input.eof()) {
- getline(input, temp);
- if (temp == "") break;
- for (int j = 0; j < size; j++) {
- if (temp[j] == '.') {
- isPassable[i][j] = true;
- }
- }
- ++i;
- }
- if (!isPassable[0][0]) {
- output << -1;
- return 0;
- }
- queue <pair<int, int>> graph;
- graph.push(make_pair(0, 0));
- while (!graph.empty()) {
- pair<int, int> point = graph.front();
- graph.pop();
- if (point.first != 0 && isPassable[point.first - 1][point.second] && isChecked[point.first - 1][point.second] == 0) {
- isChecked[point.first - 1][point.second] = isChecked[point.first][point.second] + 1;
- graph.push(make_pair(point.first - 1, point.second));
- }
- if (point.second != 0 && isPassable[point.first][point.second - 1] && isChecked[point.first][point.second - 1] == 0) {
- isChecked[point.first][point.second - 1] = isChecked[point.first][point.second] + 1;
- graph.push(make_pair(point.first, point.second - 1));
- }
- if (point.first != size - 1 && isPassable[point.first + 1][point.second] && isChecked[point.first + 1][point.second] == 0) {
- isChecked[point.first + 1][point.second] = isChecked[point.first][point.second] + 1;
- graph.push(make_pair(point.first + 1, point.second));
- }
- if (point.second != size - 1 && isPassable[point.first][point.second + 1] && isChecked[point.first][point.second + 1] == 0) {
- isChecked[point.first][point.second + 1] = isChecked[point.first][point.second] + 1;
- graph.push(make_pair(point.first, point.second + 1));
- }
- }
- if (isChecked[size - 1][size - 1] == 0) {
- output << -1;
- return 0;
- }
- output << isChecked[size - 1][size - 1];
- input.close();
- output.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement