Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <queue>
- using namespace std;
- ifstream cin("mr.in");
- ofstream cout("mr.out");
- const int di[] = {0 , 0 , -1 , 1};
- const int dj[] = {-1 , 1 , 0 , 0};
- int n , m , t , x1 , y1 , x2 , y2 , b[101][101];
- struct poz
- {
- int i , j;
- };
- struct per
- {
- int it , jt , nr;
- }a[101][101];
- queue <poz> q;
- int inside(int i , int j)
- {
- return i >= 1 && i <= n && j >= 1 && j <= m;
- }
- void lee()
- {
- poz r;
- r.i = 1 , r.j = 1;
- b[r.i][r.j] = 1;
- q.push(r);
- while(!q.empty())
- {
- poz r;
- r = q.front();
- if(a[r.i][r.j].it)
- {
- int inou = a[r.i][r.j].it;
- int jnou = a[r.i][r.j].jt;
- if(a[inou][jnou].nr != -1 && (b[inou][jnou] > b[r.i][r.j] + 1 || b[inou][jnou] == 0))
- {
- b[inou][jnou] = b[r.i][r.j] + 1;
- q.push({inou , jnou});
- }
- }
- for(int d = 0 ; d < 4 ; d++)
- {
- int inou = r.i + di[d];
- int jnou = r.j + dj[d];
- if(inside(inou , jnou) && a[inou][jnou].nr != -1 && b[inou][jnou] == 0)
- {
- b[inou][jnou] = b[r.i][r.j] + 1;
- q.push({inou , jnou});
- }
- }
- q.pop();
- }
- }
- int main()
- {
- cin >> n >> m;
- for(int i = 1 ; i <= n ; i++)
- for(int j = 1 ; j <= m ; j++)
- cin >> a[i][j].nr;
- cin >> t;
- for(int i = 0 ; i < t ; i++)
- {
- cin >> x1 >> y1 >> x2 >> y2;
- a[x1][y1].it = x2;
- a[x1][y1].jt = y2;
- a[x2][y2].it = x1;
- a[x2][y2].jt = y1;
- }
- lee();
- cout << b[n][m] - 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment