Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <deque>
- #include <vector>
- #include <algorithm>
- using namespace std;
- struct point {
- int xi;
- int xj;
- };
- int find(point dot, vector <vector<point>>& g) {
- for (int i = 0; i < g.size(); i++)
- if (g[i][0].xi == dot.xi && g[i][0].xj == dot.xj)
- return i;
- return -1;
- }
- int main() {
- int n, m;
- cin >> n >> m;
- int si, sj;
- cin >> si >> sj;
- vector <vector <int>> a(n + 2, vector <int>(m + 2, -2));
- point start;
- start.xi = si;
- start.xj = sj;
- deque <point> deq;
- deq.push_back(start);
- for (int i = 1; i < n + 1; i++)
- for (int j = 1; j < m + 1; j++) {
- cin >> a[i][j];
- if (a[i][j] == 0)
- a[i][j] = -1;
- else
- a[i][j] = -2;
- }
- a[si][sj] = 0;
- int h;
- cin >> h;
- vector <vector<point>> g(h, vector<point>(2));
- point gip;
- for (int i = 0; i < h; i++) {
- cin >> gip.xi;
- cin >> gip.xj;
- g[i][0] = gip;
- cin >> gip.xi;
- cin >> gip.xj;
- g[i][1] = gip;
- }
- int w;
- cin >> w;
- point way;
- vector <point> ways(w);
- for (int i = 0; i < w; i++) {
- cin >> way.xi;
- cin >> way.xj;
- ways[i] = way;
- }
- while (!deq.empty()) {
- point dot = deq.front();
- deq.pop_front();
- point ndot;
- int ind = find(dot, g);
- if (ind != -1 && a[g[ind][1].xi][g[ind][1].xj] == -1) {
- gip.xi = g[ind][1].xi;
- gip.xj = g[ind][1].xj;
- a[gip.xi][gip.xj] = a[dot.xi][dot.xj] + 1;
- deq.push_back(gip);
- }
- //----------------------------
- if (a[dot.xi - 1][dot.xj] == -1) {
- a[dot.xi - 1][dot.xj] = a[dot.xi][dot.xj] + 1;
- ndot.xi = dot.xi - 1;
- ndot.xj = dot.xj;
- deq.push_back(ndot);
- }
- if (a[dot.xi][dot.xj + 1] == -1) {
- a[dot.xi][dot.xj + 1] = a[dot.xi][dot.xj] + 1;
- ndot.xi = dot.xi;
- ndot.xj = dot.xj + 1;
- deq.push_back(ndot);
- }
- if (a[dot.xi + 1][dot.xj] == -1) {
- a[dot.xi + 1][dot.xj] = a[dot.xi][dot.xj] + 1;
- ndot.xi = dot.xi + 1;
- ndot.xj = dot.xj;
- deq.push_back(ndot);
- }
- if (a[dot.xi][dot.xj - 1] == -1) {
- a[dot.xi][dot.xj - 1] = a[dot.xi][dot.xj] + 1;
- ndot.xi = dot.xi;
- ndot.xj = dot.xj - 1;
- deq.push_back(ndot);
- }
- /*
- for (int i = 0; i < n + 2; i++) {
- for (int j = 0; j < m + 2; j++)
- cout << a[i][j] << " ";
- cout << endl;
- }
- cout << "-------------------------------"<< endl;
- */
- }
- int min = 1000 * 1000;
- for (int i = 0; i < ways.size(); i++) {
- if (a[ways[i].xi][ways[i].xj] != -1 && a[ways[i].xi][ways[i].xj] < min)
- min = a[ways[i].xi][ways[i].xj];
- }
- if (min == 1000 * 1000)
- cout << "Impossible";
- else
- cout << min + 1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement