Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <queue>
  4.  
  5. std::ifstream fin("alee.in");
  6. std::ofstream fout("alee.out");
  7.  
  8. using namespace std;
  9.  
  10. int a[176][176], n, m, ip1, jp1, ip2, jp2;
  11.  
  12. const int di[] = { 0,0,-1,1 }, dj[] = { -1,1, 0,0 };
  13.  
  14. inline bool Inside(int i, int j)
  15. {
  16. return i > 0 && i <= n && j > 0 && j <= n;
  17. }
  18.  
  19. void Fill(int is, int js)
  20. {
  21. queue<pair<int,int>> Q;
  22. a[is][js] = 1;
  23. Q.push({ is, js });
  24. while (!Q.empty())
  25. {
  26. int i, j;
  27. i = Q.front().first;
  28. j = Q.front().second;
  29. for (int k = 0; k < 4; k++)
  30. {
  31. int iv, jv;
  32. iv = i + di[k];
  33. jv = j + dj[k];
  34. if (Inside(iv, jv) && a[iv][jv] == 0)
  35. a[iv][jv] = a[i][j] + 1, Q.push(make_pair(iv, jv));
  36. }
  37. Q.pop();
  38. }
  39.  
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45. fin >> n >> m;
  46. for (int i = 1; i <= m; i++)
  47. {
  48. int c, b;
  49. fin >> c >> b;
  50. a[c][b] = -1;
  51. }
  52. fin >> ip1 >> jp1 >> ip2 >> jp2;
  53.  
  54. Fill(ip1, jp1);
  55. fout << a[ip2][jp2];
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement