Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <algorithm>
- #include <cstring>
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <queue>
- #include <map>
- #include <deque>
- using namespace std;
- ifstream in { "input.in" };
- ofstream out { "output.out" };
- using Pair = pair<int, int>;
- const int dx[] = { -1, 0, 1, 0 };
- const int dy[] = { 0, 1, 0, -1 };
- int n;
- Pair start, stop;
- bool a[200][200];
- void citeste() {
- int m;
- in >> n >> m;
- while (m--) {
- int i, j;
- in >> i >> j;
- a[i][j] = true;
- }
- in >> start.first >> start.second >> stop.first >> stop.second;
- --start.first;
- --start.second;
- --stop.first;
- --stop.second;
- }
- int Lee() {
- int d[200][200] = {};
- d[start.first][start.second] = 1;
- a[start.first][start.second] = true;
- queue<Pair> q;
- q.push(start);
- while (!q.empty()) {
- Pair p = q.front();
- q.pop();
- for (int k { 0 }; k < 4; ++k) {
- int i { p.first + dx[k] };
- int j { p.second + dy[k] };
- if (i >= 0 && j >= 0 && i < n && j < n && !a[i][j] && !d[i][j]) {
- d[i][j] = d[p.first][p.second] + 1;
- if (i == stop.first && j == stop.second)
- return d[i][j];
- q.emplace(i, j);
- }
- }
- }
- return d[stop.first][stop.second];
- }
- int main() {
- citeste();
- out << Lee();
- }
Add Comment
Please, Sign In to add comment