nicuvlad76

Untitled

Jan 27th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. ifstream cin("mr.in");
  6. ofstream cout("mr.out");
  7.  
  8. const int di[] = {0 , 0 , -1 , 1};
  9. const int dj[] = {-1 , 1 , 0 , 0};
  10. int n , m , t , x1 , y1 , x2 , y2 , b[101][101];
  11. struct poz
  12. {
  13. int i , j;
  14. };
  15.  
  16. struct per
  17. {
  18. int it , jt , nr;
  19. }a[101][101];
  20. queue <poz> q;
  21.  
  22. int inside(int i , int j)
  23. {
  24. return i >= 1 && i <= n && j >= 1 && j <= m;
  25. }
  26.  
  27. void lee()
  28. {
  29. poz r;
  30. r.i = 1 , r.j = 1;
  31. b[r.i][r.j] = 1;
  32. q.push(r);
  33. while(!q.empty())
  34. {
  35. poz r;
  36. r = q.front();
  37. if(a[r.i][r.j].it)
  38. {
  39. int inou = a[r.i][r.j].it;
  40. int jnou = a[r.i][r.j].jt;
  41. if(a[inou][jnou].nr != -1 && (b[inou][jnou] > b[r.i][r.j] + 1 || b[inou][jnou] == 0))
  42. {
  43. b[inou][jnou] = b[r.i][r.j] + 1;
  44. q.push({inou , jnou});
  45. }
  46. }
  47. for(int d = 0 ; d < 4 ; d++)
  48. {
  49. int inou = r.i + di[d];
  50. int jnou = r.j + dj[d];
  51. if(inside(inou , jnou) && a[inou][jnou].nr != -1 && b[inou][jnou] == 0)
  52. {
  53. b[inou][jnou] = b[r.i][r.j] + 1;
  54. q.push({inou , jnou});
  55. }
  56. }
  57. q.pop();
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. cin >> n >> m;
  64. for(int i = 1 ; i <= n ; i++)
  65. for(int j = 1 ; j <= m ; j++)
  66. cin >> a[i][j].nr;
  67. cin >> t;
  68. for(int i = 0 ; i < t ; i++)
  69. {
  70. cin >> x1 >> y1 >> x2 >> y2;
  71. a[x1][y1].it = x2;
  72. a[x1][y1].jt = y2;
  73. a[x2][y2].it = x1;
  74. a[x2][y2].jt = y1;
  75. }
  76. lee();
  77. cout << b[n][m] - 1;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment