nicuvlad76

Untitled

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